Search in sources :

Example 6 with BoundStatementBuilder

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

the class CassandraBaseTimeseriesDao method saveNull.

private ListenableFuture<Void> saveNull(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl, long partition, DataType type) {
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder((ttl == 0 ? getSaveStmt(type) : getSaveTtlStmt(type)).bind());
    stmtBuilder.setString(0, entityId.getEntityType().name()).setUuid(1, entityId.getId()).setString(2, tsKvEntry.getKey()).setLong(3, partition).setLong(4, tsKvEntry.getTs());
    stmtBuilder.setToNull(getColumnName(type));
    if (ttl > 0) {
        stmtBuilder.setInt(6, (int) ttl);
    }
    BoundStatement stmt = stmtBuilder.build();
    return getFuture(executeAsyncWrite(tenantId, stmt), rs -> null);
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 7 with BoundStatementBuilder

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

the class CassandraBaseTimeseriesLatestDao method findLatest.

@Override
public ListenableFuture<TsKvEntry> findLatest(TenantId tenantId, EntityId entityId, String key) {
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder(getFindLatestStmt().bind());
    stmtBuilder.setString(0, entityId.getEntityType().name());
    stmtBuilder.setUuid(1, entityId.getId());
    stmtBuilder.setString(2, key);
    BoundStatement stmt = stmtBuilder.build();
    log.debug(GENERATED_QUERY_FOR_ENTITY_TYPE_AND_ENTITY_ID, stmt, entityId.getEntityType(), entityId.getId());
    return getFuture(executeAsyncRead(tenantId, stmt), rs -> convertResultToTsKvEntry(key, rs.one()));
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 8 with BoundStatementBuilder

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

the class CQLKeyColumnValueStore method insertColumn.

public BatchableStatement<BoundStatement> insertColumn(final StaticBuffer key, final Entry entry, final Long timestamp) {
    final Integer ttl = (Integer) entry.getMetaData().get(EntryMetaData.TTL);
    BoundStatementBuilder builder = ttl != null ? insertColumnWithTTL.boundStatementBuilder() : insertColumn.boundStatementBuilder();
    builder = builder.setByteBuffer(KEY_BINDING, key.asByteBuffer()).setByteBuffer(COLUMN_BINDING, entry.getColumn().asByteBuffer()).setByteBuffer(VALUE_BINDING, entry.getValue().asByteBuffer());
    if (ttl != null) {
        builder = builder.setInt(TTL_BINDING, ttl);
    }
    if (timestamp != null) {
        builder = builder.setLong(TIMESTAMP_BINDING, timestamp);
    }
    return builder.build();
}
Also used : BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)

Example 9 with BoundStatementBuilder

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

the class CassandraDbHelper method loadCf.

public static void loadCf(KeyspaceMetadata ks, GuavaSession session, String cfName, String[] columns, Path sourceFile, boolean parseHeader) throws Exception {
    TableMetadata tableMetadata = ks.getTable(cfName).get();
    PreparedStatement prepared = session.prepare(createInsertStatement(cfName, columns));
    CSVFormat csvFormat = CSV_DUMP_FORMAT;
    if (parseHeader) {
        csvFormat = csvFormat.withFirstRecordAsHeader();
    } else {
        csvFormat = CSV_DUMP_FORMAT.withHeader(columns);
    }
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) {
        csvParser.forEach(record -> {
            BoundStatementBuilder boundStatementBuilder = new BoundStatementBuilder(prepared.bind());
            for (String column : columns) {
                setColumnValue(tableMetadata, column, record, boundStatementBuilder);
            }
            session.execute(boundStatementBuilder.build());
        });
    }
}
Also used : TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) CSVParser(org.apache.commons.csv.CSVParser) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 10 with BoundStatementBuilder

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

the class CassandraBaseTimeseriesDao method save.

@Override
public ListenableFuture<Integer> save(TenantId tenantId, EntityId entityId, TsKvEntry tsKvEntry, long ttl) {
    List<ListenableFuture<Void>> futures = new ArrayList<>();
    ttl = computeTtl(ttl);
    int dataPointDays = tsKvEntry.getDataPoints() * Math.max(1, (int) (ttl / SECONDS_IN_DAY));
    long partition = toPartitionTs(tsKvEntry.getTs());
    DataType type = tsKvEntry.getDataType();
    if (setNullValuesEnabled) {
        processSetNullValues(tenantId, entityId, tsKvEntry, ttl, futures, partition, type);
    }
    BoundStatementBuilder stmtBuilder = new BoundStatementBuilder((ttl == 0 ? getSaveStmt(type) : getSaveTtlStmt(type)).bind());
    stmtBuilder.setString(0, entityId.getEntityType().name()).setUuid(1, entityId.getId()).setString(2, tsKvEntry.getKey()).setLong(3, partition).setLong(4, tsKvEntry.getTs());
    addValue(tsKvEntry, stmtBuilder, 5);
    if (ttl > 0) {
        stmtBuilder.setInt(6, (int) ttl);
    }
    BoundStatement stmt = stmtBuilder.build();
    futures.add(getFuture(executeAsyncWrite(tenantId, stmt), rs -> null));
    return Futures.transform(Futures.allAsList(futures), result -> dataPointDays, MoreExecutors.directExecutor());
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) AggregationTimeseriesDao(org.thingsboard.server.dao.sqlts.AggregationTimeseriesDao) TbResultSet(org.thingsboard.server.dao.nosql.TbResultSet) PreDestroy(javax.annotation.PreDestroy) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Profiles(org.springframework.core.env.Profiles) ZoneOffset(java.time.ZoneOffset) Select(com.datastax.oss.driver.api.querybuilder.select.Select) BaseReadTsKvQuery(org.thingsboard.server.common.data.kv.BaseReadTsKvQuery) Function(com.google.common.base.Function) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DeleteTsKvQuery(org.thingsboard.server.common.data.kv.DeleteTsKvQuery) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) QueryBuilder.literal(com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal) Environment(org.springframework.core.env.Environment) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) LocalDateTime(java.time.LocalDateTime) NoSqlTsDao(org.thingsboard.server.dao.util.NoSqlTsDao) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) Row(com.datastax.oss.driver.api.core.cql.Row) Nullable(javax.annotation.Nullable) TbResultSetFuture(org.thingsboard.server.dao.nosql.TbResultSetFuture) ReentrantLock(java.util.concurrent.locks.ReentrantLock) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) Aggregation(org.thingsboard.server.common.data.kv.Aggregation) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) ChronoUnit(java.time.temporal.ChronoUnit) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ReadTsKvQuery(org.thingsboard.server.common.data.kv.ReadTsKvQuery) Collections(java.util.Collections) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DataType(org.thingsboard.server.common.data.kv.DataType) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

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