-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to new version of the connector. Add new Spark SQL example
- Loading branch information
1 parent
ea54538
commit e3ee5b5
Showing
9 changed files
with
114 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
src/main/scala/us/unemployment/demo/FromCassandraToCaseClass.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletions
17
src/main/scala/us/unemployment/demo/FromCassandraToRow.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,41 @@ | ||
package us.unemployment.demo | ||
|
||
import org.apache.spark.SparkContext._ | ||
import com.datastax.spark.connector._ | ||
import com.datastax.spark.connector.rdd.CassandraRDD | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
import us.unemployment.demo.UsUnemploymentSchema.{TABLE, KEYSPACE} | ||
|
||
object FromCassandraToRow { | ||
|
||
def main (args: Array[String]) { | ||
|
||
val conf = new SparkConf(true) | ||
.setAppName("read_csv_from_cassandra_into_case_class") | ||
.setMaster("local[4]") | ||
.setMaster("local") | ||
.set("spark.cassandra.connection.host", "localhost") | ||
|
||
|
||
val sc = new SparkContext(conf) | ||
|
||
sc.cassandraTable(UsUnemploymentSchema.KEYSPACE, UsUnemploymentSchema.TABLE) | ||
sc.cassandraTable(KEYSPACE, TABLE) | ||
.foreach( row => { | ||
val year = row.getInt("year") | ||
val unemployedPercentageToLabor = row.getDouble("unemployed_percentage_to_labor") | ||
println(s"""Year($year), unemployment % : $unemployedPercentageToLabor""") | ||
} | ||
) | ||
|
||
// or | ||
val unemployment: CassandraRDD[(String, Double)] = sc.cassandraTable(KEYSPACE, TABLE) | ||
.select("year", "unemployed_percentage_to_labor").as((_: String, _: Double)) | ||
|
||
println(" ------------Alternative ----------------- ") | ||
|
||
unemployment | ||
.sortByKey() | ||
.collect().foreach{case (year,percentage) => println(s""" Year($year), unemployment % : $percentage""")} | ||
|
||
sc.stop() | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/scala/us/unemployment/demo/FromCassandraToSQL.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package us.unemployment.demo | ||
|
||
import org.apache.spark.sql.SchemaRDD | ||
import org.apache.spark.sql.cassandra.CassandraSQLContext | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
import us.unemployment.demo.UsUnemploymentSchema.{KEYSPACE, TABLE} | ||
|
||
object FromCassandraToSQL { | ||
|
||
def main (args: Array[String]) { | ||
|
||
val conf = new SparkConf(true) | ||
.setAppName("read_csv_from_cassandra_into_case_class") | ||
.setMaster("local") | ||
.set("spark.cassandra.connection.host", "localhost") | ||
|
||
val sc = new SparkContext(conf) | ||
|
||
val cc = new CassandraSQLContext(sc) | ||
|
||
cc.setKeyspace(KEYSPACE) | ||
|
||
val row: SchemaRDD = cc.cassandraSql(s"SELECT year,unemployed_percentage_to_labor " + | ||
s"FROM $TABLE WHERE unemployed_percentage_to_labor > 8 " + | ||
s"ORDER BY year DESC") | ||
|
||
row.collect().foreach(row => println(s" Year(${row(0)}): ${row(1)}%")) | ||
|
||
sc.stop() | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters