Executing CQL through Shell Script?

梦想与她 提交于 2020-08-03 12:55:10

问题


I am trying to execute the CQL commands from shell script. I am able to connect to the cqlsh (CQL version i'm using is 1.1.18) but unable to send the queries to cql.

Any ideas or suggestion how to proceed on this? Do I need to connect to Cassandra and execute few commands (select/update ) with shell script ??


回答1:


cqlsh -e "select * from ks.table limit 1;" > ~/output



回答2:


I'm not sure about Cassandra 1.1.18, but you should be able to accomplish this with the -f flag of cqlsh. Let's say have a file of CQL commands called "commands.cql". I can invoke those commands against my local Cassandra instance like this:

$ cqlsh -f commands.cql -u myusername -p mypassword localhost

If I wanted to invoke that from within a Bash script, the script's code would look something like this:

#!/bin/bash
cqlsh -f commands.cql -u myusername -p mypassword localhost

Save that as an executable file, and run it like any other.




回答3:


Need to connect to cassandra and execute few commands (select / update ) with shell script

You can execute your commands with shell script in next way:

echo "some QUERY; exit" | cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS'



回答4:


The "exit" command in the last suggestion is a bit hacky.

I would propose using xargs with cqlsh -e.

echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e

I recently had to use this approach when working with docker, because clqsh -f was not an option (too complex to configure access to the file needed).




回答5:


echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e

But what if you Cassandra instance is on a different server to where the shell script is being executed? (Specifically in StreamSets - wouldn't the above require Cassandra installed on the same server such that it has access to the cqlsh lib?)



来源:https://stackoverflow.com/questions/25286253/executing-cql-through-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!