private String getCreateKeyspaceStatement() { return SchemaBuilder.createKeyspace(config.getCassandraKeyspace()) .ifNotExists() .with() .replication(ImmutableMap.of("class", config.getReplicationStrategy(), config.getReplicationFactorKey(), config.getReplicationFactorValue())) .durableWrites(true) .getQueryString(); }
/** * Add options for this ALTER KEYSPACE statement. * * @return the options of this ALTER KEYSPACE statement. */ public KeyspaceOptions with() { return new KeyspaceOptions(COMMAND, keyspaceName); } }
public KeyspaceOptions(String command, String keyspaceName) { validateNotEmpty(keyspaceName, "Keyspace name"); validateNotKeyWord( keyspaceName, String.format( "The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName)); this.command = command; this.keyspaceName = keyspaceName; }
@Test(groups = "unit") public void should_alter_keyspace_with_options() throws Exception { Map<String, Object> replicationOptions = new HashMap<String, Object>(); replicationOptions.put("class", "SimpleStrategy"); replicationOptions.put("replication_factor", 1); // When SchemaStatement statement = alterKeyspace("test").with().durableWrites(true).replication(replicationOptions); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tALTER KEYSPACE test" + "\n\tWITH\n\t\t" + "REPLICATION = {'replication_factor': 1, 'class': 'SimpleStrategy'}\n\t\t" + "AND DURABLE_WRITES = true"); }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void incorrect_replication_options() throws Exception { Map<String, Object> replicationOptions = new HashMap<String, Object>(); replicationOptions.put("class", 5); // When createKeyspace("test").with().replication(replicationOptions); } }
private void createKeyspaceAndTable() { Metadata metadata = client.cluster().getMetadata(); if (Objects.isNull(metadata.getKeyspace(config.getKeyspace()))) { LOGGER.info("Keyspace {} does not exist. Creating Keyspace", config.getKeyspace()); Map<String, Object> replication = new HashMap<>(); replication.put("class", "SimpleStrategy"); replication.put("replication_factor", 1); String createKeyspaceStmt = SchemaBuilder.createKeyspace(config.getKeyspace()).with() .replication(replication).getQueryString(); client.cluster().connect().execute(createKeyspaceStmt); } session = client.cluster().connect(config.getKeyspace()); KeyspaceMetadata ks = metadata.getKeyspace(config.getKeyspace()); TableMetadata tableMetadata = ks.getTable(config.getTable()); if (Objects.isNull(tableMetadata)) { LOGGER.info("Table {} does not exist in Keyspace {}. Creating Table", config.getTable(), config.getKeyspace()); String createTableStmt = SchemaBuilder.createTable(config.getTable()) .addPartitionKey(config.getPartitionKeyColumn(), DataType.varchar()) .addColumn(config.getColumn(), DataType.blob()).getQueryString(); session.execute(createTableStmt); } }
/** * Define the replication options for the statement. * * @param replication replication properties map * @return this {@code KeyspaceOptions} object */ public KeyspaceOptions replication(Map<String, Object> replication) { validateReplicationOptions(replication); this.replication = Optional.fromNullable(replication); return this; }
@Test(groups = "unit") public void should_create_keyspace_with_options() throws Exception { Map<String, Object> replicationOptions = new HashMap<String, Object>(); replicationOptions.put("class", "SimpleStrategy"); replicationOptions.put("replication_factor", 1); // When SchemaStatement statement = createKeyspace("test").with().durableWrites(true).replication(replicationOptions); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tCREATE KEYSPACE test" + "\n\tWITH\n\t\t" + "REPLICATION = {'replication_factor': 1, 'class': 'SimpleStrategy'}\n\t\t" + "AND DURABLE_WRITES = true"); }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void incorrect_replication_options() throws Exception { Map<String, Object> replicationOptions = new HashMap<String, Object>(); replicationOptions.put("class", 5); // When alterKeyspace("test").with().replication(replicationOptions); } }
/** * Define the replication options for the statement. * * @param replication replication properties map * @return this {@code KeyspaceOptions} object */ public KeyspaceOptions replication(Map<String, Object> replication) { validateReplicationOptions(replication); this.replication = Optional.fromNullable(replication); return this; }
public void initKeyspace() { // TODO consider exposing important keyspace options through commandline // such as understanding how to properly enable cassandra in production // - with data centers and snitch, for now because this is only creating // a keyspace "if not exists" a user can create a keyspace matching // their geowave namespace with any settings they want manually session.execute( SchemaBuilder.createKeyspace(gwNamespace).ifNotExists().with().replication( ImmutableMap.of( "class", "SimpleStrategy", "replication_factor", options.getReplicationFactor())).durableWrites(options.isDurableWrites())); }
protected void initKeyspace() { // Replication strategy: SimpleStrategy or NetworkTopologyStrategy String strategy = this.conf.get(CassandraOptions.CASSANDRA_STRATEGY); // Replication factor int factor = this.conf.get(CassandraOptions.CASSANDRA_REPLICATION); Map<String, Object> replication = new HashMap<>(); replication.putIfAbsent("class", strategy); replication.putIfAbsent("replication_factor", factor); Statement stmt = SchemaBuilder.createKeyspace(this.keyspace) .ifNotExists().with() .replication(replication); // Create keyspace with non-keyspace-session LOG.debug("Create keyspace: {}", stmt); Session session = this.cluster().connect(); try { session.execute(stmt); } finally { if (!session.isClosed()) { session.close(); } } }
public KeyspaceOptions(String command, String keyspaceName) { validateNotEmpty(keyspaceName, "Keyspace name"); validateNotKeyWord(keyspaceName, String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName)); this.command = command; this.keyspaceName = keyspaceName; }
/** * Add options for this CREATE KEYSPACE statement. * * @return the options of this CREATE KEYSPACE statement. */ public KeyspaceOptions with() { return new KeyspaceOptions(buildCommand(), keyspaceName); }
/** * Define the replication options for the statement. * * @param replication replication properties map * @return this {@code KeyspaceOptions} object */ public KeyspaceOptions replication(Map<String, Object> replication) { validateReplicationOptions(replication); this.replication = Optional.fromNullable(replication); return this; }
@Test(groups = "unit") public void should_alter_keyspace_with_options() throws Exception { Map<String, Object> replicationOptions = new HashMap<String, Object>(); replicationOptions.put("class", "SimpleStrategy"); replicationOptions.put("replication_factor", 1); // When SchemaStatement statement = alterKeyspace("test").with().durableWrites(true).replication(replicationOptions); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tALTER KEYSPACE test" + "\n\tWITH\n\t\t" + "REPLICATION = {'replication_factor': 1, 'class': 'SimpleStrategy'}\n\t\t" + "AND DURABLE_WRITES = true"); }
.execute(SchemaBuilder.createKeyspace(keyspace).with().replication(replicationOptions));
public KeyspaceOptions(String command, String keyspaceName) { validateNotEmpty(keyspaceName, "Keyspace name"); validateNotKeyWord(keyspaceName, String.format("The keyspace name '%s' is not allowed because it is a reserved keyword", keyspaceName)); this.command = command; this.keyspaceName = keyspaceName; }
/** * Add options for this ALTER KEYSPACE statement. * * @return the options of this ALTER KEYSPACE statement. */ public KeyspaceOptions with() { return new KeyspaceOptions(COMMAND, keyspaceName); }
/** * Define the replication options for the statement. * * @param replication replication properties map * @return this {@code KeyspaceOptions} object */ public KeyspaceOptions replication(Map<String, Object> replication) { validateReplicationOptions(replication); this.replication = Optional.fromNullable(replication); return this; }