コード例 #1
0
  /** Exercise 4 - Read a list of people from Cassandra */
  private static void readPeopleFromCassandra(JavaSparkContext javaSparkContext) {

    SparkContextJavaFunctions sparkContextJavaFunctions =
        CassandraJavaUtil.javaFunctions(javaSparkContext);

    CassandraJavaRDD<Person> personCassandraJavaRDD =
        sparkContextJavaFunctions.cassandraTable(
            "test", "people", CassandraJavaUtil.mapRowTo(Person.class));

    JavaRDD<String> readPeopleRDD = personCassandraJavaRDD.map(Person::toString);

    System.out.println(
        "Data as Person beans: \n" + StringUtils.join("\n", readPeopleRDD.collect()));

    //   select id from test.people where id=45;

    JavaRDD<String> rdd4 =
        sparkContextJavaFunctions
            .cassandraTable("test", "people")
            .select("id")
            .where("id = 45")
            .map(CassandraRow::toString);

    System.out.println(
        "Data with only 'id' column fetched: \n" + StringUtils.join("\n", rdd4.collect()));
  }
コード例 #2
0
  /** Exercise 3 - Save a list of people to Cassandra */
  private static void writePeopleToCassandra(JavaSparkContext javaSparkContext) {
    // 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> peopleRDD = javaSparkContext.parallelize(people);

    JavaPairRDD<Integer, Person> integerPersonJavaPairRDD = peopleRDD.keyBy(np -> np.getId());

    RDDJavaFunctions<Person> cassandraPersonRDD = CassandraJavaUtil.javaFunctions(peopleRDD);

    cassandraPersonRDD
        .writerBuilder("test", "people", CassandraJavaUtil.mapToRow(Person.class))
        .saveToCassandra();
  }