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';
Any suggestions are welcome.
You can add a column in the table by using the ALTER command. While adding column, you have to aware that the column name is not conflicting with the existing column names and that the table is not defined with compact storage option.
CQL provides the facility of using Collection data types. Using these Collection types, you can store multiple values in a single variable.
No, CQL really doesn't have a way to define variables, run a loop, and update/query based on those variables.
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)
For more information, check the DataStax tutorial Getting Started with Apache Cassandra and Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With