Search in sources :

Example 1 with SchemaStatement

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);
        }
    }
}
Also used : Alter(com.datastax.driver.core.schemabuilder.Alter) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SchemaStatement

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);
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement)

Example 3 with SchemaStatement

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);
    }
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 4 with SchemaStatement

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);
    }
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 5 with SchemaStatement

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());
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

SchemaStatement (com.datastax.driver.core.schemabuilder.SchemaStatement)8 ExecutionException (com.scalar.db.exception.storage.ExecutionException)3 Alter (com.datastax.driver.core.schemabuilder.Alter)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Test (org.junit.jupiter.api.Test)2 AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)1 Cluster (com.datastax.driver.core.Cluster)1 DataType (com.datastax.driver.core.DataType)1 IndexMetadata (com.datastax.driver.core.IndexMetadata)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 Session (com.datastax.driver.core.Session)1 Create (com.datastax.driver.core.schemabuilder.Create)1 SchemaBuilder (com.datastax.driver.core.schemabuilder.SchemaBuilder)1 Function (com.datastax.oss.driver.shaded.guava.common.base.Function)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1