Database Reference
In-Depth Information
def connect(self, host):
self.cluster = Cluster(host)
self.session = self.cluster.connect()
print "Cluster: %s" %
self.cluster.metadata.cluster_name
for host in self.cluster.metadata.all_hosts():
print "Host: %s" % host
Disconnecting
To disconnect from the cluster, all you need to do is call shutdown on the cluster
object. Listing 9.17 shows how to do this.
Listing 9.17 Using Python to Disconnect from a Cluster
def close(self):
self.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 condi-
tional, 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 an error. This conditional is available only in Cassandra 2.0 and later.
Listing 9.18 shows how to create a keyspace and a sample table.
Listing 9.18 Creating a Schema in Python
Click here to view code image
def create_schema(self):
self.session.execute("CREATE KEYSPACE IF NOT
EXISTS portfolio_demo "
"WITH REPLICATION = {
'class': 'SimpleStrategy', "
"'replication_factor': 1
};")
self.session.execute("CREATE TABLE IF NOT EXISTS
 
 
Search WWH ::




Custom Search