How to list all the available keyspaces in Cassandra?
Asked Answered
M

17

242

I am newbie in Cassandra and trying to implement one toy application using Cassandra. I had created one keyspace and few column families in my Cassandra DB but I forgot the name of my cluster.

I am trying to find if there is any query which can list down all the available keyspaces.

Anybody knows such a query or command?

Mitchum answered 10/9, 2013 at 7:28 Comment(4)
C* 3.x : SELECT * FROM system_schema.keyspaces;Haileyhailfellowwellmet
This answer is outdated. Correct answer is in here enter link description hereValonia
Possible duplicate of Is there a clear equivalent of 'show keyspaces' in cqlsh 2?Hun
C* 2.x: SELECT * FROM system.schema_keyspaces;Sharlenesharline
C
106

If you want to do this outside of the cqlsh tool you can query the schema_keyspaces table in the system keyspace. There's also a table called schema_columnfamilies which contains information about all tables.

The DESCRIBE and SHOW commands only work in cqlsh and cassandra-cli.

Cortisone answered 11/9, 2013 at 19:23 Comment(3)
Too much detail sometimes takes away simplicity, Marco answer is on point.Steelworks
show keyspaces in sqlsh in Cassandra 4.0 it raises an error ("Improper show command.") I used describe keyspaces instead.Reisfield
did you mean SELECT * FROM system_schema.keyspaces? instead of SELECT * FROM system.schema.keyspaces? Please edit your answer...Zubkoff
M
454

[cqlsh 4.1.0 | Cassandra 2.0.4 | CQL spec 3.1.1 | Thrift protocol 19.39.0]

Currently, the command to use is:

DESC[RIBE] keyspaces;
Maudemaudie answered 17/1, 2014 at 9:20 Comment(2)
Accept this as answer! And by the way, the output would print multiple keyspace names in one line.Jahvist
Cassandra also allow a short-hand command as in: DESC keyspacesMarcus
C
106

If you want to do this outside of the cqlsh tool you can query the schema_keyspaces table in the system keyspace. There's also a table called schema_columnfamilies which contains information about all tables.

The DESCRIBE and SHOW commands only work in cqlsh and cassandra-cli.

Cortisone answered 11/9, 2013 at 19:23 Comment(3)
Too much detail sometimes takes away simplicity, Marco answer is on point.Steelworks
show keyspaces in sqlsh in Cassandra 4.0 it raises an error ("Improper show command.") I used describe keyspaces instead.Reisfield
did you mean SELECT * FROM system_schema.keyspaces? instead of SELECT * FROM system.schema.keyspaces? Please edit your answer...Zubkoff
S
54

Its very simple. Just give the below command for listing all keyspaces.

Cqlsh> Describe keyspaces;

If you want to check the keyspace in the system schema using the SQL query

below is the command.

SELECT * FROM system_schema.keyspaces;

Hope this will answer your question...

You can go through the explanation on understanding and creating the keyspaces from below resources.

Documentation:

https://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_keyspace_r.html https://www.i2tutorials.com/cassandra-tutorial/cassandra-create-keyspace/

Siliculose answered 27/12, 2017 at 14:21 Comment(1)
DESCRIBE does not work on latest cqlsh. This worksWilly
M
13

Found it...show keyspaces command lists down all the keyspaces. I think earlier when I tried this command, I forgot to give last 's' in 'keyspaces'

Mitchum answered 10/9, 2013 at 7:33 Comment(3)
You can use tab completion to see hints about how to complete a cassandra-cli or cqlsh command. That may be helpful to you if you forget the syntax. For example, just type "show " and then tab to see the valid show commands.Fortification
The secondary point in your answer is going to be (for some people) the most valuable part of the answer. Things work a lot differently between "cqlsh" and "cassandra-cli" (and the question poster does not indicate which he is using). show keyspaces; describe (keyspace name). NOTE: If one is using "cqlsh", it requires quotes around any mixed or upper case names in a keyspace or columnfamily (OK, the poster didn't ask this but it's a common user error if new to C*)Definition
In sqlsh in Cassandra 4.0 it raises an error ("Improper show command.") I used describe keyspaces instead.Reisfield
B
10

To see all the keyspaces on your Apache Cassandra NoSQL Database Server use the command:

> DESCRIBE KEYSPACES 
Blair answered 9/4, 2020 at 8:6 Comment(1)
This should be the accepted answer. Short and directly to the point.Athenian
N
8

Once logged in to cqlsh or cassandra-cli. run below commands

  • On cqlsh

desc keyspaces;

or

describe keyspaces;

or

select * from system_schema.keyspaces;

  • On cassandra-cli

show keyspaces;

Nicolettenicoli answered 19/12, 2018 at 15:23 Comment(0)
S
5

The DESCRIBE command is your friend. You can describe one keyspace, list keyspaces, one table or list all tables in keyspace, the cluster and much more. You can get the full idea by typing

HELP DESCRIBE in cqlsh.

Connected to mscluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.8 | CQL spec 3.4.2 | Native protocol v4] Use HELP for help.

cqlsh> HELP DESCRIBE

    DESCRIBE [cqlsh only]

    (DESC may be used as a shorthand.)

      Outputs information about the connected Cassandra cluster, or about
      the data objects stored in the cluster. Use in one of the following ways:...<omitted for brevity>
  • DESCRIBE <your key space name> - describes the command used to create keyspace

cqlsh> DESCRIBE testkeyspace;

CREATE KEYSPACE testkeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor': '3'} AND durable_writes = true;

  • DESCRIBE keyspaces - lists all keyspaces

cqlsh> DESCRIBE KEYSPACES

system_schema system testkeyspace system_auth
system_distributed system_traces

  • DESCRIBE TABLES - List all tables in current keyspace

cqlsh:system> DESCRIBE TABLES;

available_ranges peers paxos
range_xfers batches compaction_history batchlog
local "IndexInfo" sstable_activity
size_estimates hints views_builds_in_progress peer_events
built_views

  • DESCRIBE your table name or DESCRIBE TABLE your table name - Gives the table details

cqlsh:system> DESCRIBE TABLE batchlog

CREATE TABLE system.batchlog ( id uuid PRIMARY KEY, data blob, version int, written_at timestamp ) WITH bloom_filter_fp_chance = 0.01 AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'} AND comment = 'DEPRECATED batchlog entries' ....omitted for brevity

Shupe answered 11/9, 2017 at 11:3 Comment(0)
N
5
  1. login to cqlsh

  2. use below command to get names/list of keyspaces present

         SELECT keyspace_name FROM system_schema.keyspaces;
    
Nauplius answered 13/3, 2020 at 8:47 Comment(1)
it depends on the version of Cassandra... Also, how this answer is different from previous?Anacardiaceous
A
4

DESC KEYSPACES will do the job.

Also, If you want to describe schema of a particular keyspace you can use

DESC

Ainslie answered 22/11, 2017 at 14:57 Comment(0)
B
4

To List all the available keyspaces in cassandra using cqlsh in CLI mode.

Command : DESCRIBE keyspaces;

Example :

cqlsh> DESCRIBE keyspaces;

Example1

Bellbella answered 23/10, 2020 at 9:14 Comment(0)
C
4

Defining A KeySpace

Understanding A Database:-In Cassandra, A database is defined as a Keyspace, Within the KeySpace Table Can be Defined.

Checking ALL KeySpace

cqlsh> DESCRIBE KEYSPACEs;

Defining A Available Keyspace

cqlsh> DESCRIBE KEYSPACE stack ;
Caracole answered 17/3, 2023 at 11:15 Comment(0)
W
2
  1. login to cqlsh
  2. desc keyspaces;
  3. select * from system_schema.keyspaces ;
Weaner answered 11/11, 2018 at 2:21 Comment(0)
P
1

desc keyspaces will do it for you.

Planish answered 26/10, 2017 at 11:51 Comment(0)
T
1

DESCRIBE keyspaces to list all keysapces DESCRIBE keyspace https://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/cqlsh_commands/cqlshDescribeKeyspace.html

Troup answered 9/5, 2019 at 8:57 Comment(0)
T
1

I suggest a combination of grep and awk:

root@DC1-Node1:/home# nodetool tablestats | grep "Keyspace :" | awk -F ":" '{print $2}'
 system_traces
 system
 system_distributed
 system_schema
 device_tool
 system_tool
Tragedy answered 5/9, 2019 at 13:33 Comment(1)
thank you for the answer but simplest way to see keyspaces is to use "DESCRIBE keyspace" command. No need to grep and awk.Mitchum
O
0

Apart from above method, if you have opscenter installed,

  1. Go to data tab > there you will see all keyspcaces created by you and some system keyspaces.
  2. You can see all tables under individual keyspaces and also replicator factor for keyspace.

for more details check below link. https://docs.datastax.com/en/opscenter/6.1/opsc/online_help/opscDataModelingManagingKeyspace_t.html

Opossum answered 19/2, 2019 at 17:48 Comment(0)
T
-1

describes and desc command will give list of keyspaces in the cluster.Please find below output for more details.

cqlsh> describe keyspaces
reaper_db      system_auth  system_distributed
system_schema  system       system_traces

OR

cqlsh> desc keyspaces
reaper_db      system_auth  system_distributed
system_schema  system       system_traces
Thad answered 15/10, 2019 at 10:24 Comment(3)
how is your answer is different from other answers? Also - these commands are only working in cqlsh, not everywhere...Anacardiaceous
cassandra@node1:~$ nodetool tablestats | grep "Keyspace :" Keyspace : reaper_db Keyspace : system_traces Keyspace : system Keyspace : system_distributed Keyspace : system_schema Keyspace : system_auth cassandra@node1:~$Thad
There are at least 4 answers that list the same commands for cqlshAnacardiaceous

© 2022 - 2024 — McMap. All rights reserved.