本文介绍了是否可以在cql脚本的cql命令中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在CQL脚本中使用时,是否可以通过CQL命令传递变量?
Is there a way to pass variables in CQL commands when being used in CQL scripts like:
select * from "Column Family Name" where "ColumnName"='A variable which takes different values';
欢迎提出任何建议。
推荐答案
不,CQL确实没有定义变量,运行循环以及基于这些变量进行更新/查询的方法。
No, CQL really doesn't have a way to define variables, run a loop, and update/query based on those variables.
作为替代,我通常将用于这样的简单任务/脚本。这是我使用一段时间后从CSV文件填充产品颜色的Python脚本的摘录。
As an alternative, I typically use the DataStax Python driver for simple tasks/scripts like this. Here is an excerpt from a Python script I used a while back to populate product colors from a CSV file.
# 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)
有关更多信息,请参见DataStax教程。
For more information, check the DataStax tutorial Getting Started with Apache Cassandra and Python.
这篇关于是否可以在cql脚本的cql命令中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!