Search in sources :

Example 1 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project flink by apache.

the class CassandraOutputFormat method writeRecord.

@Override
public void writeRecord(OUT record) throws IOException {
    if (exception != null) {
        throw new IOException("write record failed", exception);
    }
    Object[] fields = new Object[record.getArity()];
    for (int i = 0; i < record.getArity(); i++) {
        fields[i] = record.getField(i);
    }
    ResultSetFuture result = session.executeAsync(prepared.bind(fields));
    Futures.addCallback(result, callback);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) IOException(java.io.IOException)

Example 2 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project nifi by apache.

the class PutCassandraQL method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    ComponentLog logger = getLogger();
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    final long statementTimeout = context.getProperty(STATEMENT_TIMEOUT).evaluateAttributeExpressions(flowFile).asTimePeriod(TimeUnit.MILLISECONDS);
    final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());
    // The documentation for the driver recommends the session remain open the entire time the processor is running
    // and states that it is thread-safe. This is why connectionSession is not in a try-with-resources.
    final Session connectionSession = cassandraSession.get();
    String cql = getCQL(session, flowFile, charset);
    try {
        PreparedStatement statement = connectionSession.prepare(cql);
        BoundStatement boundStatement = statement.bind();
        Map<String, String> attributes = flowFile.getAttributes();
        for (final Map.Entry<String, String> entry : attributes.entrySet()) {
            final String key = entry.getKey();
            final Matcher matcher = CQL_TYPE_ATTRIBUTE_PATTERN.matcher(key);
            if (matcher.matches()) {
                final int parameterIndex = Integer.parseInt(matcher.group(1));
                String paramType = entry.getValue();
                if (StringUtils.isEmpty(paramType)) {
                    throw new ProcessException("Value of the " + key + " attribute is null or empty, it must contain a valid value");
                }
                paramType = paramType.trim();
                final String valueAttrName = "cql.args." + parameterIndex + ".value";
                final String parameterValue = attributes.get(valueAttrName);
                try {
                    setStatementObject(boundStatement, parameterIndex - 1, valueAttrName, parameterValue, paramType);
                } catch (final InvalidTypeException | IllegalArgumentException e) {
                    throw new ProcessException("The value of the " + valueAttrName + " is '" + parameterValue + "', which cannot be converted into the necessary data type: " + paramType, e);
                }
            }
        }
        try {
            ResultSetFuture future = connectionSession.executeAsync(boundStatement);
            if (statementTimeout > 0) {
                future.getUninterruptibly(statementTimeout, TimeUnit.MILLISECONDS);
            } else {
                future.getUninterruptibly();
            }
            // Emit a Provenance SEND event
            final long transmissionMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
            // This isn't a real URI but since Cassandra is distributed we just use the cluster name
            String transitUri = "cassandra://" + connectionSession.getCluster().getMetadata().getClusterName();
            session.getProvenanceReporter().send(flowFile, transitUri, transmissionMillis, true);
            session.transfer(flowFile, REL_SUCCESS);
        } catch (final TimeoutException e) {
            throw new ProcessException(e);
        }
    } catch (final NoHostAvailableException nhae) {
        getLogger().error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae);
        // Log up to 10 error messages. Otherwise if a 1000-node cluster was specified but there was no connectivity,
        // a thousand error messages would be logged. However we would like information from Cassandra itself, so
        // cap the error limit at 10, format the messages, and don't include the stack trace (it is displayed by the
        // logger message above).
        getLogger().error(nhae.getCustomMessage(10, true, false));
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_RETRY);
    } catch (final QueryExecutionException qee) {
        logger.error("Cannot execute the statement with the requested consistency level successfully", qee);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_RETRY);
    } catch (final QueryValidationException qve) {
        logger.error("The CQL statement {} is invalid due to syntax error, authorization issue, or another " + "validation problem; routing {} to failure", new Object[] { cql, flowFile }, qve);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    } catch (final ProcessException e) {
        logger.error("Unable to execute CQL select statement {} for {} due to {}; routing to failure", new Object[] { cql, flowFile, e });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Matcher(java.util.regex.Matcher) Charset(java.nio.charset.Charset) PreparedStatement(com.datastax.driver.core.PreparedStatement) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) QueryExecutionException(com.datastax.driver.core.exceptions.QueryExecutionException) QueryValidationException(com.datastax.driver.core.exceptions.QueryValidationException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) BoundStatement(com.datastax.driver.core.BoundStatement) Map(java.util.Map) Session(com.datastax.driver.core.Session) ProcessSession(org.apache.nifi.processor.ProcessSession) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project SimpleFlatMapper by arnaudroger.

the class DatastaxCrud method deleteAsync.

public UninterruptibleFuture<Void> deleteAsync(K key) {
    BoundStatement boundStatement = deleteQuery(key);
    ResultSetFuture resultSetFuture = session.executeAsync(boundStatement);
    return new NoResultFuture(resultSetFuture);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 4 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project SimpleFlatMapper by arnaudroger.

the class DatastaxCrud method deleteAsync.

public UninterruptibleFuture<Void> deleteAsync(K key, long timestamp) {
    BoundStatement boundStatement = deleteQuery(key, timestamp);
    ResultSetFuture resultSetFuture = session.executeAsync(boundStatement);
    return new NoResultFuture(resultSetFuture);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 5 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project thingsboard by thingsboard.

the class CassandraDeviceDao method findTenantDeviceTypesAsync.

@Override
public ListenableFuture<List<EntitySubtype>> findTenantDeviceTypesAsync(UUID tenantId) {
    Select select = select().from(ENTITY_SUBTYPE_COLUMN_FAMILY_NAME);
    Select.Where query = select.where();
    query.and(eq(ENTITY_SUBTYPE_TENANT_ID_PROPERTY, tenantId));
    query.and(eq(ENTITY_SUBTYPE_ENTITY_TYPE_PROPERTY, EntityType.DEVICE));
    query.setConsistencyLevel(cluster.getDefaultReadConsistencyLevel());
    ResultSetFuture resultSetFuture = executeAsyncRead(query);
    return Futures.transform(resultSetFuture, new Function<ResultSet, List<EntitySubtype>>() {

        @Nullable
        @Override
        public List<EntitySubtype> apply(@Nullable ResultSet resultSet) {
            Result<EntitySubtypeEntity> result = cluster.getMapper(EntitySubtypeEntity.class).map(resultSet);
            if (result != null) {
                List<EntitySubtype> entitySubtypes = new ArrayList<>();
                result.all().forEach((entitySubtypeEntity) -> entitySubtypes.add(entitySubtypeEntity.toEntitySubtype()));
                return entitySubtypes;
            } else {
                return Collections.emptyList();
            }
        }
    });
}
Also used : java.util(java.util) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Function(com.google.common.base.Function) CassandraAbstractSearchTextDao(org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao) Result(com.datastax.driver.mapping.Result) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DeviceEntity(org.thingsboard.server.dao.model.nosql.DeviceEntity) Device(org.thingsboard.server.common.data.Device) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) ResultSet(com.datastax.driver.core.ResultSet) EntityType(org.thingsboard.server.common.data.EntityType) DaoUtil(org.thingsboard.server.dao.DaoUtil) EntitySubtypeEntity(org.thingsboard.server.dao.model.EntitySubtypeEntity) Select(com.datastax.driver.core.querybuilder.Select) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) Statement(com.datastax.driver.core.Statement) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Nullable(javax.annotation.Nullable) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Nullable(javax.annotation.Nullable) Result(com.datastax.driver.mapping.Result)

Aggregations

ResultSetFuture (com.datastax.driver.core.ResultSetFuture)78 Test (org.junit.Test)35 UUID (java.util.UUID)26 ResultSet (com.datastax.driver.core.ResultSet)20 EntityWithCollections (com.datastax.driver.mapping.entity.EntityWithCollections)13 BoundStatement (com.datastax.driver.core.BoundStatement)12 PreparedStatement (com.datastax.driver.core.PreparedStatement)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 Row (com.datastax.driver.core.Row)7 Statement (com.datastax.driver.core.Statement)7 List (java.util.List)6 Session (com.datastax.driver.core.Session)5 Simple (com.datastax.driver.mapping.entity.Simple)5 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 BigDecimal (java.math.BigDecimal)5 HashSet (java.util.HashSet)5 Result (com.datastax.driver.mapping.Result)4 WriteOptions (com.datastax.driver.mapping.option.WriteOptions)4 Nullable (javax.annotation.Nullable)4