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

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

Introduction

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

Prototype

public static long toLong(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 va 2s . com*/
    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()));
}

From source file:com.blockwithme.longdb.cassandra.embedded.CassandraEmbTable.java

License:Apache License

@Override
protected long sizeInternal() {
    long start = 0;
    long lastEnd = 0;
    long count = 0;
    Long startKey = null;//from w  ww  .ja v  a 2s  .c om
    try {
        setks();
        /** Following while loop loads around ROW_SLICE_COUNT rows per
         * iteration using get_range_slices query. The Last key found in the
         * previous iteration becomes 'start key' in the current iteration. */
        while (true) { // $codepro.audit.disable
                       // constantConditionalExpression
            final SliceRange range = constructRange(0, Long.MIN_VALUE, Long.MAX_VALUE, true);
            final KeyRange kRange = getKeyRange(startKey);
            final SlicePredicate predicate = new SlicePredicate();
            predicate.setSlice_range(range);
            final List<KeySlice> res = server().get_range_slices(getColumnParent(), predicate, kRange,
                    DEFAULT_CONSISTENCY_LEVEL);
            final int rowCount = res.size();
            if (rowCount == 0) {
                break;
            } else {
                final KeySlice last = res.get(res.size() - 1);
                start = ByteBufferUtil.toLong(last.key);
                if (startKey != null && start == lastEnd) {
                    break;
                }
                count += rowCount - 1; // Key range is inclusive
                lastEnd = start;
                startKey = start;
            }
        }
        if (count > 0) {
            count += 1;
        }
    } catch (final Exception e) {
        LOG.error("Exception Occurred in - sizeInternal()", e);
        throw new DBException("Error getting table size.", e);
    }
    return count;
}

From source file:com.datastax.demo.portfolio.controller.PortfolioMgrHandler.java

License:Apache License

private List<Portfolio> buildPorfoliosFromRangeSlices(List<KeySlice> kslices) throws Exception {
    List<Portfolio> portfolios = new ArrayList<Portfolio>();
    for (KeySlice ks : kslices) {
        Portfolio p = new Portfolio();
        p.setName(new String(ks.getKey()));

        Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
        List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();

        for (ColumnOrSuperColumn cosc : ks.getColumns()) {
            tickers.add(cosc.getColumn().name);
            tickerLookup.put(cosc.getColumn().name, ByteBufferUtil.toLong(cosc.getColumn().value));
        }/*from   www  .java  2s .c o  m*/

        Map<ByteBuffer, List<ColumnOrSuperColumn>> prices = getClient().multiget_slice(tickers, scp, sp,
                ConsistencyLevel.ONE);

        double total = 0;
        double basis = 0;

        Random r = new Random(Long.valueOf(new String(ks.getKey())));

        for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : prices.entrySet()) {
            if (!entry.getValue().isEmpty()) {
                Double price = Double.valueOf(ByteBufferUtil.string(entry.getValue().get(0).column.value));
                Position s = new Position(ByteBufferUtil.string(entry.getKey()), price,
                        tickerLookup.get(entry.getKey()));
                p.addToConstituents(s);
                total += price * tickerLookup.get(entry.getKey());
                basis += r.nextDouble() * 100 * tickerLookup.get(entry.getKey());

            }
        }

        p.setPrice(total);
        p.setBasis(basis);

        portfolios.add(p);

    }
    return portfolios;
}

From source file:com.datastax.demo.portfolio.controller.PortfolioMgrHandler.java

License:Apache License

private List<Portfolio> buildPorfoliosFromCqlResult(CqlResult result) throws Exception {
    List<Portfolio> portfolios = new ArrayList<Portfolio>();
    for (CqlRow row : result.rows) {
        Portfolio p = new Portfolio();
        p.setName(new String(row.getKey()));

        Map<ByteBuffer, Long> tickerLookup = new HashMap<ByteBuffer, Long>();
        List<ByteBuffer> tickers = new ArrayList<ByteBuffer>();

        for (Column cosc : row.getColumns()) {
            tickers.add(cosc.name);//from www. jav  a  2s .  c o  m
            tickerLookup.put(cosc.name, ByteBufferUtil.toLong(cosc.value));
        }

        double total = 0;
        double basis = 0;

        Random r = new Random(Long.valueOf(new String(row.getKey())));

        for (ByteBuffer ticker : tickers) {
            CqlResult tResult = getClient().execute_cql_query(ByteBufferUtil.bytes(buildStocksQuery(ticker)),
                    Compression.NONE);
            CqlRow tRow = tResult.getRowsIterator().hasNext() ? tResult.getRowsIterator().next() : null;
            if (tRow != null) {
                Double price = Double.valueOf(ByteBufferUtil.string(tRow.columns.get(0).value));
                Position s = new Position(ByteBufferUtil.string(tRow.key), price, tickerLookup.get(tRow.key));
                p.addToConstituents(s);
                total += price * tickerLookup.get(tRow.key);
                basis += r.nextDouble() * 100 * tickerLookup.get(tRow.key);
            }
        }

        p.setPrice(total);
        p.setBasis(basis);

        portfolios.add(p);
    }
    return portfolios;
}

From source file:com.impetus.client.cassandra.CassandraIdGenerator.java

License:Apache License

@Override
public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) {
    Cassandra.Client conn = ((CassandraClientBase) client).getRawClient(discriptor.getSchema());
    long latestCount = 0l;
    try {//w  w  w .j  ava 2s.co  m
        conn.set_keyspace(discriptor.getSchema());

        if (((CassandraClientBase) client).isCql3Enabled()) {
            CQLTranslator translator = new CQLTranslator();
            ((CassandraClientBase) client).execute(translator.buildUpdateQuery(discriptor).toString(), conn);

            CqlResult result = ((CassandraClientBase) client)
                    .execute(translator.buildSelectQuery(discriptor).toString(), conn);

            for (CqlRow row : result.getRows()) {
                latestCount = ByteBufferUtil.toLong(ByteBuffer.wrap(row.getColumns().get(0).getValue()));
            }
        } else {

            ColumnPath columnPath = new ColumnPath(discriptor.getTable());
            columnPath.setColumn(discriptor.getValueColumnName().getBytes());

            try {
                latestCount = conn.get(ByteBuffer.wrap(discriptor.getPkColumnValue().getBytes()), columnPath,
                        ((CassandraClientBase) client).getConsistencyLevel()).counter_column.value;
            } catch (NotFoundException e) {
                log.warn("Counter value not found for {}, resetting it to zero.", discriptor.getPkColumnName());
                latestCount = 0;
            }
            ColumnParent columnParent = new ColumnParent(discriptor.getTable());

            CounterColumn counterColumn = new CounterColumn(
                    ByteBuffer.wrap(discriptor.getValueColumnName().getBytes()), 1);

            conn.add(ByteBuffer.wrap(discriptor.getPkColumnValue().getBytes()), columnParent, counterColumn,
                    ((CassandraClientBase) client).getConsistencyLevel());
        }
        if (latestCount == 0) {
            return (long) discriptor.getInitialValue();
        } else {
            return (latestCount + 1) * discriptor.getAllocationSize();
        }
    } catch (UnavailableException e) {
        log.error("Error while reading counter value from table{}, Caused by: .", discriptor.getTable(), e);
        throw new KunderaException(e);
    } catch (TimedOutException e) {
        log.error("Error while reading counter value from table{}, Caused by: .", discriptor.getTable(), e);
        throw new KunderaException(e);
    } catch (Exception e) {
        log.error("Error while using keyspace. Caused by: .", e);
        throw new KunderaException(e);
    }
}