Skip to content

Instantly share code, notes, and snippets.

@samuelorji
Created July 20, 2018 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelorji/3ebd895935a9a221a482b5e666b6eeb4 to your computer and use it in GitHub Desktop.
Save samuelorji/3ebd895935a9a221a482b5e666b6eeb4 to your computer and use it in GitHub Desktop.
A simple script that connects to a cassandra cluster and fetches data from it
package cassandra
import com.datastax.spark.connector.rdd.CassandraTableScanRDD
import org.apache.spark.{SparkConf, SparkContext}
object battles {
case class Battle(
battle_number: Option[Integer],
year: Option[Integer],
attacker_king: Option[String],
defender_king: Option[String]
)
def main(args : Array[String]) : Unit = {
// val sc = new SparkContext("local[*]","got-battles",)
val conf = new SparkConf().setAppName("got-battles").setMaster("local[*]")
.set("spark.cassandra.connection.host", "127.0.0.1")
val sc = new SparkContext(conf)
val battles :Array[Battle]= CassandraTableScanRDD[Battle](sc, "dev", "battles")
.select("battle_number", "year", "attacker_king", "defender_king").collect()
battles.foreach {
b: Battle =>
println("Battle Number %s was defended by %s.".format(b.battle_number.getOrElse("unknown"), b.defender_king.getOrElse("No One")))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment