简体   繁体   English

使用Python驱动程序为Cassandra创建表和索引

[英]Table and Index Creation Using Python Driver for Cassandra

I'm trying to automate some table and index creation from a python script using the new datastax python driver. 我正在尝试使用新的datastax python驱动程序从python脚本自动化一些表和索引的创建。 However, it appears that certain statements are being skipped or executed out of order. 但是,似乎某些语句被跳过或执行不正确。 I even tried putting 10 second sleep events after each command in hopes of it working, but it did not. 我什至尝试在每个命令后放置10秒的睡眠事件,以期希望它起作用,但没有成功。

Usually only the second and third indexes are created. 通常只创建第二和第三索引。 Sometimes the table creation doesn't occur before the index creation and they error out. 有时表创建不会在索引创建之前发生,并且它们会出错。

import logging
from cassandra.cluster import Cluster
from time import sleep


log = logging.getLogger()
log.setLevel('DEBUG')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s"))
log.addHandler(handler)

cluster = Cluster(['128.32.xxx.xxx','128.32.xxx.xxx','128.32.xxx.xxx'])
session = cluster.connect()

session.execute("""use test;""")
#session.execute("""drop table test.devices;""")
log.info('dropped table devices.')

session.execute("""CREATE TABLE devices (
                         device_uuid uuid,
                         external_identifier text,
                         geohash text,
                         latitude float,
                         longitude float,
                         measures set<text>,
                         name text,
                         parent_device_id uuid,
                         tags map<text, text>,
                         PRIMARY KEY (device_uuid)
                       ) WITH
                         compression={'sstable_compression': 'SnappyCompressor'} USING CONSISTENCY ALL;""")

session.execute("""CREATE INDEX external_id_ind ON devices (external_identifier) USING CONSISTENCY ALL;""")

session.execute("""CREATE INDEX name_ind ON devices (name) USING CONSISTENCY ALL;""")

session.execute("""CREATE INDEX geohash_ind ON devices (geohash) USING CONSISTENCY ALL;""")

session.execute("""CREATE INDEX parent_device_id_ind ON devices (parent_device_id) USING CONSISTENCY ALL;""")

I believe you are writing statements using the cql3-beta syntax (The USING Consistency) which should not work with the python driver. 我相信您正在使用cql3-beta语法(USING一致性)编写语句,该语法不适用于python驱动程序。 The python driver is only meant to be used with Cassandra 1.2+ (CQL3). python驱动程序只能与Cassandra 1.2+(CQL3)一起使用。

The main difference is that consistency is now a state set by the client rather than a directive in the query itself. 主要区别在于,一致性现在是由客户端设置的状态,而不是查询本身中的指令。 With the python driver consistency is set like so 与python驱动程序一样,一致性设置如下

session.execute(SimpleStatement("SELECT * FROM...", consistency_level=ConsistencyLevel.QUORUM))"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM