0

Let's say we connect to some Cassandra cluster:

cqlengine.connection.setup('hostname', 'some_keyspace')

The method requires that we know at least one existing keyspace. What if we don't?

What if we simply wish to check if a keyspace exists and create it otherwise?

We could create a keyspace like this:

cqlengine.management.create_keyspace('keyspace_name', some_other_args)

So there are three questions here:

  1. How do you connect without supplying a keyspace?
  2. What is the correct way to create a new keyspace (what are the other arguments besides the name of the keyspace)?
  3. How do you switch keyspaces using cqlengine?
AturSams
  • 7,568
  • 18
  • 64
  • 98

1 Answers1

0
  1. You only need a 'default' keyspace. It can be anything that already exists. I think Cassandra does have some default keyspaces.

  2. The correct way to create a keyspace is using the create_keyspace_simple command:

    from cassandra.cqlengine.management import create_keyspace_simple
    
    create_keyspace_simple(keyspace_name, replication_factor)
    
  3. To change the keyspace:

    from cassandra.cqlengine import models
    
    models.DEFAULT_KEYSPACE = keyspace_name
    

    assuming connection is imported and you have defined keyspace_name

TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33