Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use variables in cql commands in cql scripts?

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.

like image 964
schatter Avatar asked May 21 '15 12:05

schatter


People also ask

How do I add a column in CQL?

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.

How is Cassandra CQL collection used?

CQL provides the facility of using Collection data types. Using these Collection types, you can store multiple values in a single variable.


1 Answers

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.

like image 198
Aaron Avatar answered Oct 03 '22 21:10

Aaron