com.netflix.astyanax.query.RowQuery Java Examples

The following examples show how to use com.netflix.astyanax.query.RowQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ADelayedLocatorIO.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Locator> getLocators(SlotKey slotKey) throws IOException {

    Timer.Context ctx = Instrumentation.getReadTimerContext(CassandraModel.CF_METRICS_DELAYED_LOCATOR_NAME);

    try {
        RowQuery<SlotKey, Locator> query = AstyanaxIO.getKeyspace()
                .prepareQuery(CassandraModel.CF_METRICS_DELAYED_LOCATOR)
                .getKey(slotKey);
        return query.execute().getResult().getColumnNames();
    } catch (NotFoundException e) {
        Instrumentation.markNotFound(CassandraModel.CF_METRICS_DELAYED_LOCATOR_NAME);
        return Collections.emptySet();
    } catch (ConnectionException ex) {
        Instrumentation.markPoolExhausted();
        Instrumentation.markReadError();
        LOG.error("Connection exception during ADelayedLocatorIO.getLocators(" + slotKey.toString() + ")", ex);
        throw new IOException("Error reading delayed locators", ex);
    } finally {
        ctx.stop();
    }
}
 
Example #2
Source File: ALocatorIO.java    From blueflood with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the locators for a shard, i.e. those that should be rolled up, for a given shard.
 * 'Should' means:
 *  1) A locator is capable of rollup.
 *  2) A locator has had new data in the past LOCATOR_TTL seconds.
 *
 * @param shard Number of the shard you want the locators for. 0-127 inclusive.
 * @return Collection of locators
 * @throws IOException
 */
@Override
public Collection<Locator> getLocators(long shard) throws IOException {
    Timer.Context ctx = Instrumentation.getReadTimerContext(CassandraModel.CF_METRICS_LOCATOR_NAME);
    try {
        RowQuery<Long, Locator> query = AstyanaxIO.getKeyspace()
                .prepareQuery(CassandraModel.CF_METRICS_LOCATOR)
                .getKey(shard);
        if (LOG.isTraceEnabled())
            LOG.trace("ALocatorIO.getLocators() executing: select * from \"" + CassandraModel.KEYSPACE + "\"." + CassandraModel.CF_METRICS_LOCATOR_NAME + " where key=" + Long.toString(shard));
        return query.execute().getResult().getColumnNames();
    } catch (NotFoundException e) {
        Instrumentation.markNotFound(CassandraModel.CF_METRICS_LOCATOR_NAME);
        return Collections.emptySet();
    } catch (ConnectionException ex) {
        Instrumentation.markReadError(ex);
        LOG.error("Connection exception during getLocators(" + Long.toString(shard) + ")", ex);
        throw new IOException("Error reading locators", ex);
    } finally {
        ctx.stop();
    }
}
 
Example #3
Source File: AstyanaxQueueDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code RowQuery} with {@code autoPaginate(true)} repeatedly as necessary to fetch all pages. */
private <K, C> Iterator<Column<C>> executePaginated(final RowQuery<K, C> query) {
    return Iterators.concat(new AbstractIterator<Iterator<Column<C>>>() {
        @Override
        protected Iterator<Column<C>> computeNext() {
            ColumnList<C> page = execute(query);
            return !page.isEmpty() ? page.iterator() : endOfData();
        }
    });
}
 
Example #4
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code RowQuery} with {@code autoPaginate(true)} repeatedly as necessary to fetch all pages. */
private <K, C> Iterator<Column<C>> executePaginated(final RowQuery<K, C> query) {
    return Iterators.concat(new AbstractIterator<Iterator<Column<C>>>() {
        @Override
        protected Iterator<Column<C>> computeNext() {
            ColumnList<C> page = execute(query);
            return !page.isEmpty() ? page.iterator() : endOfData();
        }
    });
}
 
Example #5
Source File: HystrixCassandraGetRow.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
@Override
protected ColumnList<String> run() throws Exception {
     RowQuery<RowKeyType, String> rowQuery = keyspace.prepareQuery(columnFamily).getKey(rowKey);
     /* apply column slice if we have one */
     if (columns != null) {
         rowQuery = rowQuery.withColumnSlice(columns);
     }
     ColumnList<String> result = rowQuery.execute().getResult();
     return result;
}
 
Example #6
Source File: AstyanaxThriftDataTableResource.java    From staash with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult readRow(String key, Integer columnCount, String startColumn, String endColumn, Boolean reversed) throws PaasException {
    invariant();
    
    try {
        // Construct the query
        RowQuery<ByteBuffer, ByteBuffer> query = keyspace
                .prepareQuery(this.columnFamily)
                .getRow(serializers.keyAsByteBuffer(key));
                
        RangeBuilder range = new RangeBuilder();
        if (columnCount != null && columnCount > 0) {
            range.setLimit(columnCount);
        }
        if (startColumn != null && !startColumn.isEmpty()) {
            range.setStart(serializers.columnAsByteBuffer(startColumn));
        }
        if (endColumn != null && !endColumn.isEmpty()) {
            range.setEnd(serializers.columnAsByteBuffer(endColumn));
        }
        range.setReversed(reversed);
        query.withColumnRange(range.build());

        // Execute the query
        ColumnList<ByteBuffer> result = query.execute().getResult();
        
        // Convert raw data into a simple sparse tree
        SchemalessRows.Builder builder = SchemalessRows.builder();
        Map<String, String> columns = Maps.newHashMap();
        if (!result.isEmpty()) {
            for (Column<ByteBuffer> column : result) { 
                columns.put(serializers.columnAsString(column.getRawName()), serializers.valueAsString(column.getRawName(), column.getByteBufferValue()));
            }
            builder.addRow(key, columns);
        }

        QueryResult dr = new QueryResult();
        dr.setSrows(builder.build());
        return dr;
    } catch (ConnectionException e) {
        throw new PaasException(
                String.format("Failed to read row '%s' in column family '%s.%s'" , 
                              key, this.keyspace.getKeyspaceName(), this.columnFamily.getName()),
                e);
    }
}
 
Example #7
Source File: ColumnNameIterator.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public ColumnNameIterator( RowQuery<?, C> rowQuery, final ColumnParser<C, T> parser, final boolean skipFirst ) {
    this.rowQuery = rowQuery.autoPaginate( true );
    this.parser = parser;
    this.skipFirst = skipFirst;
}
 
Example #8
Source File: MultiKeyColumnNameIteratorTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private static ColumnNameIterator<Long, Long> createIterator( final String rowKey, final boolean reversed ) {


        final ColumnParser<Long, Long> longParser = new ColumnParser<Long, Long>() {
            @Override
            public Long parseColumn( final Column<Long> column ) {
                return column.getName();
            }
        };

        final RangeBuilder forwardRange = new RangeBuilder().setLimit( 720 ).setReversed( reversed );


        final RowQuery<String, Long> forwardQuery =
                keyspace.prepareQuery( COLUMN_FAMILY ).getKey( rowKey ).withColumnRange( forwardRange.build() );


        ColumnNameIterator<Long, Long> itr = new ColumnNameIterator<>( forwardQuery, longParser, false );

        return itr;
    }
 
Example #9
Source File: EdgeMetadataSerializationV1Impl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Get the edge types from the search criteria.
 *
 * @param scope The org scope
 * @param search The edge type search info
 * @param cf The column family to execute on
 */
private Iterator<String> getEdgeTypes( final ApplicationScope scope, final SearchEdgeType search,
                                       final MultiTenantColumnFamily<ScopedRowKey<Id>, String> cf ) {
    ValidationUtils.validateApplicationScope( scope );
    GraphValidation.validateSearchEdgeType( search );


    final ScopedRowKey< Id> sourceKey = new ScopedRowKey<>( scope.getApplication(), search.getNode() );


    //resume from the last if specified.  Also set the range


    final RangeBuilder rangeBuilder = createRange( search );

    RowQuery<ScopedRowKey<Id>, String> query =
            keyspace.prepareQuery( cf ).getKey( sourceKey ).autoPaginate( true )
                    .withColumnRange( rangeBuilder.build() );

    return new ColumnNameIterator<>( query, PARSER, search.getLast().isPresent() );
}
 
Example #10
Source File: EdgeMetadataSerializationV1Impl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Get the id types from the specified column family
 *
 * @param scope The organization scope to use
 * @param search The search criteria
 * @param cf The column family to search
 */
public Iterator<String> getIdTypes( final ApplicationScope scope, final SearchIdType search,
                                    final MultiTenantColumnFamily<ScopedRowKey<EdgeIdTypeKey>, String> cf ) {
    ValidationUtils.validateApplicationScope( scope );
    GraphValidation.validateSearchEdgeIdType( search );


    final ScopedRowKey<EdgeIdTypeKey> sourceTypeKey =
            new ScopedRowKey<>( scope.getApplication(), new EdgeIdTypeKey( search.getNode(), search.getEdgeType() ) );


    final RangeBuilder rangeBuilder = createRange( search );


    RowQuery<ScopedRowKey<EdgeIdTypeKey>, String> query =
            keyspace.prepareQuery( cf ).getKey( sourceTypeKey ).autoPaginate( true )
                    .withColumnRange( rangeBuilder.build() );


    return new ColumnNameIterator<>( query, PARSER, search.getLast().isPresent() );
}
 
Example #11
Source File: MvccEntitySerializationStrategyImpl.java    From usergrid with Apache License 2.0 3 votes vote down vote up
@Override
public Iterator<MvccEntity> loadDescendingHistory( final ApplicationScope applicationScope, final Id entityId,
                                                   final UUID version, final int fetchSize ) {

    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );
    Preconditions.checkNotNull( version, "version is required" );
    Preconditions.checkArgument( fetchSize > 0, "max Size must be greater than 0" );


    final Id applicationId = applicationScope.getApplication();
    final Id ownerId = applicationId;
    final String collectionName = LegacyScopeUtils.getCollectionScopeNameFromEntityType( entityId.getType() );

    final CollectionPrefixedKey<Id> collectionPrefixedKey =
            new CollectionPrefixedKey<>( collectionName, ownerId, entityId );


    final ScopedRowKey<CollectionPrefixedKey<Id>> rowKey =
            ScopedRowKey.fromKey( applicationId, collectionPrefixedKey );


    RowQuery<ScopedRowKey<CollectionPrefixedKey<Id>>, UUID> query =
            keyspace.prepareQuery( columnFamily ).getKey( rowKey )
                    .withColumnRange( version, null, false, fetchSize );

    return new ColumnNameIterator( query, new MvccColumnParser( entityId, getEntitySerializer() ), false );
}
 
Example #12
Source File: MvccEntitySerializationStrategyImpl.java    From usergrid with Apache License 2.0 3 votes vote down vote up
@Override
public Iterator<MvccEntity> loadAscendingHistory( final ApplicationScope applicationScope, final Id entityId,
                                                  final UUID version, final int fetchSize ) {

    Preconditions.checkNotNull( applicationScope, "applicationScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );
    Preconditions.checkNotNull( version, "version is required" );
    Preconditions.checkArgument( fetchSize > 0, "max Size must be greater than 0" );


    final Id applicationId = applicationScope.getApplication();
    final Id ownerId = applicationId;
    final String collectionName = LegacyScopeUtils.getCollectionScopeNameFromEntityType( entityId.getType() );

    final CollectionPrefixedKey<Id> collectionPrefixedKey =
            new CollectionPrefixedKey<>( collectionName, ownerId, entityId );


    final ScopedRowKey<CollectionPrefixedKey<Id>> rowKey =
            ScopedRowKey.fromKey( applicationId, collectionPrefixedKey );


    RowQuery<ScopedRowKey<CollectionPrefixedKey<Id>>, UUID> query =
            keyspace.prepareQuery( columnFamily ).getKey( rowKey )
                    .withColumnRange( null, version, true, fetchSize );

    return new ColumnNameIterator( query, new MvccColumnParser( entityId, getEntitySerializer() ), false );
}
 
Example #13
Source File: EdgeShardSerializationImpl.java    From usergrid with Apache License 2.0 3 votes vote down vote up
@Override
public Iterator<Shard> getShardMetaData( final ApplicationScope scope,
                                         final Optional<Shard> start,   final DirectedEdgeMeta metaData  ) {

    ValidationUtils.validateApplicationScope( scope );
    GraphValidation.validateDirectedEdgeMeta( metaData );


    Preconditions.checkNotNull( metaData, "metadata must be present" );

    /**
     * If the edge is present, we need to being seeking from this
     */

    final RangeBuilder rangeBuilder = new RangeBuilder().setLimit( graphFig.getScanPageSize() );

    if ( start.isPresent() ) {
        final Shard shard = start.get();
        GraphValidation.valiateShard( shard );
        rangeBuilder.setStart( shard.getShardIndex() );
    }


    final ScopedRowKey rowKey = ScopedRowKey.fromKey( scope.getApplication(), metaData );


    final RowQuery<ScopedRowKey<DirectedEdgeMeta>, Long> query =
            keyspace.prepareQuery( EDGE_SHARDS ).setConsistencyLevel( cassandraConfig.getReadCL() ).getKey( rowKey )
                    .autoPaginate( true ).withColumnRange( rangeBuilder.build() );


    return new ColumnNameIterator<>( query, COLUMN_PARSER, false );
}