CQL Statement Builder is a Java library that automatically builds a range of CQL statements from classes/models. The statements generated can then be executed in a preferred library or framework. CQL Statement Builder can generate CQL statements for the following commands:
- CREATE KEYSPACE
- CREATE TABLE
- CREATE TYPE
- DELETE
- DROP INDEX
- DROP KEYSPACE
- DROP TABLE
- DROP TYPE
- SELECT *
- SELECT COUNT(*)
- USE
CQL Statement Builder can also generate the CQL statements required to create or update schemas in an Apache Cassandra cluster. The schema generation feature is particularly useful because it will enable its user to focus on their code rather than worry about writing or re-writing CQL statements when a new class/model is created or when the structure of an old class/model changes. CQL Statement Builder makes use of the standard annotations provided by the Datastax Java Driver for Apache Cassandra, except for one:
@ClusteringColumnOrder: this annotation is used to indicate the order (i.e. Ascending or Descending) of the @ClusteringColumn. See User class example below.
You can build CREATE KEYSPACE CQL statements with either SimpleStrategy or NetworkTopologyStrategy replication strategy.
The example below shows how to build a CREATE KEYSPACE CQL statement for a keyspace called foo with the replication_factor of 1.
new CqlStatementBuildDirector().buildStatement(new CreateKeyspaceStatementBuilder(new SimpleStrategy("foo", 1)));
The example below shows how to build a CREATE KEYSPACE CQL statement for a keyspace called foo with two data centers.
new CqlStatementBuildDirector().buildStatement(new CreateKeyspaceStatementBuilder(new NetworkTopologyStrategy("foo", getDataCenters())));
private Set<DataCenter> getDataCenters() {
Set<DataCenter> dataCenters = new HashSet<>(2);
dataCenters.add(new DataCenter("datacenter1", 2));
dataCenters.add(new DataCenter("datacenter2", 3));
return dataCenters;
}
The example below shows how to build a CREATE TABLE CQL statement from the User class.
@Table(name = "user")
public class User {
@PartitionKey
private String username;
@ClusteringColumn
@ClusteringColumnOrder(clusteringOrder = ClusteringOrder.ASC)
private String firstName;
private String lastName;
@Frozen
private Address address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
@UDT(name = "address")
public class Address {
private String postCode;
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
}
new CqlStatementBuildDirector().buildStatement(new EntityStatementBuilderFactory(User.class).getEntityStatementBuilder());
The example below shows how to build a CREATE TYPE CQL statement from the Address class above.
new CqlStatementBuildDirector().buildStatement(new EntityStatementBuilderFactory(Address.class).getEntityStatementBuilder());
The example below shows how to build a DELETE CQL statement from the User class above. Multiple keys can also be specified if multiple partition keys or clustering columns are used.
new CqlStatementBuildDirector().buildStatement(new DeleteStatementBuilder(User.class, "foo"));
CQL Statement Builder can build the following DROP CQL statements:
- DROP INDEX
- DROP KEYSPACE
- DROP TABLE
- DROP TYPE
The example below shows how to build a DROP CQL statement for a KEYSPACE called foo.
new CqlStatementBuildDirector().buildStatement(new DropStatementBuilder(DroppableItem.KEYSPACE, "foo"));
The example below shows how to build a SELECT * CQL statement from the User class above. Multiple keys can also be specified if multiple partition keys or clustering columns are used.
new CqlStatementBuildDirector().buildStatement(new SelectStatementBuilder(User.class, "foo"));
The example below shows how to build a SELECT COUNT(*) CQL statement from the User class above. Multiple keys can also be specified if multiple partition keys or clustering columns are used.
new CqlStatementBuildDirector().buildStatement(new CountStatementBuilder(User.class, "foo"));
The example below shows how to build a USE CQL statement for a keyspace called foo.
new CqlStatementBuildDirector().buildStatement(new UseStatementBuilder("foo"));
The SchemaStatementGenerator generates the list of CREATE TABLE and CREATE TYPE CQL statements from classes annotated with UDT and Table annotations. The example below shows how to generate the list of CQL statements where the classes are located in the "com.foo.bar" package. The list of statements can be used to create or update the schemas in an Apache Cassandra cluster.
new SchemaStatementGenerator("com.foo.bar").generateStatements();