使用Shell脚本运行CQL语句

时间:2018-10-31 20:27:46

标签: shell unix cassandra

我有一个下面的shell脚本,该脚本读取文件并更新表。但是以下代码的问题是建立了用于运行每个sql语句的连接。我正在寻找增强此代码的建议。

#!/bin/bash
input="/Users/temp/newfile.txt"
while IFS= read -r var
do
 echo "update keyspace.tableName set randomColumn='randomText' where random_id='$var'; exit" | ./cqlsh serverName  -u username -p password
if [ $? -eq 0 ]; then
   echo SUCCESS
   echo "select random_id,randomColumn from keyspace.tableName where random_id='$var'; exit" | ./cqlsh serverName  -u username -p password
else
    echo FAIL
fi
done < "$input"

3 个答案:

答案 0 :(得分:1)

cqlsh具有-f选项,该选项允许从文件运行cql语句。您可以通过解析newfile.txt生成cql语句,然后使用-f选项运行cqlsh

在最基本的级别上,您可以执行以下操作:

#!/bin/bash
input="newfile.txt"
while IFS= read -r var
do
 echo "update ks.t1 set name='randomText' where id=$var;" >> result 
done < "$input"

./cqlsh serverName  -u username -p password -f result

if [ $? -eq 0 ]; then
   echo SUCCESS
   echo "select * from keyspace.tableName; exit" | ./cqlsh serverName  -u username -p password
else
    echo FAIL
fi

答案 1 :(得分:1)

如果您想获得更好的性能,建议您使用Python driver进行此操作。

此示例应大致等效:

#! /usr/bin/env python3

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

cluster = Cluster(auth_provider=PlainTextAuthProvider(username='cassandra', password='cassandra'))
session = cluster.connect()

with open("/Users/temp/newfile.txt") as f:
    for line in f:
        try:
            rows = session.execute("UPDATE keyspace.tableName SET randomColumn='randomText' WHERE random_id=%(var)s", {'var'=line})
            print("SUCCESS")
            for r in rows:
                print(r)
        except Exception:
            print("FAIL")

编辑:您甚至可以更进一步,并使用prepared statementsasync queries,如果您要进行大量查询,这将显着提高性能。

答案 2 :(得分:-1)

我建议将cqlsh -exargs一起使用:

#!/bin/bash
input="/Users/temp/newfile.txt"
while IFS= read -r var
do
    echo "update keyspace.tableName set randomColumn='randomText' where random_id='$var';" | xargs cqlsh serverName  -u 'username' -p 'password' -e
if [ $? -eq 0 ]; then
    echo SUCCESS
    echo "select random_id,randomColumn from keyspace.tableName where random_id='$var';" | xargs cqlsh serverName  -u 'username' -p 'password' -e
else
    echo FAIL
fi
done < "$input"