Querying Cassandra 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 sets the context to allow you to run Spark SQL queries against Cassandra tables.

When you start Spark, DataStax Enterprise sets the context to allow you to run Spark SQL queries against Cassandra tables. Use the setKeyspace method to connect to a Cassandra keyspace, and then use the sql method to execute the query.

Procedure

  1. Start the Spark shell.
    $ dse spark
  2. Set the keyspace you'd like to query using the setKeyspace method.
    csc.setKeyspace("my_keyspace_name")
  3. Use the sql method to pass in the query, storing the result in a variable.
    val results = csc.sql("SELECT * from my_keyspace_name.my_table")
  4. Use the returned data.
    results.collect().foreach(println)
    CassandraRow{type_id: 1, value: 9685.807}
    CassandraRow{type_id: 2, value: -9775.808}