Refine search
/** * Create index label table for vertex/edge * @param session DB session * @param table index table name */ private static void createIndexTable(CassandraSessionPool.Session session, String table) { Create tableBuilder = SchemaBuilder.createTable(table).ifNotExists(); tableBuilder.addPartitionKey(LABEL, DataType.cint()); tableBuilder.addColumn(ELEMENT_IDS, DataType.set(DataType.text())); session.execute(tableBuilder); }
private String getCreateWorkflowsTableStatement() { return SchemaBuilder.createTable(config.getCassandraKeyspace(), TABLE_WORKFLOWS) .ifNotExists() .addPartitionKey(WORKFLOW_ID_KEY, DataType.uuid()) .addPartitionKey(SHARD_ID_KEY, DataType.cint()) .addClusteringColumn(ENTITY_KEY, DataType.text()) .addClusteringColumn(TASK_ID_KEY, DataType.text()) .addColumn(PAYLOAD_KEY, DataType.text()) .addStaticColumn(TOTAL_TASKS_KEY, DataType.cint()) .addStaticColumn(TOTAL_PARTITIONS_KEY, DataType.cint()) .getQueryString(); }
/** * Start building a new CREATE TABLE statement. * * @param keyspaceName the name of the keyspace to be used. * @param tableName the name of the table to create. * @return an in-construction CREATE TABLE statement. */ public static Create createTable(String keyspaceName, String tableName) { return new Create(keyspaceName, tableName); }
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 static void createTableIfNotExists(final com.datastax.driver.core.Session session, final String table, final Logger log) { Create createTable = SchemaBuilder.createTable(table) .addPartitionKey(ID, DataType.varchar()) .addColumn(CREATED_AT, DataType.timestamp()) .addColumn(ACCESSED_AT, DataType.timestamp()) .addColumn(SAVED_AT, DataType.timestamp()) .addColumn(ATTRIBUTES, DataType.map(DataType.varchar(), DataType.varchar())) .ifNotExists(); Futures.addCallback(session.executeAsync(createTable), new FutureCallback<ResultSet>() { @Override public void onSuccess(final ResultSet result) { log.debug("Session table successfully created"); } @Override public void onFailure(final Throwable x) { log.error("Create session table resulted in exception", x); } }); }
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_fail_when_blank_clustering_order_column_provided() throws Exception { createTable("test") .addPartitionKey("id", DataType.bigint()) .addClusteringColumn("col1", DataType.uuid()) .addClusteringColumn("col2", DataType.uuid()) .addColumn("name", DataType.text()) .withOptions() .clusteringOrder("", Direction.DESC); }
@Test( groups = "unit", expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "The '\\[cluster\\]' columns can not be declared as clustering keys and simple columns at the same time") public void should_fail_if_same_clustering_and_simple_column() throws Exception { createTable("test") .addPartitionKey("pk", DataType.bigint()) .addClusteringColumn("cluster", DataType.bigint()) .addColumn("cluster", DataType.text()) .getQueryString(); }
@Test(groups = "unit") public void should_create_simple_table_with_map() throws Exception { // When SchemaStatement statement = createTable("test") .addPartitionKey("id", DataType.bigint()) .addColumn("friends", DataType.map(DataType.cint(), DataType.text())); // Then assertThat(statement.getQueryString()) .isEqualTo( "\n\tCREATE TABLE test(\n\t\t" + "id bigint,\n\t\t" + "friends map<int, text>,\n\t\t" + "PRIMARY KEY(id))"); }
.addColumn("c", DataType.text()) .addColumn("a", DataType.cint())); session.execute(SchemaBuilder.createType("xtype").addColumn("d", DataType.text())); .addPartitionKey("zkey", DataType.text()) .addColumn("a", ks.getUserType("atype").copy(true)) .withOptions() .compactionOptions(SchemaBuilder.leveledStrategy().ssTableSizeInMB(95))); } else { .addPartitionKey("zkey", DataType.text()) .addColumn("a", DataType.cint()) .withOptions() .compactionOptions(SchemaBuilder.leveledStrategy().ssTableSizeInMB(95))); .addPartitionKey("cid", DataType.uuid()) .addColumn("name", DataType.text()) .addColumn("age", DataType.cint()) .addColumn("birthday", DataType.date()) .addColumn("country", DataType.text())); .addPartitionKey("race_year", DataType.cint()) .addPartitionKey("race_name", DataType.text()) .addClusteringColumn("rank", DataType.cint()) .addColumn("cyclist_name", DataType.text())); assertThat(ks.exportAsString().trim()).isEqualTo(getExpectedCqlString());
@Test(groups = "short") @CassandraVersion("2.1.2") public void should_modify_table_metadata() { .execute( SchemaBuilder.createTable("ks", "TableMetadata") .addPartitionKey("a", DataType.cint()) .addPartitionKey("b", DataType.cint()) .addClusteringColumn("c", DataType.cint()) .addClusteringColumn("d", DataType.cint()) .withOptions() .compactStorage()); assertThat(row.getDouble("bloom_filter_fp_chance")).isEqualTo(0.42); assertThat(row.getString("caching")) .isEqualTo("{\"keys\":\"ALL\", \"rows_per_partition\":\"1\"}"); assertThat(row.getUUID("cf_id")).isNotNull(); assertThat(row.getString("column_aliases")).isEqualTo("[\"c\",\"d\"]"); assertThat(row.getString("comment")).isEqualTo("Useful comment"); assertThat(row.getString("compaction_strategy_class")) .isEqualTo("org.apache.cassandra.db.compaction.DateTieredCompactionStrategy");
value = "3.6", description = "Requires CASSANDRA-7423 introduced in Cassandra 3.6") @Test(groups = "short") public void should_support_setting_and_retrieving_udt_fields() { session() .execute( createType(udt).addColumn("first", DataType.text()).addColumn("last", DataType.text())); UserType userType = cluster().getMetadata().getKeyspace(keyspace).getUserType(udt); assertThat(userType).isNotNull(); .addPartitionKey("k", DataType.text()) .addUDTColumn("u", udtLiteral(udt))); .one(); assertThat(r.getString("u.first")).isEqualTo("Rick"); assertThat(r.getString("u.last")).isEqualTo("Jones");
@Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) public void should_fail_when_read_repair_chance_out_of_bound() throws Exception { createTable("test") .addPartitionKey("id", DataType.bigint()) .addColumn("name", DataType.text()) .withOptions() .readRepairChance(1.3); }
@Test(groups = "short") public void should_create_an_index() { .execute( SchemaBuilder.createTable("ks", "CreateIndex") .addPartitionKey("a", DataType.cint()) .addClusteringColumn("b", DataType.cint()) .addColumn("c", DataType.map(DataType.cint(), DataType.cint()))); verifyNextIndexDefinition(iterator, "ks_Index", "{}", "COMPOSITES", 0); verifyNextIndexDefinition(iterator, "ks_IndexOnMap", "{\"index_keys\":\"\"}", "COMPOSITES", 1); assertThat(iterator.hasNext()).isFalse();
@Test(groups = "short") public void should_add_and_drop_a_column() { // Create a table, add a column to it with an alter table statement and delete that column session() .execute( SchemaBuilder.createTable("ks", "DropColumn") .ifNotExists() .addPartitionKey("a", DataType.cint())); // Add and then drop a column session() .execute(SchemaBuilder.alterTable("ks", "DropColumn").addColumn("b").type(DataType.cint())); session().execute(SchemaBuilder.alterTable("ks", "DropColumn").dropColumn("b")); // Check that only column a exist ResultSet rows = session() .execute( "SELECT column_name, type, validator " + "FROM system.schema_columns " + "WHERE keyspace_name='ks' AND columnfamily_name='dropcolumn'"); Iterator<Row> iterator = rows.iterator(); verifyNextColumnDefinition( iterator, "a", "partition_key", "org.apache.cassandra.db.marshal.Int32Type"); assertThat(iterator.hasNext()).isFalse(); }
@Test(groups = "short") public void should_drop_an_index() { // Create a table session() .execute( SchemaBuilder.createTable("ks", "DropIndex") .addPartitionKey("a", DataType.cint()) .addClusteringColumn("b", DataType.cint())); // Create an index // Note: we have to pick a lower-case name because Cassandra uses the CamelCase index name at // creation // but a lowercase index name at deletion // See : https://issues.apache.org/jira/browse/CASSANDRA-8365 session() .execute(SchemaBuilder.createIndex("ks_index").onTable("ks", "DropIndex").andColumn("b")); // Verify that the PK index and the secondary indexes both exist assertThat(numberOfIndexedColumns()).isEqualTo(1); // Delete the index session().execute(SchemaBuilder.dropIndex("ks", "ks_index")); // Verify that only the PK index exists assertThat(numberOfIndexedColumns()).isEqualTo(0); }
@Test( groups = "unit", expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "The '\\[cluster\\]' columns can not be declared as clustering keys and static columns at the same time") public void should_fail_if_same_clustering_and_static_column() throws Exception { createTable("test") .addPartitionKey("pk", DataType.bigint()) .addClusteringColumn("cluster", DataType.bigint()) .addStaticColumn("cluster", DataType.text()) .getQueryString(); }
@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"); } }
@Test(groups = "short") @CassandraVersion("2.1.0") public void should_create_a_table_and_a_udt() { .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()) .addUDTPartitionKey("b", myUDT) .addClusteringColumn("c", DataType.ascii()) .addUDTClusteringColumn("d", myUDT) .addUDTColumn("e", myUDT) .addStaticColumn("f", DataType.bigint()) .addUDTStaticColumn("g", myUDT) .addUDTListColumn("h", myUDT) .addUDTMapColumn("i", DataType.cboolean(), myUDT) .addUDTMapColumn("j", myUDT, DataType.cboolean()) .addUDTSetColumn("k", myUDT));
@Test( groups = "unit", expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "Cannot create table 'test' with compact storage and static columns '\\[stat\\]'") public void should_fail_creating_table_with_static_columns_and_compact_storage() throws Exception { createTable("test") .addPartitionKey("pk", DataType.bigint()) .addClusteringColumn("cluster", DataType.uuid()) .addStaticColumn("stat", DataType.text()) .withOptions() .compactStorage() .getQueryString(); } }
@Test( groups = "unit", expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "The '\\[pk\\]' columns can not be declared as partition keys and clustering keys at the same time") public void should_fail_if_same_partition_and_clustering_column() throws Exception { createTable("test") .addPartitionKey("pk", DataType.bigint()) .addClusteringColumn("pk", DataType.bigint()) .getQueryString(); }