4
votes

I want to execute cql queries from bash command.

[cqlsh 3.1.8 | Cassandra 1.2.19 | CQL spec 3.0.5 | Thrift protocol 19.36.2]

[root@hostname ~]# /opt/apache-cassandra-1.2.19/bin/cqlsh -k "some_keyspace" -e "SELECT column FROM Users where key=value"

I got:

cqlsh: error: no such option: -e

Options:

  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -C, --color           Always use color output
  --no-color            Never use color output
  -u USERNAME, --username=USERNAME
                        Authenticate as user.
  -p PASSWORD, --password=PASSWORD
                        Authenticate using password.
  -k KEYSPACE, --keyspace=KEYSPACE
                        Authenticate to the given keyspace.
  -f FILE, --file=FILE  Execute commands from FILE, then exit
  -t TRANSPORT_FACTORY, --transport-factory=TRANSPORT_FACTORY
                        Use the provided Thrift transport factory function.
  --debug               Show additional debugging information
  --cqlversion=CQLVERSION
                        Specify a particular CQL version (default: 3.0.5).
                        Examples: "2", "3.0.0-beta1"
  -2, --cql2            Shortcut notation for --cqlversion=2
  -3, --cql3            Shortcut notation for --cqlversion=3

Any suggestions ?

3
How about: cqlsh -e "SELECT column FROM some_keyspace.Users where key=value;" ?markc
run cqlsh -h to get supported optionsundefined_variable

3 Answers

1
votes

First of all, you should seriously consider upgrading. You are missing out on a lot of new features and bug fixes.

Secondly, with cqlsh in Cassandra 1.2 you can use the -f flag to specify a file containing cql statements:

$ echo "use system_auth; SELECT role,is_superuser FROM roles WHERE role='cassandra';" > userQuery.cql
$ bin/cqlsh -u aploetz -p reindeerFlotilla -f userQuery.cql 

 role      | is_superuser
-----------+--------------
 cassandra |         True


(1 rows)
0
votes

You can use -f to execute from a file or SOURCE once you start CQLSH. I don't think -e is a valid option with that version.

0
votes

It's bit dirty and unstable, but here is the answer:

/opt/apache-cassandra-1.2.19/bin/cqlsh -k "keyspace" -f  /path/to/file.cql  > /path/to/output.txt
tail -2 /path/to/output.txt | head -1 > /path/to/output-value.txt