Search in sources :

Example 1 with CassandraRow

use of com.datastax.spark.connector.CassandraRow in project learning-spark by databricks.

the class BasicQueryCassandra method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        throw new Exception("Usage BasicLoadJson [sparkMaster] [cassandraHost]");
    }
    String sparkMaster = args[0];
    String cassandraHost = args[1];
    SparkConf conf = new SparkConf(true).set("spark.cassandra.connection.host", cassandraHost);
    JavaSparkContext sc = new JavaSparkContext(sparkMaster, "basicquerycassandra", conf);
    // entire table as an RDD
    // assumes your table test was created as CREATE TABLE test.kv(key text PRIMARY KEY, value int);
    JavaRDD<CassandraRow> data = javaFunctions(sc).cassandraTable("test", "kv");
    // print some basic stats
    System.out.println(data.mapToDouble(new DoubleFunction<CassandraRow>() {

        public double call(CassandraRow row) {
            return row.getInt("value");
        }
    }).stats());
    // write some basic data to Cassandra
    ArrayList<KeyValue> input = new ArrayList<KeyValue>();
    input.add(KeyValue.newInstance("mostmagic", 3));
    JavaRDD<KeyValue> kvRDD = sc.parallelize(input);
    javaFunctions(kvRDD, KeyValue.class).saveToCassandra("test", "kv");
}
Also used : ArrayList(java.util.ArrayList) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) CassandraRow(com.datastax.spark.connector.CassandraRow) SparkConf(org.apache.spark.SparkConf)

Aggregations

CassandraRow (com.datastax.spark.connector.CassandraRow)1 ArrayList (java.util.ArrayList)1 SparkConf (org.apache.spark.SparkConf)1 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)1