public class JavaApiDemo implements Serializable {
public JavaApiDemo() {
// just an initialisation of Spark Context
DemoApp demoApp = DemoApp$.MODULE$.apply();
JavaSparkContext sc = new JavaSparkContext(demoApp.sc());
// here we are going to save some data to Cassandra...
List<Person> people = Arrays.asList(
Person.newInstance(1, "John", new Date()),
Person.newInstance(2, "Anna", new Date()),
Person.newInstance(3, "Andrew", new Date())
);
JavaRDD<Person> rdd = sc.parallelize(people);
javaFunctions(rdd)
.writerBuilder("test", "people", mapToRows(Person.class))
.saveToCassandra();
// use case: we want to read that data as an RDD of CassandraRows and convert them to strings...
JavaRDD<String> cassandraRowsRDD = javaFunctions(sc).cassandraTable("test", "people")
.map(new Function<CassandraRow, String>() {
@Override
public String call(CassandraRow cassandraRow) throws Exception {
return cassandraRow.toString();
}
});
System.out.println("Data as CassandraRows: \n" + StringUtils.join("\n", cassandraRowsRDD.collect()));
// use case: we want to read that data as an RDD of Person beans and also convert them to strings...
JavaRDD<String> rdd2 = javaFunctions(sc).cassandraTable("test", "people", mapRowTo(Person.class))
.map(new Function<Person, String>() {
@Override
public String call(Person person) throws Exception {
return person.toString();
}
});
System.out.println("Data as Person beans: \n" + StringUtils.join("\n", rdd2.collect()));
// use case: we want to filter rows on the database side with use of the where clause
JavaRDD<String> rdd3 = javaFunctions(sc).cassandraTable("test", "people", mapRowTo(Person.class))
.where("name=?", "Anna").map(new Function<Person, String>() {
@Override
public String call(Person person) throws Exception {
return person.toString();
}
});
System.out.println("Data filtered by the where clause (name='Anna'): \n" + StringUtils.join("\n", rdd3.collect()));
// use case: we want to explicitly set a projection on the column set
JavaRDD<String> rdd4 = javaFunctions(sc).cassandraTable("test", "people")
.select("id").map(new Function<CassandraRow, String>() {
@Override
public String call(CassandraRow cassandraRow) throws Exception {
return cassandraRow.toString();
}
});
System.out.println("Data with only 'id' column fetched: \n" + StringUtils.join("\n", rdd4.collect()));
sc.stop();
}