keelar
keelar

Reputation: 6026

How to include input arguments in the CQL command - source

In Cassandra Query Language (CQL), there is a handy command called source which allows user to execute the cql command stored in an external file.

SOURCE

Executes a file containing CQL statements. Gives the output for each
statement in turn, if any, or any errors that occur along the way.

Errors do NOT abort execution of the CQL source file.

Usage:
      SOURCE '<file>';

But I am wondering if it is possible to allow this external file to take additional input arguments. For example, suppose I would like to develop the following cql file with two input arguments:

create keyspace $1 with
    strategy_class='SimpleStrategy' and
    strategy_options:replication_factor=$2;

and would like to execute this cql in cqlsh by something like:

source 'cql-filename' hello_world 3

I developed the above example cql, stored it in a file called create-keyspace.cql, and tried some possible commands that I can come up with, but none of them works.

cqlsh> source 'create-keyspace.cql'
create-keyspace.cql:2:Invalid syntax at char 17
create-keyspace.cql:2:  create keyspace $1 with strategy_class='SimpleStrategy' and strategy_options:replication_factor=$2;
create-keyspace.cql:2:                  ^
cqlsh> source 'create-keyspace.cql' hello_world 3
Improper source command.
cqlsh> source 'create-keyspace.cql hello_world 3'
Could not open 'create-keyspace.cql hello_world 3': [Errno 2] No such file or directory: 'create-keyspace.cql hello_world 3'

Can I know whether CQl has this type of supports? If yes, then how should I do it properly?

Upvotes: 2

Views: 4687

Answers (2)

Esteban Herrera
Esteban Herrera

Reputation: 2291

cqlsh only supports one parameter, the file containing CQL statements:

http://docs.datastax.com/en/cql/3.1/cql/cql_reference/source_r.html

It's a python-based command-line client. You can see its source code by looking for a file named cqlsh.py on the official Cassandra repo:

http://git-wip-us.apache.org/repos/asf/cassandra.git

And doing a search for SOURCE inside that file to see how it's handled.

Upvotes: 0

jbellis
jbellis

Reputation: 19377

cqlsh is not really intended to be a scripting environment. It sounds like you'd be better served by using the Python CQL driver: https://github.com/datastax/python-driver

Upvotes: 2

Related Questions