Class CassandraIO

java.lang.Object
org.apache.beam.sdk.io.cassandra.CassandraIO

public class CassandraIO extends Object
An IO to read and write from/to Apache Cassandra

Reading from Apache Cassandra

CassandraIO provides a source to read and returns a bounded collection of entities as PCollection<Entity>. An entity is built by Cassandra mapper ( com.datastax.driver.mapping.EntityMapper) based on a POJO containing annotations (as described http://docs.datastax .com/en/developer/java-driver/2.1/manual/object_mapper/creating/").

The following example illustrates various options for configuring the IO:


 pipeline.apply(CassandraIO.<Person>read()
     .withHosts(Arrays.asList("host1", "host2"))
     .withPort(9042)
     .withKeyspace("beam")
     .withTable("Person")
     .withEntity(Person.class)
     .withCoder(SerializableCoder.of(Person.class))
     // above options are the minimum set, returns PCollection<Person>

 

Alternatively, one may use CassandraIO.<Person>readAll() .withCoder(SerializableCoder.of(Person.class)) to query a subset of the Cassandra database by creating a PCollection of CassandraIO.Read<Person> each with their own query or RingRange.

Writing to Apache Cassandra

CassandraIO provides a sink to write a collection of entities to Apache Cassandra.

The following example illustrates various options for configuring the IO write:


 pipeline
    .apply(...) // provides a PCollection<Person> where Person is an entity
    .apply(CassandraIO.<Person>write()
        .withHosts(Arrays.asList("host1", "host2"))
        .withPort(9042)
        .withKeyspace("beam")
        .withEntity(Person.class));
 

Cassandra Socket Options

The following example illustrates setting timeouts for the Cassandra client:


 pipeline.apply(CassandraIO.<Person>read()
     .withHosts(Arrays.asList("host1", "host2"))
     .withPort(9042)
     .withConnectTimeout(1000)
     .withReadTimeout(5000)
     .withKeyspace("beam")
     .withTable("Person")
     .withEntity(Person.class)
     .withCoder(SerializableCoder.of(Person.class))