public List<LogEntry> getPending() throws Throwable { List<LogEntry> result = new ArrayList<LogEntry>(); SlicePredicate predicate = new SlicePredicate(); SliceRange range = new SliceRange( ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_NUMBER_COLUMNS); predicate.setSlice_range(range); KeyRange keyRange = new KeyRange(BATCH_SIZE); keyRange.setStart_key(ByteBufferUtil.bytes("")); keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); ColumnParent parent = new ColumnParent(COLUMN_FAMILY); IndexClause indexClause = new IndexClause(); indexClause.setCount(BATCH_SIZE); indexClause.setStart_key(new byte[0]); indexClause.addToExpressions( new IndexExpression( ByteBufferUtil.bytes(LogEntryColumns.STATUS.toString()), IndexOperator.EQ, ByteBufferUtil.bytes(LogEntryStatus.COMMITTED.toString()))); indexClause.addToExpressions( new IndexExpression( ByteBufferUtil.bytes(LogEntryColumns.HOST.toString()), IndexOperator.EQ, ByteBufferUtil.bytes(this.getHostName()))); List<KeySlice> rows = getConnection(KEYSPACE) .get_indexed_slices(parent, indexClause, predicate, ConsistencyLevel.ALL); result.addAll(toLogEntry(rows)); indexClause = new IndexClause(); indexClause.setCount(BATCH_SIZE); indexClause.setStart_key(new byte[0]); indexClause.addToExpressions( new IndexExpression( ByteBufferUtil.bytes(LogEntryColumns.STATUS.toString()), IndexOperator.EQ, ByteBufferUtil.bytes(LogEntryStatus.COMMITTED.toString()))); indexClause.addToExpressions( new IndexExpression( ByteBufferUtil.bytes((LogEntryColumns.TIMESTAMP.toString())), IndexOperator.LT, ByteBufferUtil.bytes( System.currentTimeMillis() - (1000L * TIME_BEFORE_PROCESS_OTHER_HOST)))); rows = getConnection(KEYSPACE) .get_indexed_slices(parent, indexClause, predicate, ConsistencyLevel.ALL); result.addAll(toLogEntry(rows)); return result; }
@Test public void testGetRangeSlices() throws HectorException { for (int i = 0; i < 10; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetRangeSlices_" + i)); keyspace.insert( "testGetRangeSlices0", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert( "testGetRangeSlices1", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert( "testGetRangeSlices2", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); KeyRange range = new KeyRange(); range.setStart_key("".getBytes()); range.setEnd_key("".getBytes()); Map<String, List<Column>> keySlices = se.fromBytesMap(keyspace.getRangeSlices(clp, sp, range)); assertNotNull(keySlices); assertNotNull("testGetRangeSlices1 is null", keySlices.get("testGetRangeSlices1")); assertEquals( "testGetRangeSlices_Value_0", string(keySlices.get("testGetRangeSlices1").get(0).getValue())); assertEquals(10, keySlices.get("testGetRangeSlices1").size()); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetRanageSlices0", cp); keyspace.remove("testGetRanageSlices1", cp); keyspace.remove("testGetRanageSlices2", cp); }
@PooledConnection public JSONArray getRows( String keyspace, String columnFamily, String queryStr, ConsistencyLevel consistencyLevel) throws InvalidRequestException, UnavailableException, TimedOutException, TException, CharacterCodingException { if (StringUtils.isNotBlank(queryStr)) { return getRowsWithQuery(keyspace, columnFamily, queryStr, consistencyLevel); } SlicePredicate predicate = new SlicePredicate(); SliceRange range = new SliceRange(ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_COLUMNS); predicate.setSlice_range(range); KeyRange keyRange = new KeyRange(MAX_ROWS); keyRange.setStart_key(ByteBufferUtil.bytes("")); keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); ColumnParent parent = new ColumnParent(columnFamily); List<KeySlice> rows = getConnection(keyspace).get_range_slices(parent, predicate, keyRange, consistencyLevel); return JsonMarshaller.marshallRows(rows, true); }
public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException { TTransport tr = new TSocket(“192.168.1.204″, 9160); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); String keyspace = “Historical_Info”; String columnFamily = “Historical_Info_Column”; //String keyUserID = “3”; // read entire row SlicePredicate predicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(); sliceRange.setStart(new byte[0]); sliceRange.setFinish(new byte[0]); predicate.setSlice_range(sliceRange); KeyRange keyrRange = new KeyRange(); keyrRange.setStart_key(“1″); keyrRange.setEnd_key(“”); //keyrRange.setCount(100); ColumnParent parent = new ColumnParent(columnFamily); List < KeySlice > ls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE); for (KeySlice result: ls) { List < ColumnOrSuperColumn > column = result.columns; for (ColumnOrSuperColumn result2: column) { Column column2 = result2.column; System.out.println(new String(column2.name, UTF8) + ” - > ” + new String(column2.value, UTF8)); } } tr.close(); }