Search in sources :

Example 1 with CassandraPojoInputFormat

use of org.apache.flink.batch.connectors.cassandra.CassandraPojoInputFormat in project flink by apache.

the class BatchPojoExample method main.

public static void main(String[] args) throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(1);
    List<Pojo> customCassandraAnnotatedPojos = IntStream.range(0, 20).mapToObj(x -> new Pojo(UUID.randomUUID().toString(), x, 0)).collect(Collectors.toList());
    DataSet<Pojo> dataSet = env.fromCollection(customCassandraAnnotatedPojos);
    ClusterBuilder clusterBuilder = new ClusterBuilder() {

        private static final long serialVersionUID = -1754532803757154795L;

        @Override
        protected Cluster buildCluster(Cluster.Builder builder) {
            return builder.addContactPoints("127.0.0.1").build();
        }
    };
    dataSet.output(new CassandraPojoOutputFormat<>(clusterBuilder, Pojo.class, () -> new Mapper.Option[] { Mapper.Option.saveNullFields(true) }));
    env.execute("Write");
    /*
         *	This is for the purpose of showing an example of creating a DataSet using CassandraPojoInputFormat.
         */
    DataSet<Pojo> inputDS = env.createInput(new CassandraPojoInputFormat<>(SELECT_QUERY, clusterBuilder, Pojo.class, () -> new Mapper.Option[] { Mapper.Option.consistencyLevel(ConsistencyLevel.ANY) }));
    inputDS.print();
}
Also used : ClusterBuilder(org.apache.flink.streaming.connectors.cassandra.ClusterBuilder) IntStream(java.util.stream.IntStream) DataSet(org.apache.flink.api.java.DataSet) List(java.util.List) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) CassandraPojoOutputFormat(org.apache.flink.batch.connectors.cassandra.CassandraPojoOutputFormat) Cluster(com.datastax.driver.core.Cluster) Mapper(com.datastax.driver.mapping.Mapper) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CassandraPojoInputFormat(org.apache.flink.batch.connectors.cassandra.CassandraPojoInputFormat) ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) ClusterBuilder(org.apache.flink.streaming.connectors.cassandra.ClusterBuilder) ClusterBuilder(org.apache.flink.streaming.connectors.cassandra.ClusterBuilder)

Example 2 with CassandraPojoInputFormat

use of org.apache.flink.batch.connectors.cassandra.CassandraPojoInputFormat in project flink by apache.

the class CassandraConnectorITCase method readPojosWithInputFormat.

private <T> List<T> readPojosWithInputFormat(Class<T> annotatedPojoClass) {
    final CassandraPojoInputFormat<T> source = new CassandraPojoInputFormat<>(injectTableName(SELECT_DATA_QUERY), builderForReading, annotatedPojoClass);
    List<T> result = new ArrayList<>();
    try {
        source.configure(new Configuration());
        source.open(null);
        while (!source.reachedEnd()) {
            T temp = source.nextRecord(null);
            result.add(temp);
        }
    } finally {
        source.close();
    }
    return result;
}
Also used : CassandraPojoInputFormat(org.apache.flink.batch.connectors.cassandra.CassandraPojoInputFormat) Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList)

Aggregations

CassandraPojoInputFormat (org.apache.flink.batch.connectors.cassandra.CassandraPojoInputFormat)2 Cluster (com.datastax.driver.core.Cluster)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 Mapper (com.datastax.driver.mapping.Mapper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 DataSet (org.apache.flink.api.java.DataSet)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 CassandraPojoOutputFormat (org.apache.flink.batch.connectors.cassandra.CassandraPojoOutputFormat)1 Configuration (org.apache.flink.configuration.Configuration)1 ClusterBuilder (org.apache.flink.streaming.connectors.cassandra.ClusterBuilder)1