Example usage for org.apache.cassandra.utils ByteBufferUtil toDouble

List of usage examples for org.apache.cassandra.utils ByteBufferUtil toDouble

Introduction

In this page you can find the example usage for org.apache.cassandra.utils ByteBufferUtil toDouble.

Prototype

public static double toDouble(ByteBuffer bytes) 

Source Link

Usage

From source file:co.cask.hydrator.plugin.test.ETLCassandraTest.java

License:Apache License

private void testCassandraRealtimeSink() throws Exception {
    Schema schema = Schema.recordOf("user", Schema.Field.of("name", Schema.of(Schema.Type.STRING)),
            Schema.Field.of("graduated", Schema.of(Schema.Type.BOOLEAN)),
            Schema.Field.of("id", Schema.of(Schema.Type.INT)),
            Schema.Field.of("score", Schema.of(Schema.Type.DOUBLE)),
            Schema.Field.of("time", Schema.of(Schema.Type.LONG)));
    List<StructuredRecord> input = ImmutableList.of(StructuredRecord.builder(schema).set("id", 1)
            .set("name", "Bob").set("score", 3.4).set("graduated", false).set("time", 1234567890000L).build());

    ETLStage source = new ETLStage("source", co.cask.cdap.etl.mock.realtime.MockSource.getPlugin(input));
    ETLStage sink = new ETLStage("Cassandra", new ETLPlugin("Cassandra", RealtimeSink.PLUGIN_TYPE,
            new ImmutableMap.Builder<String, String>().put(Constants.Reference.REFERENCE_NAME, "TestCass")
                    .put(RealtimeCassandraSink.Cassandra.ADDRESSES, "localhost:9042")
                    .put(RealtimeCassandraSink.Cassandra.KEYSPACE, "testkeyspace")
                    .put(RealtimeCassandraSink.Cassandra.COLUMN_FAMILY, "testtablerealtime")
                    .put(RealtimeCassandraSink.Cassandra.COLUMNS, "name, graduated, id, score, time")
                    .put(RealtimeCassandraSink.Cassandra.COMPRESSION, "NONE")
                    .put(RealtimeCassandraSink.Cassandra.CONSISTENCY_LEVEL, "QUORUM").build(),
            null));/*from  w w  w.j a v  a2 s.  co m*/
    final String cqlQuery = "select name,graduated,id,score,time from testtablerealtime";
    ETLRealtimeConfig etlConfig = ETLRealtimeConfig.builder().addStage(source).addStage(sink)
            .addConnection(source.getName(), sink.getName()).build();
    Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "testESSink");
    AppRequest<ETLRealtimeConfig> appRequest = new AppRequest<>(REALTIME_APP_ARTIFACT, etlConfig);
    ApplicationManager appManager = deployApplication(appId, appRequest);

    WorkerManager workerManager = appManager.getWorkerManager(ETLWorker.class.getSimpleName());

    workerManager.start();
    Tasks.waitFor(true, new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            CqlResult result = client.execute_cql3_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE,
                    ConsistencyLevel.ALL);
            return result.rows.size() > 0;
        }
    }, 30, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS);
    workerManager.stop();

    CqlResult result = client.execute_cql3_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE,
            ConsistencyLevel.ALL);
    List<Column> columns = result.getRows().get(0).getColumns();

    Assert.assertEquals("Bob", ByteBufferUtil.string(columns.get(0).bufferForValue()));
    byte[] bytes = new byte[] { 0 };
    Assert.assertEquals(ByteBuffer.wrap(bytes), columns.get(1).bufferForValue());
    Assert.assertEquals(1, ByteBufferUtil.toInt(columns.get(2).bufferForValue()));
    Assert.assertEquals(3.4, ByteBufferUtil.toDouble(columns.get(3).bufferForValue()), 0.000001);
    Assert.assertEquals(1234567890000L, ByteBufferUtil.toLong(columns.get(4).bufferForValue()));
}