Search in sources :

Example 1 with BoundStatementBuilder

use of com.datastax.oss.driver.api.core.cql.BoundStatementBuilder in project zipkin by openzipkin.

the class SelectTraceIdsFromSpan method newCompletionStage.

@Override
protected CompletionStage<AsyncResultSet> newCompletionStage() {
    BoundStatementBuilder bound = preparedStatement.boundStatementBuilder();
    int i = 0;
    if (input.l_service() != null)
        bound.setString(i++, input.l_service());
    if (input.annotation_query() != null) {
        bound.setString(i++, input.annotation_query());
    } else {
        throw new IllegalArgumentException(input.toString());
    }
    bound.setUuid(i++, input.start_ts()).setUuid(i++, input.end_ts()).setInt(i, input.limit_()).setPageSize(input.limit_());
    return factory.session.executeAsync(bound.build());
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)

Example 2 with BoundStatementBuilder

use of com.datastax.oss.driver.api.core.cql.BoundStatementBuilder in project zipkin by openzipkin.

the class SelectTraceIdsFromServiceSpan method newCompletionStage.

@Override
protected CompletionStage<AsyncResultSet> newCompletionStage() {
    int i = 0;
    BoundStatementBuilder bound = preparedStatement.boundStatementBuilder().setString(i++, input.service()).setString(i++, input.span()).setInt(i++, input.bucket()).setUuid(i++, input.start_ts()).setUuid(i++, input.end_ts());
    if (input.start_duration() != null) {
        bound.setLong(i++, input.start_duration());
        bound.setLong(i++, input.end_duration());
    }
    bound.setInt(i, input.limit_()).setPageSize(input.limit_());
    return factory.session.executeAsync(bound.build());
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)

Example 3 with BoundStatementBuilder

use of com.datastax.oss.driver.api.core.cql.BoundStatementBuilder in project thingsboard by thingsboard.

the class TbSaveToCustomCassandraTableNode method save.

private ListenableFuture<Void> save(TbMsg msg, TbContext ctx) {
    JsonElement data = parser.parse(msg.getData());
    if (!data.isJsonObject()) {
        throw new IllegalStateException("Invalid message structure, it is not a JSON Object:" + data);
    } else {
        JsonObject dataAsObject = data.getAsJsonObject();
        BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(saveStmt.bind());
        AtomicInteger i = new AtomicInteger(0);
        fieldsMap.forEach((key, value) -> {
            if (key.equals(ENTITY_ID)) {
                stmtBuilder.setUuid(i.get(), msg.getOriginator().getId());
            } else if (dataAsObject.has(key)) {
                JsonElement dataKeyElement = dataAsObject.get(key);
                if (dataKeyElement.isJsonPrimitive()) {
                    JsonPrimitive primitive = dataKeyElement.getAsJsonPrimitive();
                    if (primitive.isNumber()) {
                        if (primitive.getAsString().contains(".")) {
                            stmtBuilder.setDouble(i.get(), primitive.getAsDouble());
                        } else {
                            stmtBuilder.setLong(i.get(), primitive.getAsLong());
                        }
                    } else if (primitive.isBoolean()) {
                        stmtBuilder.setBoolean(i.get(), primitive.getAsBoolean());
                    } else if (primitive.isString()) {
                        stmtBuilder.setString(i.get(), primitive.getAsString());
                    } else {
                        stmtBuilder.setToNull(i.get());
                    }
                } else if (dataKeyElement.isJsonObject()) {
                    stmtBuilder.setString(i.get(), dataKeyElement.getAsJsonObject().toString());
                } else {
                    throw new IllegalStateException("Message data key: '" + key + "' with value: '" + value + "' is not a JSON Object or JSON Primitive!");
                }
            } else {
                throw new RuntimeException("Message data doesn't contain key: " + "'" + key + "'!");
            }
            i.getAndIncrement();
        });
        return getFuture(executeAsyncWrite(ctx, stmtBuilder.build()), rs -> null);
    }
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 4 with BoundStatementBuilder

use of com.datastax.oss.driver.api.core.cql.BoundStatementBuilder in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method deletePartitionAsync.

private void deletePartitionAsync(TenantId tenantId, final QueryCursor cursor, final SimpleListenableFuture<Void> resultFuture) {
    if (!cursor.hasNextPartition()) {
        resultFuture.set(null);
    } else {
        PreparedStatement proto = getDeletePartitionStmt();
        BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(proto.bind());
        stmtBuilder.setString(0, cursor.getEntityType());
        stmtBuilder.setUuid(1, cursor.getEntityId());
        stmtBuilder.setLong(2, cursor.getNextPartition());
        stmtBuilder.setString(3, cursor.getKey());
        BoundStatement stmt = stmtBuilder.build();
        Futures.addCallback(executeAsyncWrite(tenantId, stmt), new FutureCallback<AsyncResultSet>() {

            @Override
            public void onSuccess(@Nullable AsyncResultSet result) {
                deletePartitionAsync(tenantId, cursor, resultFuture);
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Failed to delete data for query {}-{}", stmt, t);
            }
        }, readResultsProcessingExecutor);
    }
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 5 with BoundStatementBuilder

use of com.datastax.oss.driver.api.core.cql.BoundStatementBuilder in project thingsboard by thingsboard.

the class CassandraBaseTimeseriesDao method findAllAsyncSequentiallyWithLimit.

private void findAllAsyncSequentiallyWithLimit(TenantId tenantId, final TsKvQueryCursor cursor, final SimpleListenableFuture<List<TsKvEntry>> resultFuture) {
    if (cursor.isFull() || !cursor.hasNextPartition()) {
        resultFuture.set(cursor.getData());
    } else {
        PreparedStatement proto = getFetchStmt(Aggregation.NONE, cursor.getOrderBy());
        BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(proto.bind());
        stmtBuilder.setString(0, cursor.getEntityType());
        stmtBuilder.setUuid(1, cursor.getEntityId());
        stmtBuilder.setString(2, cursor.getKey());
        stmtBuilder.setLong(3, cursor.getNextPartition());
        stmtBuilder.setLong(4, cursor.getStartTs());
        stmtBuilder.setLong(5, cursor.getEndTs());
        stmtBuilder.setInt(6, cursor.getCurrentLimit());
        BoundStatement stmt = stmtBuilder.build();
        Futures.addCallback(executeAsyncRead(tenantId, stmt), new FutureCallback<TbResultSet>() {

            @Override
            public void onSuccess(@Nullable TbResultSet result) {
                if (result == null) {
                    cursor.addData(convertResultToTsKvEntryList(Collections.emptyList()));
                    findAllAsyncSequentiallyWithLimit(tenantId, cursor, resultFuture);
                } else {
                    Futures.addCallback(result.allRows(readResultsProcessingExecutor), new FutureCallback<List<Row>>() {

                        @Override
                        public void onSuccess(@Nullable List<Row> result) {
                            cursor.addData(convertResultToTsKvEntryList(result == null ? Collections.emptyList() : result));
                            findAllAsyncSequentiallyWithLimit(tenantId, cursor, resultFuture);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.error("[{}][{}] Failed to fetch data for query {}-{}", stmt, t);
                        }
                    }, readResultsProcessingExecutor);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                log.error("[{}][{}] Failed to fetch data for query {}-{}", stmt, t);
            }
        }, readResultsProcessingExecutor);
    }
}
Also used : TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) List(java.util.List) ArrayList(java.util.ArrayList) Row(com.datastax.oss.driver.api.core.cql.Row) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(javax.annotation.Nullable)

Aggregations

BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)13 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)8 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)5 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)3 Row (com.datastax.oss.driver.api.core.cql.Row)2 FutureCallback (com.google.common.util.concurrent.FutureCallback)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)1 QueryBuilder.literal (com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal)1 Select (com.datastax.oss.driver.api.querybuilder.select.Select)1 Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1