Java Code Examples for com.datastax.driver.core.querybuilder.QueryBuilder#in()

The following examples show how to use com.datastax.driver.core.querybuilder.QueryBuilder#in() . 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: CassandraSession.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void addWhereInClauses(Select.Where where, List<CassandraColumnHandle> partitionKeyColumns, List<Set<Object>> filterPrefixes)
{
    for (int i = 0; i < filterPrefixes.size(); i++) {
        CassandraColumnHandle column = partitionKeyColumns.get(i);
        List<Object> values = filterPrefixes.get(i)
                .stream()
                .map(value -> column.getCassandraType().getJavaValue(value))
                .collect(toList());
        Clause clause = QueryBuilder.in(CassandraCqlUtils.validColumnName(column.getName()), values);
        where.and(clause);
    }
}
 
Example 2
Source File: MapSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public void delete( final MapScope scope, final String key ) {

    Statement deleteMapEntry;
    Clause equalsEntryKey = QueryBuilder.eq("key", getMapEntryPartitionKey(scope, key));
    deleteMapEntry = QueryBuilder.delete().from(MAP_ENTRIES_TABLE)
        .where(equalsEntryKey);
    session.execute(deleteMapEntry);



    // not sure which bucket the value is in, execute a delete against them all
    final int[] buckets = BUCKET_LOCATOR.getAllBuckets( scope.getName() );
    List<ByteBuffer> mapKeys = new ArrayList<>();
    for( int bucket :  buckets){
        mapKeys.add( getMapKeyPartitionKey(scope, bucket));
    }

    Statement deleteMapKey;
    Clause inKey = QueryBuilder.in("key", mapKeys);
    Clause column1Equals = QueryBuilder.eq("column1", DataType.text().serialize(key, ProtocolVersion.NEWEST_SUPPORTED));
    deleteMapKey = QueryBuilder.delete().from(MAP_KEYS_TABLE)
        .where(inKey).and(column1Equals);
    session.execute(deleteMapKey);


}
 
Example 3
Source File: MapSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private ByteBuffer getValueCQL( MapScope scope, String key, final ConsistencyLevel consistencyLevel ) {

        Clause in = QueryBuilder.in("key", getMapEntryPartitionKey(scope, key) );
        Statement statement = QueryBuilder.select().all().from(MAP_ENTRIES_TABLE)
            .where(in)
            .setConsistencyLevel(consistencyLevel);

        ResultSet resultSet = session.execute(statement);
        com.datastax.driver.core.Row row = resultSet.one();

        return row != null ? row.getBytes("value") : null;
    }
 
Example 4
Source File: CassandraTable.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
protected Clause relation2Cql(Relation relation) {
    String key = relation.serialKey().toString();
    Object value = relation.serialValue();

    switch (relation.relation()) {
        case EQ:
            return QueryBuilder.eq(key, value);
        case GT:
            return QueryBuilder.gt(key, value);
        case GTE:
            return QueryBuilder.gte(key, value);
        case LT:
            return QueryBuilder.lt(key, value);
        case LTE:
            return QueryBuilder.lte(key, value);
        case IN:
            return QueryBuilder.in(key, value);
        case CONTAINS_VALUE:
            return QueryBuilder.contains(key, value);
        case CONTAINS_KEY:
            return QueryBuilder.containsKey(key, value);
        case SCAN:
            String[] col = pkColumnName().stream()
                                         .map(pk -> formatKey(pk))
                                         .toArray(String[]::new);
            Shard shard = (Shard) value;
            Object start = QueryBuilder.raw(shard.start());
            Object end = QueryBuilder.raw(shard.end());
            return Clauses.and(
                    QueryBuilder.gte(QueryBuilder.token(col), start),
                    QueryBuilder.lt(QueryBuilder.token(col), end));
        /*
         * Currently we can't support LIKE due to error:
         * "cassandra no viable alternative at input 'like'..."
         */
        // case LIKE:
        //    return QueryBuilder.like(key, value);
        case NEQ:
        default:
            throw new NotSupportException("relation '%s'", relation);
    }
}
 
Example 5
Source File: MapSerializationImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
public MapKeyResults getAllKeys(final MapScope scope, final String cursor, final int limit ){

    final int[] buckets = BUCKET_LOCATOR.getAllBuckets( scope.getName() );
    final List<ByteBuffer> partitionKeys = new ArrayList<>(NUM_BUCKETS.length);

    for (int bucket : buckets) {

        partitionKeys.add(getMapKeyPartitionKey(scope, bucket));
    }

    Clause in = QueryBuilder.in("key", partitionKeys);

    Statement statement;
    if( isBlank(cursor) ){
        statement = QueryBuilder.select().all().from(MAP_KEYS_TABLE)
            .where(in)
            .setFetchSize(limit);
    }else{
        statement = QueryBuilder.select().all().from(MAP_KEYS_TABLE)
            .where(in)
            .setFetchSize(limit)
            .setPagingState(PagingState.fromString(cursor));
    }


    ResultSet resultSet = session.execute(statement);
    PagingState pagingState = resultSet.getExecutionInfo().getPagingState();

    final List<String> keys = new ArrayList<>();
    Iterator<Row> resultIterator = resultSet.iterator();
    int size = 0;
    while( resultIterator.hasNext() && size < limit){

        size++;
        keys.add((String)DataType.text().deserialize(resultIterator.next().getBytes("column1"), ProtocolVersion.NEWEST_SUPPORTED));

    }

    return new MapKeyResults(pagingState != null ? pagingState.toString() : null, keys);

}
 
Example 6
Source File: MapSerializationImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
private <T> T getValuesCQL(
    final MapScope scope, final Collection<String> keys, final ResultsBuilderCQL<T> builder ) {

    final List<ByteBuffer> serializedKeys = new ArrayList<>();

    keys.forEach(key -> serializedKeys.add(getMapEntryPartitionKey(scope,key)));

    Clause in = QueryBuilder.in("key", serializedKeys );
    Statement statement = QueryBuilder.select().all().from(MAP_ENTRIES_TABLE)
        .where(in);


    ResultSet resultSet = session.execute(statement);

    return builder.buildResultsCQL( resultSet );
}
 
Example 7
Source File: UniqueValueSerializationStrategyImpl.java    From usergrid with Apache License 2.0 3 votes vote down vote up
@Override
public Iterator<UniqueValue> getAllUniqueFields( final ApplicationScope collectionScope, final Id entityId ) {
    Preconditions.checkNotNull( collectionScope, "collectionScope is required" );
    Preconditions.checkNotNull( entityId, "entity id is required" );


    Clause inKey = QueryBuilder.in("key", getLogPartitionKey(collectionScope.getApplication(), entityId));

    Statement statement = QueryBuilder.select().all().from(TABLE_UNIQUE_VALUES_LOG)
        .where(inKey);

    return new AllUniqueFieldsIterator(session, statement, entityId);


}