Search in sources :

Example 1 with CQLStatementTupleMapper

use of org.apache.storm.cassandra.query.CQLStatementTupleMapper in project storm by apache.

the class MapStateFactoryBuilder method build.

public StateFactory build() {
    Objects.requireNonNull(keyspace, "A keyspace is required.");
    Objects.requireNonNull(table, "A table name is required.");
    Objects.requireNonNull(keys, "At least one key must be specified.");
    if (keys.length == 0) {
        throw new IllegalArgumentException("At least one key must be specified.");
    }
    Objects.requireNonNull(stateMapper, "A state mapper must be specified.");
    Objects.requireNonNull(stateType, "A state type must be specified.");
    List<String> stateFields = stateMapper.getStateFields().toList();
    String[] stateFieldsArray = stateFields.toArray(new String[stateFields.size()]);
    List<String> allFields = new ArrayList<>();
    Collections.addAll(allFields, keys);
    allFields.addAll(stateFields);
    // Build get query
    Select.Where getQuery = select(stateFieldsArray).from(keyspace, table).where();
    for (String key : keys) {
        getQuery.and(eq(key, bindMarker()));
    }
    CQLStatementTupleMapper get = boundQuery(getQuery.toString()).bind(all()).build();
    // Build put query
    Insert putStatement = insertInto(keyspace, table).values(allFields, Collections.<Object>nCopies(allFields.size(), bindMarker()));
    CQLStatementTupleMapper put = boundQuery(putStatement.toString()).bind(all()).build();
    CassandraBackingMap.Options options = new CassandraBackingMap.Options<T>(new CassandraContext()).withGetMapper(get).withPutMapper(put).withStateMapper(stateMapper).withKeys(new Fields(keys)).withMaxParallelism(maxParallelism);
    logger.debug("Building factory with: \n  get: {}\n  put: {}\n  mapper: {}", getQuery.toString(), putStatement.toString(), stateMapper.toString());
    switch(stateType) {
        case NON_TRANSACTIONAL:
            return CassandraMapStateFactory.nonTransactional(options, cassandraConfig).withCache(cacheSize);
        case TRANSACTIONAL:
            return CassandraMapStateFactory.transactional(options, cassandraConfig).withCache(cacheSize);
        case OPAQUE:
            return CassandraMapStateFactory.opaque(options, cassandraConfig).withCache(cacheSize);
        default:
            throw new IllegalArgumentException(String.format("stateType %s not supported", stateType));
    }
}
Also used : ArrayList(java.util.ArrayList) CassandraContext(org.apache.storm.cassandra.CassandraContext) Insert(com.datastax.driver.core.querybuilder.Insert) CQLStatementTupleMapper(org.apache.storm.cassandra.query.CQLStatementTupleMapper) Fields(org.apache.storm.tuple.Fields) Select(com.datastax.driver.core.querybuilder.Select)

Aggregations

Insert (com.datastax.driver.core.querybuilder.Insert)1 Select (com.datastax.driver.core.querybuilder.Select)1 ArrayList (java.util.ArrayList)1 CassandraContext (org.apache.storm.cassandra.CassandraContext)1 CQLStatementTupleMapper (org.apache.storm.cassandra.query.CQLStatementTupleMapper)1 Fields (org.apache.storm.tuple.Fields)1