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());
}
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);
}
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;
}
}
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);
}
Aggregations