I need to List all the available keyspaces in Cassandra and save to a .txt file

Viewed 1363

Hi All I am new in Cassandra and got an assignment where I need to List all the available keyspaces in Cassandra and save to a .txt file

I have tried all possible codes and searched many sites but still I am unable to succeed.

I have tried below codes in order to save the available keyspaces in .txt file.

cqlsh -e 'DESCRIBE KEYSPACE firstkeyspace' > test.txt;
cqlsh -e "DESCRIBE KEYSPACE firstkeyspace" > pathtosomekeyspace.txt

cqlsh -e "DESC KEYSPACE firstkeyspace" > firstkeyspace_schema.txt;
cqlsh -e "DESC KEYSPACES" > firstkeyspace_schema.txt

I am getting error and unable to fix it.

SyntaxException: line 1:0 no viable alternative at input 'cqlsh' ([cqlsh]...)

I have also checked with single quote but still not working. Request you all to help me to solve this problem. Thanks in advance.

3 Answers

This error indicates that you're running the commands within cqlsh itself:

SyntaxException: line 1:0 no viable alternative at input 'cqlsh' ([cqlsh]...)

For example:

cqlsh> cqlsh -e "DESCRIBE KEYSPACE ks" > ks.txt ;
SyntaxException: line 1:0 no viable alternative at input 'cqlsh' ([cqlsh]...)

You need to exit out of cqlsh and run the commands at the Linux command line. For example:

$ cqlsh -e "DESCRIBE KEYSPACES" > keyspaces.txt

Don't confuse CQL commands like DESCRIBE KEYSPACES with Linux shell commands. Cheers!

So I see that error when I try to run cqlsh from within cqlsh.

aploetz@cqlsh> cqlsh -u aploetz -p xxxxxxxx -e 'DESCRIBE KEYSPACE stackoverflow' ;
SyntaxException: line 1:0 no viable alternative at input 'cqlsh' ([cqlsh]...)

That's not going to work. Exit out, and run it from your command line, instead.

aploetz@cqlsh> exit
% bin/cqlsh -u aploetz -p xxxxxxxx -e 'DESCRIBE KEYSPACE stackoverflow' > stackoverflow.txt
% head -n 5 stackoverflow.txt

CREATE KEYSPACE stackoverflow WITH replication = {'class': 'NetworkTopologyStrategy', 'SnakesAndArrows': '1'}  AND durable_writes = true;

CREATE TABLE stackoverflow.customer_info_by_date (
    billing_due_date date,

If you're referring to a hackerrrank prompt, or even otherwise, here is what I did to solve it!

hr gave me the example of trying: cqlsh -e "command" > filename

HOWEVER: this didn't work for me just as it didn't for you. Instead, do:

COPY system_schema.keyspaces TO 'keyspace.txt';

**Here, system_schema.keyspaces is generic to all systems as it seems to collect all the keyspaces (rather than being one of my named keyspaces)

Related