Querying database data using Spark SQL in Scala

You can execute Spark SQL queries in Scala by starting the Spark shell. When you start Spark, DataStax Enterprise creates a Spark session instance to allow you to run Spark SQL queries against database tables.

When you start Spark, DataStax Enterprise creates a Spark session instance to allow you to run Spark SQL queries against database tables. The session object is named spark and is an instance of org.apache.spark.sql.SparkSession. Use the sql method to execute the query.

Procedure

  1. Start the Spark shell.
    dse spark
  2. Use the sql method to pass in the query, storing the result in a variable.
    val results = spark.sql("SELECT * from my_keyspace_name.my_table")
  3. Use the returned data.
    results.show()
    +--------------------+-----------+
    |                  id|description|
    +--------------------+-----------+
    |de2d0de1-4d70-11e...|      thing|
    |db7e4191-4d70-11e...|    another|
    |d576ad50-4d70-11e...|yet another|
    +--------------------+-----------+