/** * get 讀取1個column * * @throws Exception */ @Test public void get() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // 讀取1個column String COLUMN_FAMILY = "student"; ColumnPath columnPath = new ColumnPath(COLUMN_FAMILY); // String COLUMN = "grad"; columnPath.setColumn(ByteBufferHelper.toByteBuffer(COLUMN)); String ROW_KEY = "Jack"; // key, column_path, consistency_level ColumnOrSuperColumn cos = client.get( ByteBufferHelper.toByteBuffer(ROW_KEY), columnPath, ConsistencyLevel.ONE);// NotFoundException Column column = cos.getColumn(); System.out.println(ROW_KEY + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, grad: 5, 1380932164492000 }
/** * getByCql * * @throws Exception */ @Test public void getByCql() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // String CQL = "select * from student where KEY='Jack'"; // query, compression CqlResult result = client.execute_cql_query( ByteBufferHelper.toByteBuffer(CQL), Compression.NONE); System.out.println(result); for (CqlRow cqlRow : result.getRows()) { for (Column column : cqlRow.getColumns()) { System.out.println(ByteHelper.toString(cqlRow.getKey()) + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, KEY: Jack, -1 // Jack, art: 87, 1380933848350 // Jack, grad: 5, 1380932164492000 // Jack, math: 97, 1380933848305 } } }
private Mutation getMutation(String columnName, Object value, long timestamp) { byte[] columnNameBytes; try { columnNameBytes = columnName.getBytes("UTF-8"); } catch (UnsupportedEncodingException exc) { throw new StorageException("Unsupported character encoding for column name", exc); } byte[] valueBytes = convertValueToBytes(value); Column column = new Column(); column.setName(columnNameBytes); column.setValue(valueBytes); column.setTimestamp(timestamp); ColumnOrSuperColumn columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(columnOrSuperColumn); return mutation; }
private static Mutation createMutation(byte[] colName, byte[] colValue, long timestamp) { if (colValue == null) { colValue = EMPTY_BYTES; } Column col = new Column(); col.setName(colName); col.setValue(colValue); col.setTimestamp(timestamp); ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); cosc.setColumn(col); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(cosc); return mutation; }
public List<String> QueryOneMinute(String min) { List<String> userList = new ArrayList<String>(); try { List<ColumnOrSuperColumn> results = client.get_slice(Utils .toByteBuffer(min), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cc : results) { SuperColumn superColumn = cc.getSuper_column(); List<Column> list = superColumn.getColumns(); for (Column c : list) { String columnName = new String(c.getName(), "UTF-8"); if (columnName.equals("username")) { String value = new String(c.getValue(), "UTF-8"); if (!userList.contains(value)) { userList.add(value); } } } } } catch (Exception e) { System.out.println(e); } return userList; }
public List<String> QueryOneMinute(String min){ List<String> bookList = new ArrayList<String>(); try { List<ColumnOrSuperColumn> results = client.get_slice(Utils .toByteBuffer(min), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cc : results) { SuperColumn superColumn = cc.getSuper_column(); List<Column> list = superColumn.getColumns(); for (Column c : list) { String columnName = new String(c.getName(), "UTF-8"); if (columnName.equals("bookno")) { String value = new String(c.getValue(), "UTF-8"); if (!bookList.contains(value)) { bookList.add(value); } } } } } catch (Exception e) { System.out.println(e); } return bookList; }
/** * Insert into IpUser Column Family */ public boolean InsertTest(String key, int val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer("signal")); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(String.valueOf(val))); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(key), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into IpUser Column Family */ public boolean InsertTest2(int key, String val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer("content")); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(val)); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(key)), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into IpUser Column Family */ public boolean InsertTest3(int userid, int key, String val) { System.out.println("------------InsertTest--------------"); try { ColumnParent parent = new ColumnParent("test"); long timeStamp = System.currentTimeMillis(); Column idColumnPageid = new Column(); idColumnPageid.setName(this.cassandraUtil.toByteBuffer(String.valueOf(key))); idColumnPageid.setValue(this.cassandraUtil.toByteBuffer(val)); idColumnPageid.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(userid)), parent, idColumnPageid, ConsistencyLevel.ONE); } catch (Exception e) { e.printStackTrace(); } return true; }
/** * Insert into CF -- "UserChapter" * @param int userid: the key of column like '119115' * @param int signal: chapter's signal like '23' * @param String content: chapter's value like '"07018720_1.0.0.0.0"' */ public boolean InsertIntoUserChapter(int userid, int signal, String content){ // System.out.println("------------InsertIntoUserChapter--------------"); try { ColumnParent parent = new ColumnParent("UserChapter"); long timeStamp = System.currentTimeMillis(); Column userChapter = new Column(); userChapter.setName(this.cassandraUtil.toByteBuffer(String.valueOf(signal))); userChapter.setValue(this.cassandraUtil.toByteBuffer(content)); userChapter.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(userid)), parent, userChapter, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Insert into CF -- "SignalChapterMap" * @param int signal: chapter's signal like '23' * @param String content: chapter's value like '"07018720_1.0.0.0.0"' */ public boolean InsertIntoSignalChapterMap(int signal, String content){ // System.out.println("------------InsertIntoSignalChapterMap--------------"); try { ColumnParent parent = new ColumnParent("SignalChapterMap"); long timeStamp = System.currentTimeMillis(); Column signalChapter = new Column(); signalChapter.setName(this.cassandraUtil.toByteBuffer("content")); signalChapter.setValue(this.cassandraUtil.toByteBuffer(content)); signalChapter.setTimestamp(timeStamp); client.insert(this.cassandraUtil.toByteBuffer(String.valueOf(signal)), parent, signalChapter, ConsistencyLevel.QUORUM); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * Update 'maxid' in CF 'ChapterSignalMap' */ public boolean UpdateMaxid(String signal) { try{ ColumnParent parent = new ColumnParent("ChapterSignalMap"); long timeStamp = System.currentTimeMillis(); Column signalCol = new Column(); signalCol.setName(this.cassandraUtil.toByteBuffer("maxid")); signalCol.setValue(this.cassandraUtil.toByteBuffer(signal)); signalCol.setTimestamp(timeStamp); this.client.insert(this.cassandraUtil.toByteBuffer("maxid"), parent, signalCol, ConsistencyLevel.QUORUM); }catch(Exception e) { e.printStackTrace(); } return true; }
public void open() throws Exception { TSocket sock = new TSocket(Config.getCassandraIP(), Config.getCassandraPort()); sock.setTimeout(1000000); transport = new TFramedTransport(sock); client = new Cassandra.Client(new TBinaryProtocol(transport)); transport.open(); client.set_keyspace(Config.getCassandraKeyspace()); cl = ConsistencyLevel.valueOf(Config.getCassandraConsistencyLevel()); writeColumn = new Column(); writeColumnFamily = new ColumnParent(Config.getCassandraColumnFamily()); columnPath = new ColumnPath(Config.getCassandraColumnFamily()); bytesRead = bytesWritten = readLatency = writeLatency = 0; }
private static Column thriftify(org.apache.cassandra.db.Cell c) { ByteBuffer value = (c instanceof CounterCell) ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value())) : c.value(); return new Column(c.name().toByteBuffer()).setValue(value).setTimestamp(c.timestamp()); }
public void run(final ThriftClient client) throws IOException { final ByteBuffer key = getKey(); final List<Column> columns = getColumns(); List<Mutation> mutations = new ArrayList<>(columns.size()); for (Column c : columns) { ColumnOrSuperColumn column = new ColumnOrSuperColumn().setColumn(c); mutations.add(new Mutation().setColumn_or_supercolumn(column)); } Map<String, List<Mutation>> row = Collections.singletonMap(type.table, mutations); final Map<ByteBuffer, Map<String, List<Mutation>>> record = Collections.singletonMap(key, row); timeWithRetry(new RunOp() { @Override public boolean run() throws Exception { client.batch_mutate(record, settings.command.consistencyLevel); return true; } @Override public int partitionCount() { return 1; } @Override public int rowCount() { return 1; } }); }
protected List<Column> getColumns() { final ColumnSelection selection = select(); final List<ByteBuffer> values = getColumnValues(selection); final List<Column> columns = new ArrayList<>(values.size()); final List<ByteBuffer> names = select().select(settings.columns.names); for (int i = 0 ; i < values.size() ; i++) columns.add(new Column(names.get(i)) .setValue(values.get(i)) .setTimestamp(settings.columns.timestamp != null ? Long.parseLong(settings.columns.timestamp) : FBUtilities.timestampMicros())); return columns; }
private static void validateColumnNames(Iterable<ByteBuffer> columns) throws InvalidRequestException { for (ByteBuffer name : columns) { if (name.remaining() > org.apache.cassandra.db.Column.MAX_NAME_LENGTH) throw new InvalidRequestException(String.format("column name is too long (%s > %s)", name.remaining(), org.apache.cassandra.db.Column.MAX_NAME_LENGTH)); if (name.remaining() == 0) throw new InvalidRequestException("zero-length column name"); } }
private static Column thriftify(org.apache.cassandra.db.Column c) { ByteBuffer value = (c instanceof CounterColumn) ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value())) : c.value(); return new Column(c.name()).setValue(value).setTimestamp(c.timestamp()); }
private void printRows(CqlResult result) { for (CqlRow row : result.getRows()) { getLog().info("Row key: " + keyValidatorVal.getString(row.key)); getLog().info("-----------------------------------------------"); for (Column column : row.getColumns()) { getLog().info(" name: " + comparatorVal.getString(column.name)); getLog().info(" value: " + defaultValidatorVal.getString(column.value)); getLog().info("-----------------------------------------------"); } } }
/** * get 讀取所有column * * @throws Exception */ @Test public void get2() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); // 讀取所有column String COLUMN_FAMILY = "student"; ColumnParent columnParent = new ColumnParent(COLUMN_FAMILY); // 術語 SlicePredicate predicate = new SlicePredicate(); // 範圍 SliceRange sliceRange = new SliceRange(); // sliceRange.setStart(ByteBufferHelper.toByteBuffer(new byte[0]));//開始 sliceRange.setStart(new byte[0]);// 開始 sliceRange.setFinish(new byte[0]);// 結束 sliceRange.setCount(100);// 筆數 // predicate.setSlice_range(sliceRange); String ROW_KEY = "Jack"; // 結果 // key, column_parent, predicate, consistency_level List<ColumnOrSuperColumn> results = client.get_slice( ByteBufferHelper.toByteBuffer(ROW_KEY), columnParent, predicate, ConsistencyLevel.ONE); for (ColumnOrSuperColumn cos : results) { Column column = cos.getColumn(); System.out.println(ROW_KEY + ", " + ByteHelper.toString(column.getName()) + ": " + ByteHelper.toString(column.getValue()) + ", " + column.getTimestamp()); // Jack, art, 87, 1380788003220 // Jack, grad, 5, 1380788003203 // Jack, math, 97, 1380788003214 } }
/** * update * * @throws Exception */ @Test public void update() throws Exception { String KEYSPACE = "mock"; client.set_keyspace(KEYSPACE); List<Mutation> mutations = new LinkedList<Mutation>(); // <columnFamily,mutations> Map<String, List<Mutation>> columnfamilyMutaions = new HashMap<String, List<Mutation>>();// keyMutations // <rowKey,keyMutations> Map<ByteBuffer, Map<String, List<Mutation>>> rowKeyMutations = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); long timestamp = System.nanoTime(); // Column column = new Column(); column.setName(ByteBufferHelper.toByteBuffer("grad")); column.setValue(ByteBufferHelper.toByteBuffer("9")); column.setTimestamp(timestamp); // ColumnOrSuperColumn cos = new ColumnOrSuperColumn(); cos.setColumn(column); // Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(cos); mutations.add(mutation); String COLUMN_FAMILY = "student"; columnfamilyMutaions.put(COLUMN_FAMILY, mutations); String ROW_KEY = "Jack"; rowKeyMutations.put(ByteBufferHelper.toByteBuffer(ROW_KEY), columnfamilyMutaions); // mutation_map, consistency_level client.batch_mutate(rowKeyMutations, ConsistencyLevel.ONE); }
public void queryDB(String rowKey) { Selector selector = Pelops.createSelector(_pool); List<Column> columns = selector.getColumnsFromRow(_colFamily, rowKey, false, ConsistencyLevel.ONE); LOG.info("Entity: " + Selector.getColumnStringValue(columns, "entity")); LOG.info("Category: " + Selector.getColumnStringValue(columns, "category")); }
/** * Converts a cassandra row to a Kettle row * * @param metaData meta data on the cassandra column family being read from * @param cassandraRow a row from the column family * @param outputFormatMap a Map of output field names to indexes in the * outgoing Kettle row structure * @return a Kettle row * @throws KettleException if a problem occurs */ public Object[] cassandraRowToKettle(CassandraColumnMetaData metaData, CqlRow cassandraRow, Map<String, Integer> outputFormatMap) throws KettleException { Object[] outputRowData = RowDataUtil .allocateRowData(m_outputRowMeta.size()); Object key = metaData.getKeyValue(cassandraRow); if (key == null) { throw new KettleException("Unable to obtain a key value for the row!"); } String keyName = metaData.getKeyName(); int keyIndex = m_outputRowMeta.indexOfValue(keyName); if (keyIndex < 0) { throw new KettleException("Unable to find the key field name '" + keyName + "' in the output row meta data!"); } outputRowData[keyIndex] = key; // do the columns List<Column> rowColumns = cassandraRow.getColumns(); for (Column aCol : rowColumns) { String colName = metaData.getColumnName(aCol); Integer outputIndex = outputFormatMap.get(colName); if (outputIndex != null) { Object colValue = metaData.getColumnValue(aCol); outputRowData[outputIndex.intValue()] = colValue; } } return outputRowData; }
private void insertPOISpringTraining() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long timestamp = System.nanoTime(); String keyName = "Spring Training"; Column descCol = new Column(bytes("desc")); Column phoneCol = new Column(bytes("phone")); List<Column> cols = new ArrayList<Column>(); cols.add(descCol); cols.add(phoneCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn sc = new SuperColumn(); sc.name = bytes(CAMBRIA_NAME); sc.columns = cols; descCosc.super_column = sc; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(CAMBRIA_NAME.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(keyName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Spring Training."); }
private void insertPOICentralPark() throws Exception { Map<ByteBuffer, Map<String, List<Mutation>>> outerMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); List<Mutation> columnsToAdd = new ArrayList<Mutation>(); long ts = System.nanoTime(); String keyName = "Central Park"; Column descCol = new Column(bytes("desc")); // no phone column for park List<Column> cols = new ArrayList<Column>(); cols.add(descCol); Map<String, List<Mutation>> innerMap = new HashMap<String, List<Mutation>>(); Mutation columns = new Mutation(); ColumnOrSuperColumn descCosc = new ColumnOrSuperColumn(); SuperColumn waldorfSC = new SuperColumn(); waldorfSC.name = bytes(WALDORF_NAME); waldorfSC.columns = cols; descCosc.super_column = waldorfSC; columns.setColumn_or_supercolumn(descCosc); columnsToAdd.add(columns); String superCFName = "PointOfInterest"; ColumnPath cp = new ColumnPath(); cp.column_family = superCFName; cp.setSuper_column(WALDORF_NAME.getBytes()); cp.setSuper_columnIsSet(true); innerMap.put(superCFName, columnsToAdd); outerMap.put(bytes(keyName), innerMap); client.batch_mutate(outerMap, CL); LOG.debug("Done inserting Central Park."); }
private void insertColumnValue(String parentName, String id, String name, String value, long ts) throws TException, TimedOutException, UnavailableException, InvalidRequestException, UnsupportedEncodingException { ColumnParent parent = new ColumnParent(parentName); Column column = new Column(toByteBuffer(name)); column.setValue(toByteBuffer(value)); column.setTimestamp(ts); getClient().insert(toByteBuffer(id), parent, column, ConsistencyLevel.ONE); }
public CqlResult toThriftResult() { String UTF8 = "UTF8Type"; CqlMetadata schema = new CqlMetadata(new HashMap<ByteBuffer, String>(), new HashMap<ByteBuffer, String>(), // The 2 following ones shouldn't be needed in CQL3 UTF8, UTF8); for (ColumnSpecification name : metadata.names) { ByteBuffer colName = ByteBufferUtil.bytes(name.toString()); schema.name_types.put(colName, UTF8); AbstractType<?> normalizedType = name.type instanceof ReversedType ? ((ReversedType)name.type).baseType : name.type; schema.value_types.put(colName, normalizedType.toString()); } List<CqlRow> cqlRows = new ArrayList<CqlRow>(rows.size()); for (List<ByteBuffer> row : rows) { List<Column> thriftCols = new ArrayList<Column>(metadata.names.size()); for (int i = 0; i < metadata.names.size(); i++) { Column col = new Column(ByteBufferUtil.bytes(metadata.names.get(i).toString())); col.setValue(row.get(i)); thriftCols.add(col); } // The key of CqlRow shoudn't be needed in CQL3 cqlRows.add(new CqlRow(ByteBufferUtil.EMPTY_BYTE_BUFFER, thriftCols)); } CqlResult res = new CqlResult(CqlResultType.ROWS); res.setRows(cqlRows).setSchema(schema); return res; }
private static Column thriftify(IColumn c) { ByteBuffer value = (c instanceof CounterColumn) ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value())) : c.value(); return new Column(c.name()).setValue(value).setTimestamp(c.timestamp()); }
@Test public void test64kColumn() { Column column = new Column(); column.name = ByteBufferUtil.bytes("test"); // a byte buffer more than 64k ByteBuffer buffer = ByteBuffer.allocate(1024 * 65); buffer.clear(); //read more than 64k for (int i=0; i<1024*64/4 + 1; i++) buffer.putInt(0); // for read buffer.flip(); column.value = buffer; MockRowIndex mockRowIndex = new MockRowIndex(); MockColumnIndex mockColumnIndex = new MockColumnIndex(); assertTrue(mockRowIndex.validate(column)); assertFalse(mockColumnIndex.validate(column)); // test less than 64k value buffer.flip(); buffer.clear(); buffer.putInt(20); buffer.flip(); assertTrue(mockRowIndex.validate(column)); assertTrue(mockColumnIndex.validate(column)); }
@Test public void test64kColumn() { Column column = new Column(); column.name = ByteBufferUtil.bytes("test"); // a byte buffer more than 64k ByteBuffer buffer = ByteBuffer.allocate(1024 * 65); buffer.clear(); //read more than 64k for (int i=0; i<1024*64/4 + 1; i++) buffer.putInt(0); // for read buffer.flip(); column.value = buffer; SecondaryIndexColumnSizeTest.MockRowIndex mockRowIndex = new SecondaryIndexColumnSizeTest.MockRowIndex(); SecondaryIndexColumnSizeTest.MockColumnIndex mockColumnIndex = new SecondaryIndexColumnSizeTest.MockColumnIndex(); assertTrue(mockRowIndex.validate(column)); assertFalse(mockColumnIndex.validate(column)); // test less than 64k value buffer.flip(); buffer.clear(); buffer.putInt(20); buffer.flip(); assertTrue(mockRowIndex.validate(column)); assertTrue(mockColumnIndex.validate(column)); }