Search in sources :

Example 1 with CreateTable

use of com.datastax.oss.driver.api.querybuilder.schema.CreateTable in project automatiko-engine by automatiko-io.

the class CassandraProcessInstances method createTable.

protected void createTable() {
    if (config.createKeyspace().orElse(true)) {
        CreateKeyspace createKs = createKeyspace(config.keyspace().orElse("automatiko")).ifNotExists().withSimpleStrategy(1);
        cqlSession.execute(createKs.build());
    }
    CreateTable createTable = SchemaBuilder.createTable(config.keyspace().orElse("automatiko"), tableName).ifNotExists().withPartitionKey(INSTANCE_ID_FIELD, DataTypes.TEXT).withColumn(STATUS_FIELD, DataTypes.INT).withColumn(CONTENT_FIELD, DataTypes.BLOB).withColumn(TAGS_FIELD, DataTypes.setOf(DataTypes.TEXT)).withColumn(VERSION_FIELD, DataTypes.BIGINT);
    cqlSession.execute(createTable.build());
    CreateIndex index = SchemaBuilder.createIndex(tableName + "_STATUS_IDX").ifNotExists().onTable(config.keyspace().orElse("automatiko"), tableName).andColumn(STATUS_FIELD);
    cqlSession.execute(index.build());
}
Also used : CreateIndex(com.datastax.oss.driver.api.querybuilder.schema.CreateIndex) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) CreateKeyspace(com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace)

Example 2 with CreateTable

use of com.datastax.oss.driver.api.querybuilder.schema.CreateTable in project automatiko-engine by automatiko-io.

the class CassandraJobService method createTable.

protected void createTable() {
    if (config.createKeyspace().orElse(true)) {
        CreateKeyspace createKs = createKeyspace(config.keyspace().orElse("automatiko")).ifNotExists().withSimpleStrategy(1);
        cqlSession.execute(createKs.build());
    }
    CreateTable createTable = SchemaBuilder.createTable(config.keyspace().orElse("automatiko"), tableName).ifNotExists().withPartitionKey(INSTANCE_ID_FIELD, DataTypes.TEXT).withColumn(FIRE_AT_FIELD, DataTypes.BIGINT).withColumn(OWNER_INSTANCE_ID_FIELD, DataTypes.TEXT).withColumn(OWNER_DEF_ID_FIELD, DataTypes.TEXT).withColumn(TRIGGER_TYPE_FIELD, DataTypes.TEXT).withColumn(STATUS_FIELD, DataTypes.TEXT).withColumn(FIRE_LIMIT_FIELD, DataTypes.INT).withColumn(REPEAT_INTERVAL_FIELD, DataTypes.BIGINT).withColumn(EXPRESSION_FIELD, DataTypes.TEXT);
    cqlSession.execute(createTable.build());
    CreateIndex index = SchemaBuilder.createIndex(tableName + "_IDX").ifNotExists().onTable(config.keyspace().orElse("automatiko"), tableName).andColumn(FIRE_AT_FIELD);
    cqlSession.execute(index.build());
}
Also used : CreateIndex(com.datastax.oss.driver.api.querybuilder.schema.CreateIndex) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) CreateKeyspace(com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace)

Example 3 with CreateTable

use of com.datastax.oss.driver.api.querybuilder.schema.CreateTable in project geowave by locationtech.

the class CassandraOperations method addOptions.

private CreateTable addOptions(final CreateTable create) {
    final Iterator<String[]> validOptions = options.getTableOptions().entrySet().stream().map(e -> new String[] { e.getKey(), e.getValue() }).iterator();
    CreateTable retVal = create;
    boolean addCompaction = true;
    boolean addGcGraceSeconds = true;
    while (validOptions.hasNext()) {
        final String[] option = validOptions.next();
        final String key = option[0].trim();
        final String valueStr = option[1].trim();
        Object value;
        if (valueStr.startsWith("{")) {
            try {
                value = new ObjectMapper().readValue(valueStr, HashMap.class);
            } catch (final IOException e) {
                LOGGER.warn("Unable to convert '" + valueStr + "' to a JSON map for cassandra table creation", e);
                value = valueStr;
            }
        } else {
            value = valueStr;
        }
        if ("compaction".equals(key)) {
            addCompaction = false;
            LOGGER.info("Found compaction in general table options, ignoring --compactionStrategy option.");
        } else if ("gc_grace_seconds".equals(key)) {
            addGcGraceSeconds = false;
            LOGGER.info("Found gc_grace_seconds in general table options, ignoring --gcGraceSeconds option.");
        }
        retVal = (CreateTable) retVal.withOption(key, value);
    }
    if (addCompaction) {
        retVal = (CreateTable) retVal.withCompaction(options.getCompactionStrategy());
    }
    if (addGcGraceSeconds) {
        retVal = (CreateTable) retVal.withGcGraceSeconds(options.getGcGraceSeconds());
    }
    return retVal;
}
Also used : CassandraRow(org.locationtech.geowave.datastore.cassandra.CassandraRow) QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) CassandraOptions(org.locationtech.geowave.datastore.cassandra.config.CassandraOptions) RecordReaderParams(org.locationtech.geowave.mapreduce.splits.RecordReaderParams) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) LoggerFactory(org.slf4j.LoggerFactory) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) RowWriter(org.locationtech.geowave.core.store.operations.RowWriter) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) ByteBuffer(java.nio.ByteBuffer) KeyspaceStatePool(org.locationtech.geowave.datastore.cassandra.util.KeyspaceStatePool) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) SessionPool(org.locationtech.geowave.datastore.cassandra.util.SessionPool) Drop(com.datastax.oss.driver.api.querybuilder.schema.Drop) Duration(java.time.Duration) Map(java.util.Map) ByteArrayRange(org.locationtech.geowave.core.index.ByteArrayRange) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) Select(com.datastax.oss.driver.api.querybuilder.select.Select) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) SelectFrom(com.datastax.oss.driver.api.querybuilder.select.SelectFrom) RowReader(org.locationtech.geowave.core.store.operations.RowReader) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) KeyspaceState(org.locationtech.geowave.datastore.cassandra.util.KeyspaceStatePool.KeyspaceState) Sets(com.google.common.collect.Sets) SchemaBuilder(com.datastax.oss.driver.api.querybuilder.SchemaBuilder) List(java.util.List) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) Stream(java.util.stream.Stream) Optional(java.util.Optional) CassandraField(org.locationtech.geowave.datastore.cassandra.CassandraRow.CassandraField) ByteArray(org.locationtech.geowave.core.index.ByteArray) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) StringUtils(org.locationtech.geowave.core.index.StringUtils) DataIndexUtils(org.locationtech.geowave.core.store.base.dataidx.DataIndexUtils) GeoWaveRowIteratorTransformer(org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer) HashMap(java.util.HashMap) RowDeleter(org.locationtech.geowave.core.store.operations.RowDeleter) Function(java.util.function.Function) RowReaderWrapper(org.locationtech.geowave.core.store.operations.RowReaderWrapper) BaseDataStoreOptions(org.locationtech.geowave.core.store.BaseDataStoreOptions) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) MetadataType(org.locationtech.geowave.core.store.operations.MetadataType) MapReduceDataStoreOperations(org.locationtech.geowave.mapreduce.MapReduceDataStoreOperations) AbstractGeoWavePersistence(org.locationtech.geowave.core.store.metadata.AbstractGeoWavePersistence) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ReaderParams(org.locationtech.geowave.core.store.operations.ReaderParams) CreateTableStart(com.datastax.oss.driver.api.querybuilder.schema.CreateTableStart) ClientVisibilityFilter(org.locationtech.geowave.core.store.query.filter.ClientVisibilityFilter) Index(org.locationtech.geowave.core.store.api.Index) Row(com.datastax.oss.driver.api.core.cql.Row) ExecutorService(java.util.concurrent.ExecutorService) CassandraRequiredOptions(org.locationtech.geowave.datastore.cassandra.config.CassandraRequiredOptions) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) MetadataDeleter(org.locationtech.geowave.core.store.operations.MetadataDeleter) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) Literal(com.datastax.oss.driver.api.querybuilder.Literal) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DataIndexReaderParams(org.locationtech.geowave.core.store.operations.DataIndexReaderParams) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) MetadataReader(org.locationtech.geowave.core.store.operations.MetadataReader) IOException(java.io.IOException) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) TypeCodecs(com.datastax.oss.driver.api.core.type.codec.TypeCodecs) CassandraUtils(org.locationtech.geowave.datastore.cassandra.util.CassandraUtils) ChronoUnit(java.time.temporal.ChronoUnit) MetadataWriter(org.locationtech.geowave.core.store.operations.MetadataWriter) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) ByteArrayUtils(org.locationtech.geowave.core.index.ByteArrayUtils) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Statement(com.datastax.oss.driver.api.core.cql.Statement) HashMap(java.util.HashMap) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 4 with CreateTable

use of com.datastax.oss.driver.api.querybuilder.schema.CreateTable in project stargate by stargate.

the class GeoTypeTest method createSchema.

@BeforeAll
public static void createSchema(CqlSession session, @TestKeyspace CqlIdentifier keyspaceId) {
    allTypes = generateAllTypes(keyspaceId);
    // Creating a new table for each type is too slow, use a single table with all possible types:
    CreateTable createTableQuery = createTable("test").withPartitionKey("k", DataTypes.INT);
    for (TypeSample<?> sample : allTypes) {
        createTableQuery = createTableQuery.withColumn(sample.columnName, sample.cqlType);
    }
    session.execute(createTableQuery.asCql());
}
Also used : CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with CreateTable

use of com.datastax.oss.driver.api.querybuilder.schema.CreateTable in project geowave by locationtech.

the class CassandraOperations method createTable.

private boolean createTable(final String indexName) {
    synchronized (CREATE_TABLE_MUTEX) {
        try {
            if (!indexExists(indexName)) {
                final String tableName = getCassandraSafeName(indexName);
                CreateTable create = addOptions(CassandraField.GW_PARTITION_ID_KEY.addPartitionKey(getCreateTable(tableName)));
                CassandraField[] fields = CassandraField.values();
                if (DataIndexUtils.isDataIndex(tableName)) {
                    fields = Arrays.stream(fields).filter(f -> f.isDataIndexColumn()).filter(f -> !f.isPartitionKey()).toArray(i -> new CassandraField[i]);
                }
                for (final CassandraField f : fields) {
                    create = f.addColumn(create);
                }
                executeCreateTable(create, tableName);
                return true;
            }
        } catch (final IOException e) {
            LOGGER.error("Unable to create table '" + indexName + "'", e);
        }
    }
    return false;
}
Also used : CassandraRow(org.locationtech.geowave.datastore.cassandra.CassandraRow) QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) Arrays(java.util.Arrays) CassandraOptions(org.locationtech.geowave.datastore.cassandra.config.CassandraOptions) RecordReaderParams(org.locationtech.geowave.mapreduce.splits.RecordReaderParams) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) LoggerFactory(org.slf4j.LoggerFactory) InternalAdapterStore(org.locationtech.geowave.core.store.adapter.InternalAdapterStore) RowWriter(org.locationtech.geowave.core.store.operations.RowWriter) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) ByteBuffer(java.nio.ByteBuffer) KeyspaceStatePool(org.locationtech.geowave.datastore.cassandra.util.KeyspaceStatePool) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) SessionPool(org.locationtech.geowave.datastore.cassandra.util.SessionPool) Drop(com.datastax.oss.driver.api.querybuilder.schema.Drop) Duration(java.time.Duration) Map(java.util.Map) ByteArrayRange(org.locationtech.geowave.core.index.ByteArrayRange) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) Select(com.datastax.oss.driver.api.querybuilder.select.Select) InternalDataAdapter(org.locationtech.geowave.core.store.adapter.InternalDataAdapter) SelectFrom(com.datastax.oss.driver.api.querybuilder.select.SelectFrom) RowReader(org.locationtech.geowave.core.store.operations.RowReader) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) KeyspaceState(org.locationtech.geowave.datastore.cassandra.util.KeyspaceStatePool.KeyspaceState) Sets(com.google.common.collect.Sets) SchemaBuilder(com.datastax.oss.driver.api.querybuilder.SchemaBuilder) List(java.util.List) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) Stream(java.util.stream.Stream) Optional(java.util.Optional) CassandraField(org.locationtech.geowave.datastore.cassandra.CassandraRow.CassandraField) ByteArray(org.locationtech.geowave.core.index.ByteArray) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) StringUtils(org.locationtech.geowave.core.index.StringUtils) DataIndexUtils(org.locationtech.geowave.core.store.base.dataidx.DataIndexUtils) GeoWaveRowIteratorTransformer(org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer) HashMap(java.util.HashMap) RowDeleter(org.locationtech.geowave.core.store.operations.RowDeleter) Function(java.util.function.Function) RowReaderWrapper(org.locationtech.geowave.core.store.operations.RowReaderWrapper) BaseDataStoreOptions(org.locationtech.geowave.core.store.BaseDataStoreOptions) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) MetadataType(org.locationtech.geowave.core.store.operations.MetadataType) MapReduceDataStoreOperations(org.locationtech.geowave.mapreduce.MapReduceDataStoreOperations) AbstractGeoWavePersistence(org.locationtech.geowave.core.store.metadata.AbstractGeoWavePersistence) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ReaderParams(org.locationtech.geowave.core.store.operations.ReaderParams) CreateTableStart(com.datastax.oss.driver.api.querybuilder.schema.CreateTableStart) ClientVisibilityFilter(org.locationtech.geowave.core.store.query.filter.ClientVisibilityFilter) Index(org.locationtech.geowave.core.store.api.Index) Row(com.datastax.oss.driver.api.core.cql.Row) ExecutorService(java.util.concurrent.ExecutorService) CassandraRequiredOptions(org.locationtech.geowave.datastore.cassandra.config.CassandraRequiredOptions) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) MetadataDeleter(org.locationtech.geowave.core.store.operations.MetadataDeleter) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) Literal(com.datastax.oss.driver.api.querybuilder.Literal) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DataIndexReaderParams(org.locationtech.geowave.core.store.operations.DataIndexReaderParams) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) MetadataReader(org.locationtech.geowave.core.store.operations.MetadataReader) IOException(java.io.IOException) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) PersistentAdapterStore(org.locationtech.geowave.core.store.adapter.PersistentAdapterStore) TypeCodecs(com.datastax.oss.driver.api.core.type.codec.TypeCodecs) CassandraUtils(org.locationtech.geowave.datastore.cassandra.util.CassandraUtils) ChronoUnit(java.time.temporal.ChronoUnit) MetadataWriter(org.locationtech.geowave.core.store.operations.MetadataWriter) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) ByteArrayUtils(org.locationtech.geowave.core.index.ByteArrayUtils) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Statement(com.datastax.oss.driver.api.core.cql.Statement) CassandraField(org.locationtech.geowave.datastore.cassandra.CassandraRow.CassandraField) CreateTable(com.datastax.oss.driver.api.querybuilder.schema.CreateTable) IOException(java.io.IOException)

Aggregations

CreateTable (com.datastax.oss.driver.api.querybuilder.schema.CreateTable)7 IOException (java.io.IOException)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)2 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 Statement (com.datastax.oss.driver.api.core.cql.Statement)2 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)2 DataTypes (com.datastax.oss.driver.api.core.type.DataTypes)2 TypeCodecs (com.datastax.oss.driver.api.core.type.codec.TypeCodecs)2 Literal (com.datastax.oss.driver.api.querybuilder.Literal)2 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)2 SchemaBuilder (com.datastax.oss.driver.api.querybuilder.SchemaBuilder)2 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)2 InsertInto (com.datastax.oss.driver.api.querybuilder.insert.InsertInto)2 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)2 CreateIndex (com.datastax.oss.driver.api.querybuilder.schema.CreateIndex)2 CreateKeyspace (com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace)2