如何使用 Python 在 Cassandra 中创建键空间?
from cassandra.cluster import Cluster
cluster = Cluster(['cassandra.sucks.com'])
session = cluster.connect()
session.execute('CREATE KEYSPACE IF NOT EXISTS kong')
File "cassandra/cluster.py", line 1998, in cassandra.cluster.Session.execute (cassandra/cluster.c:34869)
File "cassandra/cluster.py", line 3781, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:73073)
cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 0:-1 mismatched input '<EOF>' expecting K_WITH">
Run Code Online (Sandbox Code Playgroud)
如果您有单个数据中心,您应该使用以下查询创建键空间:
CREATE KEYSPACE IF NOT EXISTS kong WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
Run Code Online (Sandbox Code Playgroud)
'replication_factor' 如果类是 SimpleStrategy,则为必需;否则,不使用。多个节点上数据的副本数。
或者如果您有多个数据中心,则使用:
CREATE KEYSPACE IF NOT EXISTS kong WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2}
Run Code Online (Sandbox Code Playgroud)
dc1 和 dc2 是数据中心名称。如果 class 为 NetworkTopologyStrategy 并且您提供数据中心的名称,则为必需项。此值是该数据中心中每个节点上的数据副本数。
来源:http : //docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html