public String generateCreateTableStmt() { final String fields = generateFieldsSyntax(); final String primaryKeyStmt = generatePrimaryKeySyntax(); final String clusteringOrder = generateClusteringOrderSyntax(); final String createStatement = String.format("CREATE TABLE IF NOT EXISTS %s.%s (%s, %s) %s", this.schema.getKeySpace(), this.schema.getTableName(), fields, primaryKeyStmt, clusteringOrder); log.info("Generated table schema is {}", createStatement); return createStatement; }
/** * @return * Returns all the column names for a specific Cassandra table */ public String getColumnNamesFromTableQuery() { return String.format("SELECT %s FROM %s WHERE %s = '%s' " + "AND table_name = '%s'", COLUMN_NAME, SYSTEM_SCHEMA_COLS, KEYSPACE_NAME, this.schema.getKeySpace(), this.schema.getTableName()); }
public CassandraSinkSchemaManager(@NonNull final CassandraSchema schema, @NonNull final List<String> partitionKeys, @NonNull final List<ClusterKey> clusteringKeys, @NonNull final Optional<Long> ttl) { this.schema = schema; this.partitionKeys = partitionKeys; this.clusteringKeys = clusteringKeys; this.ttl = ttl; validateSchema(); }
/** * Creates a basic table in Cassandra that can be used for more testing (i.e altering) */ private void createBasicTable() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Arrays.asList(new ClusterKey("city", ClusterKey.Order.DESC), new ClusterKey("gym_name", ClusterKey.Order.ASC))); final String createTableStmt = schemaManager.generateCreateTableStmt(); try (final Session session = getSession()) { session.execute(createTableStmt); } }
@Test public void testGenerateColumnNameQueries() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Arrays.asList(new ClusterKey("city", ClusterKey.Order.DESC), new ClusterKey("gym_name", ClusterKey.Order.ASC))); final String cfQuery = schemaManager.getColumnNamesFromColumnFamilyQuery(); final String tableQuery = schemaManager.getColumnNamesFromTableQuery(); Assert.assertEquals("SELECT column_name FROM system_schema.columns WHERE " + "keyspace_name = 'marmaray' AND columnfamily_name = 'crossfit_gyms'", cfQuery); Assert.assertEquals("SELECT column_name FROM system_schema.columns WHERE " + "keyspace_name = 'marmaray' AND table_name = 'crossfit_gyms'", tableQuery); }
@Test(expected = IllegalStateException.class) public void testPartitionAndClusterKeyHaveSameName() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Arrays.asList(new ClusterKey("country_code", ClusterKey.Order.DESC), new ClusterKey("gym_name", ClusterKey.Order.ASC))); Assert.fail(); }
@Test(expected = IllegalStateException.class) public void testPartitionKeysIsEmpty() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Collections.EMPTY_LIST, Collections.EMPTY_LIST); Assert.fail(); }
public static ClusterKey parse(@NotEmpty final String value) { final List<String> tokens = splitter.splitToList(value); Preconditions.checkState(tokens.size() <= 2); if (tokens.size() == 1) { return new ClusterKey(tokens.get(0), Order.ASC); } else { return new ClusterKey(tokens.get(0), Order.valueOf(tokens.get(1))); } } }
@Test public void testCreateTableWithMultiplePrimaryKeysAndClusteringKeys() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Arrays.asList(new ClusterKey("city", ClusterKey.Order.DESC), new ClusterKey("gym_name", ClusterKey.Order.ASC))); final String createTableStmt = schemaManager.generateCreateTableStmt(); final String expected = "CREATE TABLE IF NOT EXISTS marmaray.crossfit_gyms (country_code text," + "state_province text,city text,capacity int,gym_name text, PRIMARY KEY ((country_code,state_province)" + ",city,gym_name)) WITH CLUSTERING ORDER BY (city DESC,gym_name ASC)"; Assert.assertEquals(expected, createTableStmt); try (final Session session = getSession()) { session.execute(createTableStmt); validateCreateTable(session); } }
@Test(expected = IllegalStateException.class) public void testEmptyKeySpace() { final CassandraSchema schema = new CassandraSchema(StringTypes.EMPTY, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Arrays.asList(new ClusterKey("country_code", ClusterKey.Order.DESC), new ClusterKey("gym_name", ClusterKey.Order.ASC))); Assert.fail(); }
@Test(expected = IllegalStateException.class) public void testPartitionKeyIsNotInFieldNames() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Collections.singletonList("non_field_name"), Collections.EMPTY_LIST); Assert.fail(); }
/** * @return * Returns all the column names for a specific column family */ public String getColumnNamesFromColumnFamilyQuery() { return String.format("SELECT %s FROM %s WHERE %s = '%s' " + "AND columnfamily_name = '%s'", COLUMN_NAME, SYSTEM_SCHEMA_COLS, KEYSPACE_NAME, this.schema.getKeySpace(), this.schema.getTableName()); }
@Test public void testCreateTableWithSinglePrimaryKeyAndOneClusteringKeyDesc() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Collections.singletonList("country_code"), Collections.singletonList(new ClusterKey("state_province", ClusterKey.Order.DESC))); final String createTableStmt = schemaManager.generateCreateTableStmt(); final String expected = "CREATE TABLE IF NOT EXISTS marmaray.crossfit_gyms (country_code text," + "state_province text,city text,capacity int,gym_name text, PRIMARY KEY ((country_code),state_province))" + " WITH CLUSTERING ORDER BY (state_province DESC)"; Assert.assertEquals(expected, createTableStmt); try (final Session session = getSession()) { session.execute(createTableStmt); validateCreateTable(session); } }
@Test(expected = IllegalStateException.class) public void tesClusterKeyIsNotInFieldNames() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Collections.singletonList("country_code"), Collections.singletonList(new ClusterKey("non_cluster_field_name", ClusterKey.Order.ASC))); Assert.fail(); }
@Test(expected = IllegalStateException.class) public void testEmptyTableName() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, StringTypes.EMPTY, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Collections.EMPTY_LIST); Assert.fail(); }
@Test public void testCreateTableWithMultiplePrimaryKeys() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, fields); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Collections.EMPTY_LIST); final String createTableStmt = schemaManager.generateCreateTableStmt(); final String expected = "CREATE TABLE IF NOT EXISTS marmaray.crossfit_gyms (country_code text," + "state_province text,city text,capacity int,gym_name text, PRIMARY KEY ((country_code,state_province))) "; Assert.assertEquals(expected, createTableStmt); try (final Session session = getSession()) { session.execute(createTableStmt); validateCreateTable(session); } }
@Test(expected = IllegalStateException.class) public void testFieldsIsNull() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, null); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Collections.EMPTY_LIST); Assert.fail(); }
@Test(expected = IllegalStateException.class) public void testFieldsIsEmpty() { final CassandraSchema schema = new CassandraSchema(KEY_SPACE, TABLE, Collections.EMPTY_LIST); final CassandraSinkSchemaManager schemaManager = new CassandraSinkSchemaManager( schema, Arrays.asList("country_code", "state_province"), Collections.EMPTY_LIST); Assert.fail(); }