private String getCreateKeyspaceStatement() { return SchemaBuilder.createKeyspace(config.getCassandraKeyspace()) .ifNotExists() .with() .replication(ImmutableMap.of("class", config.getReplicationStrategy(), config.getReplicationFactorKey(), config.getReplicationFactorValue())) .durableWrites(true) .getQueryString(); }
@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); } }
@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); } }
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(); } } }
@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") 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"); }
.execute(SchemaBuilder.createKeyspace(keyspace).with().replication(replicationOptions));
@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); } }
@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); } }
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(); } } }
@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") 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"); }
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); } }
Session initializeSession(final String keyspaceName) { final Session s = this.cluster.connect(); // if the keyspace already exists, just return the session if (this.cluster.getMetadata().getKeyspace(keyspaceName) != null) { return s; } final Configuration configuration = getStorageConfig(); // Setting replication strategy based on value reading from the configuration: either "SimpleStrategy" or "NetworkTopologyStrategy" final Map<String, Object> replication = Match(configuration.get(REPLICATION_STRATEGY)).of( Case($("SimpleStrategy"), strategy -> HashMap.<String, Object> of("class", strategy, "replication_factor", configuration.get(REPLICATION_FACTOR))), Case($("NetworkTopologyStrategy"), strategy -> HashMap.<String, Object> of("class", strategy) .merge(Array.of(configuration.get(REPLICATION_OPTIONS)) .grouped(2) .toMap(array -> Tuple.of(array.get(0), Integer.parseInt(array.get(1))))))) .toJavaMap(); s.execute(createKeyspace(keyspaceName) .ifNotExists() .with() .replication(replication)); return s; }
.execute(SchemaBuilder.createKeyspace(keyspace).with().replication(replicationOptions));