Database Reference
In-Depth Information
Cluster.builder().addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Cluster: %s\n",
metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Host: %s
\n",host.getAddress());
}
session = cluster.connect();
}
Disconnecting
To disconnect from the cluster, all you need to do is call
shutdown
on the cluster
object.
Listing 9.3
shows how to do this.
Listing 9.3
Using Java to Disconnect from a Cluster
public void close(){
cluster.shutdown();
}
Schema Creation
Now that all of the connections have been created, we can create our schema.
When creating the schema, we can use the optional
IF NOT EXISTS
condition-
al, which will allow running the command multiple times without error. When this
conditional is not specified, additional executions of the schema creation code will
result in a
QueryExecutionException
. This conditional is available only in
Cassandra 2.0 and later.
Listing 9.4
shows how to create a keyspace and a sample
table.
Listing 9.4
Creating a Schema in Java
public void createSchema(){
session.execute("CREATE KEYSPACE IF NOT EXISTS
portfolio_demo " +
"WITH REPLICATION = { 'class':