private String getCreateTaskLookupTableStatement() { return SchemaBuilder.createTable(config.getCassandraKeyspace(), TABLE_TASK_LOOKUP) .ifNotExists() .addPartitionKey(TASK_ID_KEY, DataType.uuid()) .addColumn(WORKFLOW_ID_KEY, DataType.uuid()) .getQueryString(); }
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") public void should_drop_keyspace() throws Exception { // When SchemaStatement statement = dropKeyspace("test"); // Then assertThat(statement.getQueryString()).isEqualTo("DROP KEYSPACE test"); }
private CompactionOptions<?> getCompaction(boolean appendHeavyReadLight) { return appendHeavyReadLight ? SchemaBuilder.sizedTieredStategy().minThreshold(4).maxThreshold(32) : SchemaBuilder.leveledStrategy(); }
@Test(groups = "unit") public void should_create_table_with_udt_partition_key() throws Exception { // When SchemaStatement statement = createTable("test").addUDTPartitionKey("u", frozen("user")); // Then assertThat(statement.getQueryString()) .isEqualTo("\n\tCREATE TABLE test(\n\t\t" + "u frozen<user>,\n\t\t" + "PRIMARY KEY(u))"); }
@Test(groups = "unit") public void should_create_key_value_UDT_map_column() throws Exception { // When SchemaStatement statement = createType("ks", "myType") .addColumn("col1", DataType.text()) .addUDTMapColumn("my_udt", frozen("coords"), frozen("address")); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tCREATE TYPE ks.myType(\n\t\t" + "col1 text,\n\t\t" + "my_udt map<frozen<coords>, frozen<address>>)"); }
.execute(SchemaBuilder.createKeyspace(keyspace).with().replication(replicationOptions)); SchemaBuilder.createType("ztype") .addColumn("c", DataType.text()) .addColumn("a", DataType.cint())); session.execute(SchemaBuilder.createType("xtype").addColumn("d", DataType.text())); SchemaBuilder.createType("ctype") .addColumn("\"Z\"", ks.getUserType("ztype").copy(true)) .addColumn("x", ks.getUserType("xtype").copy(true))); session.execute(SchemaBuilder.createType("btype").addColumn("a", DataType.text())); SchemaBuilder.createType("atype").addColumn("c", ks.getUserType("ctype").copy(true))); SchemaBuilder.createTable("ztable") .addPartitionKey("zkey", DataType.text()) .addColumn("a", ks.getUserType("atype").copy(true)) .withOptions() .compactionOptions(SchemaBuilder.leveledStrategy().ssTableSizeInMB(95))); } else { SchemaBuilder.createTable("ztable") .addPartitionKey("zkey", DataType.text()) .addColumn("a", DataType.cint()) .withOptions() .compactionOptions(SchemaBuilder.leveledStrategy().ssTableSizeInMB(95)));
createTable("test") .addPartitionKey("id", DataType.bigint()) .addClusteringColumn("col1", DataType.uuid()) .caching(Caching.ROWS_ONLY) .comment("This is a comment") .compactionOptions(leveledStrategy().ssTableSizeInMB(160)) .compressionOptions(lz4()) .dcLocalReadRepairChance(0.21) .defaultTimeToLive(100) .readRepairChance(0.05) .replicateOnWrite(true) .speculativeRetry(always()) .cdc(true);
@Test(groups = "unit") public void should_create_table_with_speculative_retry_none() throws Exception { // When SchemaStatement statement = createTable("test") .addPartitionKey("id", DataType.bigint()) .addColumn("name", DataType.text()) .withOptions() .speculativeRetry(noSpeculativeRetry()); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tCREATE TABLE test(\n\t\t" + "id bigint,\n\t\t" + "name text,\n\t\t" + "PRIMARY KEY(id))\n\t" + "WITH speculative_retry = 'NONE'"); }
alterTable("test") .withOptions() .bloomFilterFPChance(0.01) .caching(Caching.ROWS_ONLY) .comment("This is a comment") .compactionOptions(leveledStrategy().ssTableSizeInMB(160)) .compressionOptions(lz4()) .dcLocalReadRepairChance(0.21) .defaultTimeToLive(100) .replicateOnWrite(true) .readRepairChance(0.42) .speculativeRetry(always()) .cdc(true); alterTable("test").withOptions().caching(KeyCaching.NONE, rows(100));
@Test(groups = "short") public void should_drop_a_table() { // Create a table session() .execute( SchemaBuilder.createTable("ks", "DropTable").addPartitionKey("a", DataType.cint())); // Drop the table session().execute(SchemaBuilder.dropTable("ks", "DropTable")); session().execute(SchemaBuilder.dropTable("DropTable").ifExists()); ResultSet rows = session() .execute( "SELECT columnfamily_name " + "FROM system.schema_columnfamilies " + "WHERE keyspace_name='ks' AND columnfamily_name='droptable'"); if (rows.iterator().hasNext()) { fail("This table should have been deleted"); } }
public void dropTable(CassandraSessionPool.Session session) { session.execute(SchemaBuilder.dropTable(this.table).ifExists()); }
session() .execute( createType(udt).addColumn("first", DataType.text()).addColumn("last", DataType.text())); UserType userType = cluster().getMetadata().getKeyspace(keyspace).getUserType(udt); assertThat(userType).isNotNull(); createTable(table) .addPartitionKey("k", DataType.text()) .addUDTColumn("u", udtLiteral(udt)));
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); } }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_fail_when_negative_rows_per_partition() throws Exception { createTable("test") .addPartitionKey("id", DataType.bigint()) .addColumn("name", DataType.text()) .withOptions() .caching(KeyCaching.ALL, rows(-3)); }
@Test(groups = "unit", expectedExceptions = IllegalStateException.class) public void should_fail_when_both_caching_versions() throws Exception { createTable("test") .addPartitionKey("id", DataType.bigint()) .addColumn("name", DataType.text()) .withOptions() .caching(Caching.KEYS_ONLY) .caching(KeyCaching.ALL, allRows()) .getQueryString(); }
.execute(SchemaBuilder.createType("MyUDT").ifNotExists().addColumn("x", DataType.cint())); UDTType myUDT = UDTType.frozen("MyUDT"); session() .execute( SchemaBuilder.createTable("ks", "CreateTable") .ifNotExists() .addPartitionKey("a", DataType.cint())
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_throw_exception_if_tombstone_threshold_negative() throws Exception { sizedTieredStategy().bucketLow(0.5).bucketHigh(1.2).coldReadsRatioToOmit(-1.0).build(); } }
private static CompressionOptions compressionOptions(final Configuration configuration) { if (!configuration.get(CF_COMPRESSION)) { // No compression return noCompression(); } return Match(configuration.get(CF_COMPRESSION_TYPE)).of( Case($("LZ4Compressor"), lz4()), Case($("SnappyCompressor"), snappy()), Case($("DeflateCompressor"), deflate())) .withChunkLengthInKb(configuration.get(CF_COMPRESSION_BLOCK_SIZE)); }
private static CompactionOptions<?> compactionOptions(final Configuration configuration) { if (!configuration.has(COMPACTION_STRATEGY)) { return null; } final CompactionOptions<?> compactionOptions = Match(configuration.get(COMPACTION_STRATEGY)) .of( Case($("SizeTieredCompactionStrategy"), sizedTieredStategy()), Case($("DateTieredCompactionStrategy"), dateTieredStrategy()), Case($("LeveledCompactionStrategy"), leveledStrategy())); Array.of(configuration.get(COMPACTION_OPTIONS)) .grouped(2) .forEach(keyValue -> compactionOptions.freeformOption(keyValue.get(0), keyValue.get(1))); return compactionOptions; }