List of usage examples for org.apache.cassandra.utils ByteBufferUtil string
public static String string(ByteBuffer buffer) throws CharacterCodingException
From source file:ClientOnlyExample.java
License:Apache License
private static void testReading() throws Exception { // do some queries. Collection<ByteBuffer> cols = new ArrayList<ByteBuffer>() { {/* w w w. java 2s . co m*/ add(ByteBufferUtil.bytes("colb")); } }; for (int i = 0; i < 100; i++) { List<ReadCommand> commands = new ArrayList<ReadCommand>(); SliceByNamesReadCommand readCommand = new SliceByNamesReadCommand(KEYSPACE, ByteBuffer.wrap(("key" + i).getBytes()), new QueryPath(COLUMN_FAMILY, null, null), cols); readCommand.setDigestQuery(false); commands.add(readCommand); List<Row> rows = StorageProxy.read(commands, ConsistencyLevel.ONE); assert rows.size() == 1; Row row = rows.get(0); ColumnFamily cf = row.cf; if (cf != null) { for (IColumn col : cf.getSortedColumns()) { System.out .println(ByteBufferUtil.string(col.name()) + ", " + ByteBufferUtil.string(col.value())); } } else System.err.println("This output indicates that nothing was read."); } }
From source file:andromache.debug.MutationStringifier.java
License:Apache License
private static String getStr(List<ByteBuffer> column_names) { StringBuilder sb = new StringBuilder("["); for (ByteBuffer column_name : column_names) { try {// www .j av a 2 s . c o m sb.append(ByteBufferUtil.string(column_name)).append(","); } catch (CharacterCodingException e) { sb.append(column_name.toString()).append(","); } } sb.append("]"); return sb.toString(); }
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 ww. ja va 2s. 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())); }
From source file:com.cloudian.support.RowFinder.java
License:Apache License
private void find() throws IOException { // get ColumnFamilyStore instance System.out.println("Opening keyspace " + ksName + " ..."); Keyspace keyspace = Keyspace.open(ksName); System.out.println("Opened keyspace " + ksName); System.out.println("Getting column family " + cfName + " ..."); ColumnFamilyStore cfStore = keyspace.getColumnFamilyStore(cfName); Collection<SSTableReader> ssTables = cfStore.getSSTables(); System.out.println("Got column family " + cfName); ByteBuffer buff = ByteBufferUtil.bytes(this.rowKey); IPartitioner<?> partitioner = cfStore.partitioner; System.out.println(this.rowKey + " is included in the following files"); System.out.println("=============================================================================="); System.out.println("FINE_NAME, COLUMN_INFO, CONTIGUOUS_TOMBSTONED_COLUMNS(over " + threshold + ")"); System.out.println("=============================================================================="); for (SSTableReader reader : ssTables) { if (reader.getBloomFilter().isPresent(buff)) { // seek to row key RandomAccessReader dfile = reader.openDataReader(); RowIndexEntry entry = reader.getPosition(partitioner.decorateKey(buff), SSTableReader.Operator.EQ); if (entry == null) continue; dfile.seek(entry.position);/*from w ww. j a v a 2s . c o m*/ // read some ByteBufferUtil.readWithShortLength(dfile); if (reader.descriptor.version.hasRowSizeAndColumnCount) dfile.readLong(); DeletionInfo deletionInfo = new DeletionInfo(DeletionTime.serializer.deserialize(dfile)); int columnCount = reader.descriptor.version.hasRowSizeAndColumnCount ? dfile.readInt() : Integer.MAX_VALUE; // get iterator Iterator<OnDiskAtom> atomIterator = reader.metadata.getOnDiskIterator(dfile, columnCount, reader.descriptor.version); // iterate System.out.print(new File(reader.getFilename()).getName()); boolean isContiguous = false; int contiguousTombstonedColumns = 0; String contiguousTombstonedColumnsStart = null; int live = 0; int deleted = 0; int rangeTombstone = 0; StringBuffer sb = new StringBuffer(); while (atomIterator.hasNext()) { OnDiskAtom atom = atomIterator.next(); if (atom instanceof Column) { if (atom instanceof DeletedColumn) { deleted++; if (!isContiguous) { isContiguous = true; contiguousTombstonedColumnsStart = ByteBufferUtil.string(atom.name()); } contiguousTombstonedColumns++; } else { live++; if (isContiguous) { // print if (contiguousTombstonedColumns >= this.threshold) { sb.append(", [" + contiguousTombstonedColumnsStart + "|" + contiguousTombstonedColumns + "]"); } // reset contiguousTombstonedColumns = 0; contiguousTombstonedColumnsStart = null; isContiguous = false; } } } else if (atom instanceof RangeTombstone) { rangeTombstone++; int localDeletionTime = atom.getLocalDeletionTime(); ByteBuffer min = ((RangeTombstone) atom).min; ByteBuffer max = ((RangeTombstone) atom).max; String minString = ByteBufferUtil.string(min); String maxString = ByteBufferUtil.string(max); sb.append(", [" + minString + ", " + maxString + "(" + localDeletionTime + ")]"); } // if it ends with finished columns if (contiguousTombstonedColumns >= this.threshold) { sb.append( ", [" + contiguousTombstonedColumnsStart + "|" + contiguousTombstonedColumns + "]"); } } System.out.print(", (live, deleted, range tombstone)=(" + live + ", " + deleted + ", " + rangeTombstone + ")"); System.out.println(sb.toString()); } } }
From source file:com.cloudian.support.SSTableKeyWriter.java
License:Apache License
private void iterateKeys(int limit) throws IOException { KeyIterator itr = new KeyIterator(this.descriptor); int count = 0; while (itr.hasNext() && count < limit) { DecoratedKey key = itr.next();/*from ww w. ja va 2 s .c om*/ System.out.println(ByteBufferUtil.string(key.key)); count++; } itr.close(); }
From source file:com.cloudian.support.SSTableKeyWriter.java
License:Apache License
private void scanRows(int limit) throws IOException { SSTableIdentityIterator row;//w w w .java 2s. c om SSTableScanner scanner = reader.getScanner(); int count = 0; while (scanner.hasNext() && count < limit) { row = (SSTableIdentityIterator) scanner.next(); String rowKey = ByteBufferUtil.string(row.getKey().key); System.out.println(rowKey + DELIMITER + row.dataSize); count++; } scanner.close(); }
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 w w w . j a v a2 s.c om*/ 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 . j a v a 2 s . c om 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.datastax.demo.portfolio.controller.PortfolioMgrHandler.java
License:Apache License
private void addLossInformation(List<Portfolio> portfolios) { Map<ByteBuffer, Portfolio> portfolioLookup = new HashMap<ByteBuffer, Portfolio>(); List<ByteBuffer> portfolioNames = new ArrayList<ByteBuffer>(); for (Portfolio p : portfolios) { ByteBuffer name = ByteBufferUtil.bytes(p.name); portfolioLookup.put(name, p);//from w w w . j a v a2 s. c o m portfolioNames.add(name); } try { Map<ByteBuffer, List<ColumnOrSuperColumn>> result = getClient().multiget_slice(portfolioNames, lcp, lcols, ConsistencyLevel.ONE); for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : result.entrySet()) { Portfolio portfolio = portfolioLookup.get(entry.getKey()); if (portfolio == null) continue; for (ColumnOrSuperColumn col : entry.getValue()) { if (col.getColumn().name.equals(lossCol)) portfolio.setLargest_10day_loss( Double.valueOf(ByteBufferUtil.string(col.getColumn().value))); if (col.getColumn().name.equals(lossDateCol)) portfolio.setLargest_10day_loss_date(ByteBufferUtil.string(col.getColumn().value)); } } } catch (InvalidRequestException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimedOutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CharacterCodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.datastax.demo.portfolio.controller.PortfolioMgrHandler.java
License:Apache License
private void addHistInformation(List<Portfolio> portfolios) { for (Portfolio p : portfolios) { ByteBuffer name = ByteBufferUtil.bytes(p.name); List<ByteBuffer> tickers = new ArrayList<ByteBuffer>(); for (Position position : p.constituents) { tickers.add(ByteBufferUtil.bytes(position.ticker)); }/*w w w. j a v a2 s .c o m*/ try { Map<ByteBuffer, List<ColumnOrSuperColumn>> result = getClient().multiget_slice(tickers, hcp, hsp, ConsistencyLevel.ONE); Map<String, Double> histPrices = new LinkedHashMap<String, Double>(); for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> entry : result.entrySet()) { for (ColumnOrSuperColumn col : entry.getValue()) { Double price = histPrices.get(ByteBufferUtil.string(col.column.name)); if (price == null) price = 0.0; price = +Double.valueOf(ByteBufferUtil.string(col.column.value)); histPrices.put(ByteBufferUtil.string(col.column.name), price); } } p.setHist_prices(Arrays.asList(histPrices.values().toArray(new Double[] {}))); } catch (InvalidRequestException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimedOutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CharacterCodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }