use of com.datastax.driver.core.schemabuilder.SchemaStatement in project sdc by onap.
the class SdcSchemaBuilder method alterTable.
/**
* check if there are new columns that were added to definition but don't exist in DB
*
* @param session
* @param existingTablesMetadata
* @param tableDescription
* @param tableName
* @param columnDescription
*/
private static void alterTable(Session session, Map<String, List<String>> existingTablesMetadata, ITableDescription tableDescription, String tableName, Map<String, ImmutablePair<DataType, Boolean>> columnDescription) {
List<String> definedTableColumns = existingTablesMetadata.get(tableName);
// add column to casandra if was added to table definition
for (Map.Entry<String, ImmutablePair<DataType, Boolean>> column : columnDescription.entrySet()) {
String columnName = column.getKey();
if (!definedTableColumns.contains(columnName.toLowerCase())) {
log.info("Adding new column {} to the table {}", columnName, tableName);
Alter alter = SchemaBuilder.alterTable(tableDescription.getKeyspace(), tableDescription.getTableName());
SchemaStatement addColumn = alter.addColumn(columnName).type(column.getValue().getLeft());
log.trace("executing :{}", addColumn);
session.execute(addColumn);
}
}
}
use of com.datastax.driver.core.schemabuilder.SchemaStatement in project incubator-hugegraph by apache.
the class CassandraTable method createIndex.
protected void createIndex(CassandraSessionPool.Session session, String indexLabel, HugeKeys column) {
String indexName = joinTableName(this.table(), indexLabel);
SchemaStatement index = SchemaBuilder.createIndex(indexName).ifNotExists().onTable(this.table()).andColumn(formatKey(column));
LOG.debug("Create index: {}", index);
session.execute(index);
}
use of com.datastax.driver.core.schemabuilder.SchemaStatement in project scalardb by scalar-labs.
the class CassandraAdmin method dropIndex.
@Override
public void dropIndex(String namespace, String table, String columnName) throws ExecutionException {
String indexName = getIndexName(table, columnName);
SchemaStatement dropIndex = SchemaBuilder.dropIndex(quoteIfNecessary(namespace), indexName);
try {
clusterManager.getSession().execute(dropIndex.getQueryString());
} catch (RuntimeException e) {
throw new ExecutionException(String.format("dropping the secondary index for %s.%s.%s failed", namespace, table, columnName), e);
}
}
use of com.datastax.driver.core.schemabuilder.SchemaStatement in project scalardb by scalar-labs.
the class CassandraAdmin method createIndex.
@Override
public void createIndex(String namespace, String table, String columnName, Map<String, String> options) throws ExecutionException {
String indexName = getIndexName(table, columnName);
SchemaStatement createIndex = SchemaBuilder.createIndex(indexName).onTable(quoteIfNecessary(namespace), quoteIfNecessary(table)).andColumn(quoteIfNecessary(columnName));
try {
clusterManager.getSession().execute(createIndex.getQueryString());
} catch (RuntimeException e) {
throw new ExecutionException(String.format("creating the secondary index for %s.%s.%s failed", namespace, table, columnName), e);
}
}
use of com.datastax.driver.core.schemabuilder.SchemaStatement in project scalardb by scalar-labs.
the class CassandraAdminTest method createSecondaryIndex_WithReservedKeywordsIndexesNames_ShouldCreateBothIndexes.
@Test
public void createSecondaryIndex_WithReservedKeywordsIndexesNames_ShouldCreateBothIndexes() throws ExecutionException {
// Arrange
String namespace = "sample_ns";
String table = "sample_table";
Set<String> indexes = new HashSet<>();
indexes.add("from");
indexes.add("to");
// Act
cassandraAdmin.createSecondaryIndexes(namespace, table, indexes, Collections.emptyMap());
// Assert
SchemaStatement c1IndexStatement = SchemaBuilder.createIndex(table + "_" + CassandraAdmin.INDEX_NAME_PREFIX + "_from").onTable(namespace, table).andColumn("\"from\"");
SchemaStatement c2IndexStatement = SchemaBuilder.createIndex(table + "_" + CassandraAdmin.INDEX_NAME_PREFIX + "_to").onTable(namespace, table).andColumn("\"to\"");
verify(cassandraSession).execute(c1IndexStatement.getQueryString());
verify(cassandraSession).execute(c2IndexStatement.getQueryString());
}
Aggregations