Refine search
@Override public String getStatusMessage(final String pluginName) { final ScopedRowKey<String> rowKey = ScopedRowKey.fromKey( STATIC_ID, pluginName); try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( rowKey ).getColumn( COL_STATUS_MESSAGE ) .execute().getResult().getStringValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return null; } catch ( ConnectionException e ) { throw new DataMigrationException( "Unable to retrieve status", e ); } }
@Test public void testGetColumnSlice() throws ConnectionException { LOG.info("Starting testGetColumnSlice..."); OperationResult<ColumnList<String>> result = keyspace .prepareQuery(CF_STANDARD1) .getKey("A") .withColumnSlice( new ColumnSlice<String>("c", "h").setLimit(5)) .execute(); Assert.assertNotNull(result.getResult()); Assert.assertEquals(5, result.getResult().size()); }
@SuppressWarnings("unchecked") public Map<C, V> lookup(TupleMapper<K, C, V> tupleMapper, Tuple input) throws Exception { String cf = tupleMapper.mapToColumnFamily(input); String keyspace = tupleMapper.mapToKeyspace(input); K rowKey = tupleMapper.mapToRowKey(input); Class<K> keyClass = tupleMapper.getKeyClass(); Class<C> colClass = tupleMapper.getColumnNameClass(); ColumnFamily<K, C> columnFamily = new ColumnFamily<K, C>(cf, (Serializer<K>) serializerFor(keyClass), (Serializer<C>) serializerFor(colClass)); OperationResult<ColumnList<C>> result; result = this.getKeyspace(keyspace).prepareQuery(columnFamily).getKey(rowKey).execute(); ColumnList<C> columns = (ColumnList<C>) result.getResult(); HashMap<C, V> retval = new HashMap<C, V>(); Iterator<Column<C>> it = columns.iterator(); while (it.hasNext()) { Column<C> col = it.next(); retval.put(col.getName(), col.getValue((Serializer<V>) serializerFor(tupleMapper.getColumnValueClass()))); } return retval; }
@Test public void testColumnRangeSlice() throws ConnectionException { OperationResult<ColumnList<String>> r1 = keyspace .prepareQuery(CF_STANDARD1) .getKey("A") .withColumnRange( new RangeBuilder().setStart("a").setEnd("b") .setLimit(5).build()).execute(); Assert.assertEquals(2, r1.getResult().size()); OperationResult<ColumnList<String>> r2 = keyspace .prepareQuery(CF_STANDARD1).getKey("A") .withColumnRange("a", null, false, 5).execute(); Assert.assertEquals(5, r2.getResult().size()); Assert.assertEquals("a", r2.getResult().getColumnByIndex(0).getName()); ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]); OperationResult<ColumnList<String>> r3 = keyspace .prepareQuery(CF_STANDARD1).getKey("A") .withColumnRange(EMPTY_BUFFER, EMPTY_BUFFER, true, 5).execute(); Assert.assertEquals(5, r3.getResult().size()); Assert.assertEquals("z", r3.getResult().getColumnByIndex(0).getName()); }
private void verifyPresent(Keyspace ks, int rowKey) throws ConnectionException { ColumnList<Long> result = ks.prepareQuery(CF_DUAL_WRITES).getRow(rowKey).execute().getResult(); if (result.isEmpty()) { throw new RuntimeException("Row: " + rowKey + " missing from keysapce: " + ks.getKeyspaceName()); } else { System.out.println("Verified Row: " + rowKey + " present in ks: " + ks.getKeyspaceName()); } }
@Test public void testGetSingleKey() throws ConnectionException { for (char key = 'A'; key <= 'Z'; key++) { String keyName = Character.toString(key); OperationResult<ColumnList<String>> result = keyspace.prepareQuery(CF_STANDARD1).getKey(keyName).execute(); Assert.assertNotNull(result.getResult()); Assert.assertFalse(result.getResult().isEmpty()); } }
if ( keyspace.describeKeyspace().getColumnFamily( CF_NAME ) == null ) { keyspace.createColumnFamily( testCf, null ); MutationBatch batch = keyspace.prepareMutationBatch(); Column<String> col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); assertEquals( colname, col.getName() ); assertEquals( colvalue, col.getStringValue() ); keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); batch.execute(); col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); assertEquals( colname, col.getName() ); assertEquals( colvalue, col.getStringValue() ); col = keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult(); assertEquals( colname, col.getName() ); keyspace.prepareQuery( testCf ).getKey( key ).getColumn( colname ).execute().getResult();
if ( keyspace.describeKeyspace().getColumnFamily( "test" ) == null ) { keyspace.createColumnFamily( cf, new HashMap<String, Object>() ); final long timestamp = 100l; MutationBatch batch = keyspace.prepareMutationBatch().withConsistencyLevel( ConsistencyLevel.CL_QUORUM ) .setTimestamp( timestamp ); Column<String> column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); assertEquals( colName, column.getName() ); assertTrue( column.getBooleanValue() ); column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); fail( "I shouldn't return a value" ); column = keyspace.prepareQuery( cf ).getKey( rowKey ).getColumn( colName ).execute().getResult(); fail( "I shouldn't return a value" );
/** * Read from a {@link CassandraNode} using the Astyanax API. * @throws ConnectionException */ public void readData(String keyspaceName) throws ConnectionException { // Create context AstyanaxContext<Keyspace> context = newAstyanaxContextForKeyspace(keyspaceName); try { Keyspace keyspace = context.getEntity(); // Query data OperationResult<ColumnList<String>> query = keyspace.prepareQuery(sampleColumnFamily) .getKey("one") .execute(); assertEquals(query.getHost().getHostName(), hostname); assertTrue(query.getLatency() > 0L); ColumnList<String> columns = query.getResult(); assertEquals(columns.size(), 2); // Lookup columns in response by name String name = columns.getColumnByName("name").getStringValue(); assertEquals(name, "Alice"); // Iterate through the columns for (Column<String> c : columns) { assertTrue(ImmutableList.of("name", "company").contains(c.getName())); } } finally { context.shutdown(); } }
keyspace.prepareQuery( cf ).setConsistencyLevel( consistencyLevel ).getKeySlice( rowKeys ) .withColumnRange( rangeBuilder.build() ); result = query.execute().getResult();
latestEntityColumns = keyspace.prepareQuery( CF_ENTITY_LOG ).getKeySlice( rowKeys ) .withColumnRange( maxVersion, null, false, 1 ).execute().getResult() .iterator(); if ( columns.size() == 0 ) { continue; final Column<UUID> column = columns.getColumnByIndex( 0 ); final UUID version = column.getName(); final StageStatus stageStatus = column.getValue( SER );
public void read(int empId) { OperationResult<ColumnList<String>> result; try { result = keyspace.prepareQuery(EMP_CF) .getKey(empId) .execute(); ColumnList<String> cols = result.getResult(); logger.debug("read: isEmpty: "+cols.isEmpty()); for(Iterator<Column<String>> i = cols.iterator(); i.hasNext(); ) { Column<String> c = i.next(); Object v = null; if(c.getName().endsWith("id")) // type induction hack v = c.getIntegerValue(); else v = c.getStringValue(); logger.debug("- col: '"+c.getName()+"': "+v); logger.debug("- emp id: "+cols.getIntegerValue(COL_NAME_EMPID, null)); logger.debug("- dept: "+cols.getIntegerValue(COL_NAME_DEPTID, null)); logger.debug("- firstName: "+cols.getStringValue(COL_NAME_FIRST_NAME, null));
@SuppressWarnings("unchecked") public Map<C, V> lookup(TridentTupleMapper<K, C, V> tupleMapper, TridentTuple input, List<C> slice) throws Exception { String cf = tupleMapper.mapToColumnFamily(input); String keyspace = tupleMapper.mapToKeyspace(input); K rowKey = tupleMapper.mapToRowKey(input); Class<K> keyClass = tupleMapper.getKeyClass(); Class<C> colClass = tupleMapper.getColumnNameClass(); ColumnFamily<K, C> columnFamily = new ColumnFamily<K, C>(cf, (Serializer<K>) serializerFor(keyClass), (Serializer<C>) serializerFor(colClass)); HashMap<C, V> retval = new HashMap<C, V>(); for (C c : slice) { RowQuery<K, C> query = this.getKeyspace(keyspace).prepareQuery(columnFamily).getKey(rowKey); query = query.withColumnRange(getRangeBuilder(c, c, null, (Serializer<C>) serializerFor(colClass))); OperationResult<ColumnList<C>> result = query.execute(); LOG.debug("Selecting [" + c.toString() + "] returned [" + result.getResult().size() + "] results."); Iterator<Column<C>> it = result.getResult().iterator(); while (it.hasNext()) { Column<C> col = it.next(); LOG.debug("Adding [" + col.getName() + "]=>[" + col.getStringValue() + "]"); retval.put(col.getName(), col.getValue((Serializer<V>) serializerFor(tupleMapper.getColumnValueClass()))); } } return retval; }
@Override public ByteBuffer readChunk(String objectName, int chunkId) throws Exception { return keyspace.prepareQuery(cf).setConsistencyLevel(readConsistencyLevel).withRetryPolicy(retryPolicy) .getKey(getRowKey(objectName, chunkId)).getColumn(getColumnName(Columns.DATA)).execute().getResult() .getByteBufferValue(); }
@Override public int getSystemVersion() { try { return keyspace.prepareQuery( CF_MIGRATION_INFO ).getKey( LEGACY_ROW_KEY ).getColumn( COLUMN_VERSION ) .execute().getResult().getIntegerValue(); } //swallow, it doesn't exist catch ( NotFoundException nfe ) { return 0; } catch ( ConnectionException e ) { throw new DataMigrationException( "Unable to retrieve status", e ); } }
@Test public void testHasValue() throws Exception { MutationBatch m = keyspace.prepareMutationBatch(); m.withRow(CF_USER_INFO, "acct1234") .putColumn("firstname", "john", null) .putColumn("lastname", "smith", null) .putColumn("address", "555 Elm St", null) .putColumn("age", 30, null) .putEmptyColumn("empty"); m.execute(); ColumnList<String> response = keyspace.prepareQuery(CF_USER_INFO).getRow("acct1234").execute().getResult(); Assert.assertEquals("firstname", response.getColumnByName("firstname").getName()); Assert.assertEquals("firstname", response.getColumnByName("firstname").getName()); Assert.assertEquals("john", response.getColumnByName("firstname").getStringValue()); Assert.assertEquals("john", response.getColumnByName("firstname").getStringValue()); Assert.assertEquals(true, response.getColumnByName("firstname").hasValue()); Assert.assertEquals(false, response.getColumnByName("empty").hasValue()); }
private void testSingleRowAllColumnsColumnCountQuery(boolean rowDeleted) throws Exception { int expected = rowDeleted ? 0 : columnNames.size(); for (int i=0; i<TestRowCount; i++) { int count = keyspace.prepareQuery(CF_USER_INFO).getRow("acct_" + i).getCount().execute().getResult().intValue(); Assert.assertEquals(expected, count); } }
@Override public ObjectMetadata readMetadata(String objectName) throws Exception, NotFoundException { ColumnList<String> columns = keyspace.prepareQuery(cf).getKey(objectName).execute().getResult(); if (columns.isEmpty()) { throw new NotFoundException(objectName); } return new ObjectMetadata().setObjectSize(columns.getLongValue(getColumnName(Columns.OBJECTSIZE), null)) .setChunkSize(columns.getIntegerValue(getColumnName(Columns.CHUNKSIZE), null)) .setChunkCount(columns.getIntegerValue(getColumnName(Columns.CHUNKCOUNT), null)) .setAttributes(columns.getStringValue(getColumnName(Columns.ATTRIBUTES), null)); }
@Override public Optional<Long> getMaxVersion( final ApplicationScope scope, final Id node ) { ValidationUtils.validateApplicationScope( scope ); ValidationUtils.verifyIdentity( node ); ColumnFamilyQuery<ScopedRowKey<Id>, Boolean> query = keyspace.prepareQuery( GRAPH_DELETE ).setConsistencyLevel( fig.getReadCL() ); Column<Boolean> result = null; try { result = query.getKey( ScopedRowKey.fromKey( scope.getApplication(), node ) ).getColumn( COLUMN_NAME ).execute() .getResult(); } catch(NotFoundException nfe){ //swallow, there's just no column return Optional.absent(); } catch ( ConnectionException e ) { throw new RuntimeException( "Unable to connect to casandra", e ); } return Optional.of( result.getLongValue() ); }
return keyspace.prepareQuery( CF_ENTITY_DATA ).getKeySlice( scopedRowKeys ) .withColumnSlice( COL_VALUE ).execute().getResult(); if ( columns.size() == 0 ) { continue; final Column<Boolean> column = columns.getColumnByIndex( 0 );