sch*_*ter 7 cql cassandra cqlsh
有没有办法在CQL脚本中使用CQL命令时传递变量,如:
select * from "Column Family Name" where "ColumnName"='A variable which takes different values';
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议.
不,CQL真的没有办法定义变量,运行循环,并根据这些变量更新/查询.
作为替代方案,我通常使用DataStax Python驱动程序来完成这样的简单任务/脚本.以下是Python脚本的摘录,我用了一段时间来填充CSV文件中的产品颜色.
# connect to Cassandra
auth_provider = PlainTextAuthProvider(username='username', password='currentHorseBatteryStaple')
cluster = Cluster(['127.0.0.1'], auth_provider=auth_provider)
session = cluster.connect('products')
# prepare statements
preparedUpdate = session.prepare(
"""
UPDATE products.productsByItemID SET color=? WHERE itemid=? AND productid=?;
"""
)
# end prepare statements
counter = 0
# read csv file
dataFile = csv.DictReader(csvfilename, delimiter=',')
for csvRow in dataFile:
itemid = csvRow['itemid']
color = csvRow['customcolor']
productid = csvRow['productid']
#update product color
session.execute(preparedUpdate,[color,itemid,productid])
counter = counter + 1
# close Cassandra connection
session.cluster.shutdown()
session.shutdown()
print "updated %d colors" % (counter)
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看DataStax教程Apache Cassandra和Python入门.
| 归档时间: |
|
| 查看次数: |
4045 次 |
| 最近记录: |