use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project rocketmq-externals by apache.
the class Updater method deleteRow.
private boolean deleteRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
DeleteSelection deleteSelection = QueryBuilder.deleteFrom(dbName, tableName);
Delete delete = null;
int count = 0;
for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
count++;
String fieldName = entry.getKey().getName();
FieldType fieldType = entry.getKey().getType();
Object fieldValue = entry.getValue()[1];
if (count == 1) {
delete = deleteSelection.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
} else {
delete = delete.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
}
}
boolean finishDelete = false;
SimpleStatement stmt = delete.build();
try {
while (!cqlSession.isClosed() && !finishDelete) {
ResultSet result = cqlSession.execute(stmt);
if (result.wasApplied()) {
log.info("delete from table success, executed query {}", delete);
return true;
}
finishDelete = true;
}
} catch (Exception e) {
log.error("delete from table error,{}", e);
}
return false;
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project rocketmq-externals by apache.
the class Updater method updateRow.
/**
* Since we have no way of getting the id of a record, and we cannot get the primary key list of a table,
* even we can it is not extensible. So we the result sql sentense would be like
* UPDATE dbName.tableName SET afterUpdateValues WHERE beforeUpdateValues.
*/
private Boolean updateRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
log.info("Updater.updateRow() get called ");
int count = 0;
InsertInto insert = QueryBuilder.insertInto(dbName, tableName);
RegularInsert regularInsert = null;
for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
count++;
String fieldName = entry.getKey().getName();
FieldType fieldType = entry.getKey().getType();
Object fieldValue = entry.getValue()[1];
if (count == 1) {
regularInsert = insert.value(fieldName, buildTerm(fieldType, fieldValue));
} else {
regularInsert = regularInsert.value(fieldName, buildTerm(fieldType, fieldValue));
}
}
SimpleStatement stmt;
boolean finishUpdate = false;
log.info("trying to execute sql query,{}", regularInsert.asCql());
try {
while (!cqlSession.isClosed() && !finishUpdate) {
stmt = regularInsert.build();
ResultSet result = cqlSession.execute(stmt);
if (result.wasApplied()) {
log.info("update table success, executed cql query {}", regularInsert.asCql());
return true;
}
finishUpdate = true;
}
} catch (Exception e) {
log.error("update table error,{}", e);
}
return false;
}
use of com.datastax.oss.driver.api.core.cql.SimpleStatement in project rocketmq-externals by apache.
the class Database method init.
public void init() {
Select selectFrom = QueryBuilder.selectFrom(SCHEMA_SYSTEM_SCHEMA, TABLE_COLUMNS).all().whereColumn(COLUMN_KEYSPACE_NAME).isEqualTo(QueryBuilder.literal(name));
SimpleStatement stmt;
boolean finishUpdate = false;
LOGGER.info("trying to execute sql query,{}", selectFrom.asCql());
ResultSet result = null;
try {
while (!cqlSession.isClosed() && !finishUpdate) {
stmt = selectFrom.build();
result = cqlSession.execute(stmt);
if (result.wasApplied()) {
LOGGER.info("query columns success, executed cql query {}", selectFrom.asCql());
}
finishUpdate = true;
}
for (Row row : result) {
String tableName = row.getString(COLUMN_TABLE_NAME);
String columnName = row.getString(COLUMN_COLUMN_NAME);
String columnType = row.getString(COLUMN_TYPE);
ColumnParser columnParser = ColumnParser.getColumnParser(columnType, columnType, GENERAL_CHARSET);
if (!tableWhiteList.contains(tableName)) {
continue;
}
if (!tableMap.containsKey(tableName)) {
addTable(tableName);
}
Table table = tableMap.get(tableName);
table.addCol(columnName);
table.addParser(columnParser);
table.addRawDataType(columnType);
table.setFilterMap(tableFilterMap.get(tableName));
}
} catch (Exception e) {
LOGGER.error("init cassandra Schema failure,{}", e);
}
}
Aggregations