@Test public void createComplexPrimaryKey() { final Schema keySchema = SchemaBuilder.struct() .field("username", Schema.STRING_SCHEMA) .field("companyID", Schema.INT64_SCHEMA) .build(); final Schema valueSchema = SchemaBuilder.struct() .field("username", Schema.STRING_SCHEMA) .field("companyID", Schema.INT64_SCHEMA) .field("firstName", Schema.STRING_SCHEMA) .field("lastName", Schema.STRING_SCHEMA) .field("created", Timestamp.SCHEMA) .field("updated", Timestamp.SCHEMA) .build(); this.builder.build("foo", keySchema, valueSchema); verify(this.session, times(1)).executeStatement(any(Create.class)); }
/** * Build schema programmatically * <p> * DDL equivalent: * * <pre> * CREATE TABLE messages ( * sessionId uuid, * seqNo bigint, * message blob, * PRIMARY KEY (sessionId, seqNo ) ); * </pre> * * @throws StoreException if the store is not open * */ public void buildSchema() throws StoreException { if (session != null) { // Appropriate for a local test only session.execute(new SimpleStatement("CREATE KEYSPACE IF NOT EXISTS " + KEYSPACE_NAME + " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }")); System.out.format("Keyspace %s available\n", KEYSPACE_NAME); Create create = SchemaBuilder.createTable(KEYSPACE_NAME, TABLE_NAME).ifNotExists() .addPartitionKey(SESSION_ID_COLNAME, DataType.uuid()) .addClusteringColumn(SEQ_NO_COLNAME, DataType.bigint()) .addColumn(MESSAGE_COLNAME, DataType.blob()); ResultSet resultSet = session.execute(create); System.out.format("Table %s available\n", TABLE_NAME); } else { throw new StoreException("Schema not created; store not open"); } }
private static void cqlBuilders() { FieldModel model = SampleModels.wrapper(); Create create = SchemaBuilder.createTable("Field").addClusteringColumn(LOGIN.name(), text()) .addPartitionKey("snapshot_id", timeuuid()); model.getFieldInfos().stream().filter(f -> f.id() != LOGIN) .forEach(f -> create.addColumn(f.id().name(), cqlType(f))); Create.Options createWithOptions = create.withOptions().clusteringOrder(LOGIN.name(), DESC); System.out.println(createWithOptions); Insert insert = QueryBuilder.insertInto("Field"); model.stream().forEach(e -> insert.value(e.getKey().name(), e.getValue())); System.out.println(insert.getQueryString(codecRegistry())); }
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 public void createBasicPrimaryKey() { final Schema keySchema = SchemaBuilder.struct() .field("id", Schema.STRING_SCHEMA) .build(); final Schema valueSchema = SchemaBuilder.struct() .field("id", Schema.STRING_SCHEMA) .field("firstName", Schema.STRING_SCHEMA) .field("lastName", Schema.STRING_SCHEMA) .build(); builder.build("foo", keySchema, valueSchema); verify(this.session, times(1)).executeStatement(any(Create.class)); }
@Test public void simpleCassandraSchema() { FieldModel model = SampleModels.wrapper(); Create createRequest = SchemaBuilder.createTable("fields_model") .addClusteringColumn(LOGIN.name(), text()) .addPartitionKey("snapshot_id", timeuuid()); model.getFieldInfos().stream() .filter(info -> info.id() != LOGIN) .forEach(info -> createRequest.addColumn(info.id().name(), cqlType(info))); Options createRequestWithOptions = createRequest.withOptions().clusteringOrder(LOGIN.name(), DESC); print(createRequestWithOptions.getQueryString()); }