Search in sources :

Example 1 with Statement

use of com.datastax.oss.driver.api.core.cql.Statement in project cas by apereo.

the class CassandraTicketRegistry method addTicketToCassandra.

private void addTicketToCassandra(final Ticket ticket, final boolean inserting) throws Exception {
    LOGGER.debug("Adding ticket [{}]", ticket.getId());
    val metadata = this.ticketCatalog.find(ticket);
    LOGGER.trace("Located ticket definition [{}] in the ticket catalog", metadata);
    val encTicket = encodeTicket(ticket);
    val data = ticketSerializationManager.serializeTicket(encTicket);
    val ttl = getTimeToLive(ticket);
    var insert = (Statement) null;
    if (inserting) {
        insert = QueryBuilder.insertInto(this.properties.getKeyspace(), metadata.getProperties().getStorageName()).value("id", QueryBuilder.literal(encTicket.getId())).value("data", QueryBuilder.literal(data)).value("type", QueryBuilder.literal(encTicket.getClass().getName())).usingTtl(ttl).build();
    } else {
        insert = QueryBuilder.update(this.properties.getKeyspace(), metadata.getProperties().getStorageName()).usingTtl(ttl).setColumn("data", QueryBuilder.literal(data)).whereColumn("id").isEqualTo(QueryBuilder.literal(encTicket.getId())).whereColumn("type").isEqualTo(QueryBuilder.literal(encTicket.getClass().getName())).build();
    }
    insert = insert.setConsistencyLevel(DefaultConsistencyLevel.valueOf(properties.getConsistencyLevel())).setSerialConsistencyLevel(DefaultConsistencyLevel.valueOf(properties.getSerialConsistencyLevel())).setTimeout(Beans.newDuration(properties.getTimeout()));
    LOGGER.trace("Attempting to locate ticket via query [{}]", insert);
    cassandraSessionFactory.getCqlTemplate().execute(insert);
    LOGGER.debug("Added ticket [{}]", encTicket.getId());
}
Also used : lombok.val(lombok.val) Statement(com.datastax.oss.driver.api.core.cql.Statement)

Example 2 with Statement

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

the class CassandraBaseTimeseriesLatestDao method deleteLatest.

private ListenableFuture<Void> deleteLatest(TenantId tenantId, EntityId entityId, String key) {
    Statement delete = QueryBuilder.deleteFrom(ModelConstants.TS_KV_LATEST_CF).whereColumn(ModelConstants.ENTITY_TYPE_COLUMN).isEqualTo(literal(entityId.getEntityType().name())).whereColumn(ModelConstants.ENTITY_ID_COLUMN).isEqualTo(literal(entityId.getId())).whereColumn(ModelConstants.KEY_COLUMN).isEqualTo(literal(key)).build();
    log.debug("Remove request: {}", delete.toString());
    return getFuture(executeAsyncWrite(tenantId, delete), rs -> null);
}
Also used : BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement)

Example 3 with Statement

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

the class CassandraDbHelper method dumpCfIfExists.

public static Path dumpCfIfExists(KeyspaceMetadata ks, GuavaSession session, String cfName, String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception {
    if (ks.getTable(cfName) != null) {
        Path dumpFile = Files.createTempFile(dumpPrefix, null);
        Files.deleteIfExists(dumpFile);
        CSVFormat csvFormat = CSV_DUMP_FORMAT;
        if (printHeader) {
            csvFormat = csvFormat.withHeader(columns);
        }
        try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(dumpFile), csvFormat)) {
            Statement stmt = SimpleStatement.newInstance("SELECT * FROM " + cfName);
            stmt.setPageSize(1000);
            ResultSet rs = session.execute(stmt);
            Iterator<Row> iter = rs.iterator();
            while (iter.hasNext()) {
                Row row = iter.next();
                if (row != null) {
                    dumpRow(row, columns, defaultValues, csvPrinter);
                }
            }
        }
        return dumpFile;
    } else {
        return null;
    }
}
Also used : Path(java.nio.file.Path) CSVPrinter(org.apache.commons.csv.CSVPrinter) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CSVFormat(org.apache.commons.csv.CSVFormat) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 4 with Statement

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

the class CassandraToSqlTable method migrateToSql.

public void migrateToSql(GuavaSession session, Connection conn) throws SQLException {
    log.info("[{}] Migrating data from cassandra '{}' Column Family to '{}' SQL table...", this.sqlTableName, this.cassandraCf, this.sqlTableName);
    DatabaseMetaData metadata = conn.getMetaData();
    java.sql.ResultSet resultSet = metadata.getColumns(null, null, this.sqlTableName, null);
    while (resultSet.next()) {
        String name = resultSet.getString("COLUMN_NAME");
        int sqlType = resultSet.getInt("DATA_TYPE");
        int size = resultSet.getInt("COLUMN_SIZE");
        CassandraToSqlColumn column = this.getColumn(name);
        column.setSize(size);
        column.setSqlType(sqlType);
    }
    this.sqlInsertStatement = createSqlInsertStatement(conn);
    Statement cassandraSelectStatement = createCassandraSelectStatement();
    cassandraSelectStatement.setPageSize(100);
    ResultSet rs = session.execute(cassandraSelectStatement);
    Iterator<Row> iter = rs.iterator();
    int rowCounter = 0;
    List<CassandraToSqlColumnData[]> batchData;
    boolean hasNext;
    do {
        batchData = this.extractBatchData(iter);
        hasNext = batchData.size() == this.batchSize;
        this.batchInsert(batchData, conn);
        rowCounter += batchData.size();
        log.info("[{}] {} records migrated so far...", this.sqlTableName, rowCounter);
    } while (hasNext);
    this.sqlInsertStatement.close();
    log.info("[{}] {} total records migrated.", this.sqlTableName, rowCounter);
    log.info("[{}] Finished migration data from cassandra '{}' Column Family to '{}' SQL table.", this.sqlTableName, this.cassandraCf, this.sqlTableName);
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) PreparedStatement(java.sql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

Statement (com.datastax.oss.driver.api.core.cql.Statement)4 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 Path (java.nio.file.Path)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 lombok.val (lombok.val)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVPrinter (org.apache.commons.csv.CSVPrinter)1