public boolean hasOutstandingTransactions( UUID queueId, UUID consumerId ) { SliceQuery<ByteBuffer, UUID, UUID> q = createSliceQuery( ko, be, ue, ue ); q.setColumnFamily( CONSUMER_QUEUE_TIMEOUTS.getColumnFamily() ); q.setKey( getQueueClientTransactionKey( queueId, consumerId ) ); q.setRange( null, null, false, 1 ); return q.execute().get().getColumns().size() > 0; }
/** * Get the position in the queue for the given appId, consumer and queu * * @param queueId The queueId * @param consumerId The consumerId */ public UUID getConsumerQueuePosition( UUID queueId, UUID consumerId ) { HColumn<UUID, UUID> result = HFactory.createColumnQuery( ko, ue, ue, ue ).setKey( consumerId ).setName( queueId ) .setColumnFamily( CONSUMERS.getColumnFamily() ).execute().get(); if ( result != null ) { return result.getValue(); } return null; }
@Test public void testRowDeletion() { String cf = "Standard1"; long initialTime = keyspace.createClock(); Mutator<String> m = createMutator(keyspace, se); for (int i = 0; i < 5; i++) { m.addInsertion("key" + i, cf, createColumn("name", "value" + i, se, se)); } m.execute(); // Try to delete the row with key "k0" with a clock previous to the insertion. // Cassandra will discard this operation. m.addDeletion("key0", cf, null, se, (initialTime - 100)); m.execute(); // Check that the delete was harmless QueryResult<HColumn<String, String>> columnResult = createColumnQuery(keyspace, se, se, se).setColumnFamily(cf).setKey("key0"). setName("name").execute(); assertEquals("value0", columnResult.get().getValue()); for (int i = 0; i < 5; i++) { m.addDeletion("key" + i, cf, null, se); } m.execute(); // Check that the delete took place now columnResult = createColumnQuery(keyspace, se, se, se). setColumnFamily(cf).setKey("key0").setName("name").execute(); assertNull(columnResult.get()); }
/** * Get the bounds for the queue * * @return The bounds for the queue */ public QueueBounds getQueueBounds( UUID queueId ) { try { ColumnSlice<String, UUID> result = HFactory.createSliceQuery( ko, ue, se, ue ).setKey( queueId ) .setColumnNames( QUEUE_NEWEST, QUEUE_OLDEST ) .setColumnFamily( QUEUE_PROPERTIES.getColumnFamily() ).execute() .get(); if ( result != null && result.getColumnByName( QUEUE_OLDEST ) != null && result.getColumnByName( QUEUE_NEWEST ) != null ) { return new QueueBounds( result.getColumnByName( QUEUE_OLDEST ).getValue(), result.getColumnByName( QUEUE_NEWEST ).getValue() ); } } catch ( Exception e ) { logger.error( "Error getting oldest queue message ID", e ); } return null; }
@Test public void testTruncateColumnFamily() throws Exception { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "TruncateableCf"); cassandraCluster.addColumnFamily(cfDef); Keyspace workingKeyspace = HFactory.createKeyspace("Keyspace1", cassandraCluster); Mutator<String> mutator = HFactory.createMutator(workingKeyspace, StringSerializer.get()); mutator.insert("mykey", "TruncateableCf", HFactory.createStringColumn("mycolname", "myval")); ColumnQuery<String,String,String> q = HFactory.createColumnQuery(workingKeyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get()); q.setKey("mykey").setName("mycolname").setColumnFamily("TruncateableCf"); assertEquals("myval",q.execute().get().getValue()); cassandraCluster.truncate("Keyspace1", "TruncateableCf"); assertNull(q.execute().get()); }
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); try { Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); mutator.insert("jsmith", "Standard1", HFactory.createStringColumn("first", "John")); ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keyspaceOperator); columnQuery.setColumnFamily("Standard1").setKey("jsmith").setName("first"); QueryResult<HColumn<String, String>> result = columnQuery.execute(); System.out.println("Read HColumn from cassandra: " + result.get()); System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] "); } catch (HectorException e) { e.printStackTrace(); } cluster.getConnectionManager().shutdown(); }
SliceQuery<ByteBuffer, N, V> q = createSliceQuery( ko, be, nameSerializer, valueSerializer ); q.setColumnFamily( columnFamily.toString() ); q.setKey( bytebuffer( key ) ); q.setColumnNames( ( N[] ) nameSerializer.fromBytesSet( se.toBytesSet( new ArrayList<String>( columnNames ) ) ) .toArray() ); QueryResult<ColumnSlice<N, V>> r = q.execute(); ColumnSlice<N, V> slice = r.get(); List<HColumn<N, V>> results = slice.getColumns();
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); try { Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keyspaceOperator); columnQuery.setColumnFamily("Standard1").setKey("fake_key_0").setName("fake_column_0"); QueryResult<HColumn<String, String>> colResult = columnQuery.execute(); System.out.println("Execution time: " + colResult.getExecutionTimeMicro()); System.out.println("CassandraHost used: " + colResult.getHostUsed()); System.out.println("Query Execute: " + colResult.getQuery());
public <N, V> ColumnSlice<N, V> getColumns( Keyspace ko, Object columnFamily, Object key, N[] columns, Serializer<N> nameSerializer, Serializer<V> valueSerializer ) throws Exception { if ( db_logger.isTraceEnabled() ) { db_logger.trace( "getColumn cf={} key={} column={}", columnFamily, key, columns ); } /* * ByteBuffer column_bytes = null; if (column instanceof List) { * column_bytes = Composite.serializeToByteBuffer((List<?>) column); } else * { column_bytes = bytebuffer(column); } */ SliceQuery<ByteBuffer, N, V> q = HFactory.createSliceQuery( ko, be, nameSerializer, valueSerializer ); QueryResult<ColumnSlice<N, V>> r = q.setKey( bytebuffer( key ) ).setColumnNames( columns ).setColumnFamily( columnFamily.toString() ) .execute(); ColumnSlice<N, V> result = r.get(); if ( db_logger.isTraceEnabled() ) { if ( result == null ) { db_logger.trace( "getColumn returned null" ); } } return result; }
SliceQuery<byte[], String, byte[]> q = HFactory.createSliceQuery(keyspace, BytesArraySerializer.get(), StringSerializer.get(), BytesArraySerializer.get()); q.setColumnFamily(colFamName); q.setKey(colFamKey); q.setColumnNames(cfMapDef.getSliceColumnNameArr()); } else { q.setRange("", "", false, maxNumColumns); QueryResult<ColumnSlice<String, byte[]>> result = q.execute(); if (null == result || null == result.get()) { return null; T obj = createObject(cfMapDef, pkObj, result.get()); return obj;
@Override public Void call() throws Exception { log.debug("Starting VerifyLastInsertCommand"); String key = "test"; sliceQuery.setColumnFamily(commandArgs.workingColumnFamily); log.info("StartKey: {} for thread {}", key, Thread.currentThread().getId()); String colValue; for (int col = 0; col < commandArgs.columnCount; col++) { colValue = String.format(COLUMN_VAL_FORMAT, col); mutator.addInsertion(key, commandArgs.workingColumnFamily, HFactory.createStringColumn(String.format(COLUMN_NAME_FORMAT, col), colValue)); executeMutator(col); // Let's verify sliceQuery.setKey(key); sliceQuery.setRange(null, null, true, 1); QueryResult<ColumnSlice<String,String>> result = sliceQuery.execute(); String actualValue = result.get().getColumns().get(0).getValue(); if (!actualValue.equals(colValue)) { log.error("Column values don't match. Expected: " + colValue + " - Actual: " + actualValue); break; } } commandRunner.doneSignal.countDown(); log.debug("VerifyLastInsertCommand complete"); return null; }
private void refresh() { query.setRange(start, finish.function(), reversed, count); columns = 0; List<HColumn<N, V>> list = query.execute().get().getColumns(); iterator = Iterators.peekingIterator(list.iterator()); if (iterator.hasNext()) { // The lower bound column may have been removed prior to the query executing, // so check to see if the first column returned by the current query is the same // as the lower bound column. If both columns are the same, skip the column N first = list.get(0).getName(); if (first.equals(start)) { next(); } } }
private void assertColumnExists(String keyspace, String cf, String key, String column) { ColumnPath cp = new ColumnPath(cf); cp.setColumn(StringUtils.bytes(column)); Keyspace ks = HFactory.createKeyspace(keyspace, cluster); ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(ks); assertNotNull(String.format("Should have value for %s.%s[%s][%s]", keyspace, cf, key, column), columnQuery.setColumnFamily(cf).setKey(key).setName(column).execute().get().getValue()); //client.getKeyspace(keyspace).getColumn(key, cp)); //cluster.releaseClient(client); }
HFactory.createRangeSlicesQuery( keyspace, // StringSerializer.get(), // StringSerializer.get(), // StringSerializer.get() ) // for ( Row<String, String, String> row : cResult.get().getList() )
private Map<String, String> getResults(QueryResult<ColumnSlice<String, String>> queryResult) { Map<String, String> result = Maps.newHashMap(); for (HColumn<String, String> col : queryResult.get().getColumns()) { result.put(col.getName(), col.getValue()); } return result; }
.createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get() ) // if ( result.get().getCount() < 1 ) MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) // .addInsertion( repositoryId, cf, result.get().getList().get( 0 ).getColumnSlice().getColumnByName( REPOSITORY_NAME.toString() ).getValue() );
RangeSlicesQuery<String, String, String> q = createRangeSlicesQuery(ko, se, se, se); q.setColumnFamily(cf); OrderedRows<String, String, String> rows = r.get(); assertNotNull(rows); .getColumnByName("testRangeSlicesQueryColumn1").getValue()); assertEquals("value12", slice .getColumnByName("testRangeSlicesQueryColumn2").getValue()); assertNull(slice.getColumnByName("testRangeSlicesQueryColumn3")); List<HColumn<String, String>> columns = slice.getColumns(); r = q.execute(); assertNotNull(r); rows = r.get(); assertEquals(3, rows.getCount()); for (Row<String, String, String> row2 : rows) { assertEquals(2, slice.getColumns().size()); for (HColumn<String, String> column : slice.getColumns()) { if (!column.getName().equals("testRangeSlicesQueryColumn1") && !column.getName().equals("testRangeSlicesQueryColumn2")) { fail("A columns with unexpected column name returned: "
MultigetSliceQuery<String, String, String> q = createMultigetSliceQuery(ko, se, se, se); q.setColumnFamily(cf); QueryResult<Rows<String, String, String>> r = q.execute(); assertNotNull(r); Rows<String, String, String> rows = r.get(); assertNotNull(rows); assertEquals(2, rows.getCount()); slice.getColumnByName("testMultigetSliceQueryColumn1").getValue()); assertEquals("value12", slice.getColumnByName("testMultigetSliceQueryColumn2").getValue()); assertNull(slice.getColumnByName("testMultigetSliceQueryColumn3")); List<HColumn<String, String>> columns = slice.getColumns(); q = createMultigetSliceQuery(ko, se, se, se); q.setColumnFamily(cf); q.setKeys("testMultigetSliceQuery3"); r = q.execute(); assertNotNull(r); rows = r.get(); assertEquals(1, rows.getCount()); for (Row<String, String, String> row2 : rows) {
@Override public String getStoredType(ByteBuffer rowKey, String cfName, Keyspace keyspace) { SliceQuery<ByteBuffer, String, ByteBuffer> query = MappingUtils .buildSliceQuery(rowKey, columns, cfName, keyspace); QueryResult<ColumnSlice<String, ByteBuffer>> result = query.execute(); // only need to check > 0. If the entity wasn't tombstoned then we would // have loaded the static jpa marker column HColumn<String, ByteBuffer> descrimValue = result.get().getColumnByName( DISCRIMINAATOR_COL); if (descrimValue == null) { return null; } return StringSerializer.get().fromByteBuffer(descrimValue.getValue()); }
/** * This method intentionally swallows ordered execution issues. For some reason, our Time UUID ordering does * not agree with the cassandra comparator as our micros get very close * @param query * @param <K> * @param <UUID> * @param <V> * @return */ protected static <K, UUID, V> List<HColumn<UUID, V>> swallowOrderedExecution( final SliceQuery<K, UUID, V> query ) { try { return query.execute().get().getColumns(); } catch ( HInvalidRequestException e ) { //invalid request. Occasionally we get order issues when there shouldn't be, disregard them. final Throwable invalidRequestException = e.getCause(); if ( invalidRequestException instanceof InvalidRequestException //we had a range error && ( ( InvalidRequestException ) invalidRequestException ).getWhy().contains( "range finish must come after start in the order of traversal" )) { return Collections.emptyList(); } throw e; } }