Search in sources :

Example 1 with KeyspaceOptions

use of com.datastax.driver.core.schemabuilder.KeyspaceOptions in project kafka-connect-scylladb by scylladb.

the class ScyllaDbSinkConnector method start.

/**
 * Start the Connector. The method will establish a ScyllaDB session
 * and perform the following:<ol>
 * <li>Create a keyspace with the name under <code>scylladb.keyspace</code> configuration,
 * if <code>scylladb.keyspace.create.enabled</code> is set to true.
 * <li>Create a table for managing offsets in ScyllaDB with the name under
 * <code>scylladb.offset.storage.table</code> configuration,
 * if <code>scylladb.offset.storage.table.enable</code> is set to true.
 * </ol>
 */
@Override
public void start(Map<String, String> settings) {
    config = new ScyllaDbSinkConnectorConfig(settings);
    ScyllaDbSessionFactory sessionFactory = new ScyllaDbSessionFactory();
    try (ScyllaDbSession session = sessionFactory.newSession(config)) {
        if (config.keyspaceCreateEnabled) {
            KeyspaceOptions createKeyspace = SchemaBuilder.createKeyspace(config.keyspace).ifNotExists().with().durableWrites(true).replication(ImmutableMap.of("class", (Object) "SimpleStrategy", "replication_factor", config.keyspaceReplicationFactor));
            session.executeStatement(createKeyspace);
        }
        if (config.offsetEnabledInScyllaDB) {
            final Create createOffsetStorage = SchemaBuilder.createTable(config.keyspace, config.offsetStorageTable).addPartitionKey("topic", DataType.varchar()).addPartitionKey("partition", DataType.cint()).addColumn("offset", DataType.bigint()).ifNotExists();
            session.executeStatement(createOffsetStorage);
        }
    } catch (IOException ex) {
        // TODO: improve error handling for both cases
        throw new ConnectException("Failed to start the Connector.", ex);
    }
}
Also used : KeyspaceOptions(com.datastax.driver.core.schemabuilder.KeyspaceOptions) Create(com.datastax.driver.core.schemabuilder.Create) IOException(java.io.IOException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 2 with KeyspaceOptions

use of com.datastax.driver.core.schemabuilder.KeyspaceOptions in project scalardb by scalar-labs.

the class CassandraAdminTest method createNamespace_UseNetworkTopologyStrategy_ShouldExecuteCreateKeyspaceStatement.

@Test
public void createNamespace_UseNetworkTopologyStrategy_ShouldExecuteCreateKeyspaceStatement() throws ExecutionException {
    // Arrange
    String namespace = "sample_ns";
    Map<String, String> options = new HashMap<>();
    options.put(CassandraAdmin.REPLICATION_STRATEGY, ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.toString());
    options.put(CassandraAdmin.REPLICATION_FACTOR, "5");
    // Act
    cassandraAdmin.createNamespace(namespace, options);
    // Assert
    Map<String, Object> replicationOptions = new LinkedHashMap<>();
    replicationOptions.put("class", ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.toString());
    replicationOptions.put("dc1", "5");
    KeyspaceOptions query = SchemaBuilder.createKeyspace(namespace).with().replication(replicationOptions);
    verify(cassandraSession).execute(query.getQueryString());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KeyspaceOptions(com.datastax.driver.core.schemabuilder.KeyspaceOptions) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 3 with KeyspaceOptions

use of com.datastax.driver.core.schemabuilder.KeyspaceOptions in project scalardb by scalar-labs.

the class CassandraAdminTest method createNamespace_UseSimpleStrategy_ShouldExecuteCreateKeyspaceStatement.

@Test
public void createNamespace_UseSimpleStrategy_ShouldExecuteCreateKeyspaceStatement() throws ExecutionException {
    // Arrange
    String namespace = "sample_ns";
    Map<String, String> options = new HashMap<>();
    options.put(CassandraAdmin.REPLICATION_STRATEGY, ReplicationStrategy.SIMPLE_STRATEGY.toString());
    options.put(CassandraAdmin.REPLICATION_FACTOR, "3");
    // Act
    cassandraAdmin.createNamespace(namespace, options);
    // Assert
    Map<String, Object> replicationOptions = new LinkedHashMap<>();
    replicationOptions.put("class", ReplicationStrategy.SIMPLE_STRATEGY.toString());
    replicationOptions.put("replication_factor", "3");
    KeyspaceOptions query = SchemaBuilder.createKeyspace(namespace).with().replication(replicationOptions);
    verify(cassandraSession).execute(query.getQueryString());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KeyspaceOptions(com.datastax.driver.core.schemabuilder.KeyspaceOptions) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 4 with KeyspaceOptions

use of com.datastax.driver.core.schemabuilder.KeyspaceOptions in project scalardb by scalar-labs.

the class CassandraAdminTest method createNamespace_WithoutReplicationStrategyNorReplicationFactor_ShouldExecuteCreateKeyspaceStatementWithSimpleStrategy.

@Test
public void createNamespace_WithoutReplicationStrategyNorReplicationFactor_ShouldExecuteCreateKeyspaceStatementWithSimpleStrategy() throws ExecutionException {
    // Arrange
    String namespace = "sample_ns";
    Map<String, String> options = new HashMap<>();
    // Act
    cassandraAdmin.createNamespace(namespace, options);
    // Assert
    Map<String, Object> replicationOptions = new LinkedHashMap<>();
    replicationOptions.put("class", ReplicationStrategy.SIMPLE_STRATEGY.toString());
    replicationOptions.put("replication_factor", "1");
    KeyspaceOptions query = SchemaBuilder.createKeyspace(namespace).with().replication(replicationOptions);
    verify(cassandraSession).execute(query.getQueryString());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) KeyspaceOptions(com.datastax.driver.core.schemabuilder.KeyspaceOptions) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 5 with KeyspaceOptions

use of com.datastax.driver.core.schemabuilder.KeyspaceOptions in project scalardb by scalar-labs.

the class CassandraAdminTest method createNamespace_WithReservedKeywords_ShouldExecuteCreateKeyspaceStatementProperly.

@Test
public void createNamespace_WithReservedKeywords_ShouldExecuteCreateKeyspaceStatementProperly() throws ExecutionException {
    // Arrange
    String namespace = "keyspace";
    Map<String, String> options = Collections.emptyMap();
    // Act
    cassandraAdmin.createNamespace(namespace, options);
    // Assert
    Map<String, Object> replicationOptions = new LinkedHashMap<>();
    replicationOptions.put("class", ReplicationStrategy.SIMPLE_STRATEGY.toString());
    replicationOptions.put("replication_factor", "1");
    KeyspaceOptions query = SchemaBuilder.createKeyspace(quote(namespace)).with().replication(replicationOptions);
    verify(cassandraSession).execute(query.getQueryString());
}
Also used : KeyspaceOptions(com.datastax.driver.core.schemabuilder.KeyspaceOptions) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Aggregations

KeyspaceOptions (com.datastax.driver.core.schemabuilder.KeyspaceOptions)5 LinkedHashMap (java.util.LinkedHashMap)4 Test (org.junit.jupiter.api.Test)4 HashMap (java.util.HashMap)3 Create (com.datastax.driver.core.schemabuilder.Create)1 IOException (java.io.IOException)1 ConnectException (org.apache.kafka.connect.errors.ConnectException)1