How to list all the available keyspaces in Cassandra? - cassandra

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?

[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;

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.

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/

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'

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

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;

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

DESC KEYSPACES will do the job.
Also, If you want to describe schema of a particular keyspace you can use
DESC

To List all the available keyspaces in cassandra using cqlsh in CLI mode.
Command : DESCRIBE keyspaces;
Example :
cqlsh> DESCRIBE keyspaces;

login to cqlsh
use below command to get names/list of keyspaces present
SELECT keyspace_name FROM system_schema.keyspaces;

login to cqlsh
desc keyspaces;
select * from system_schema.keyspaces ;

desc keyspaces will do it for you.

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

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

Apart from above method, if you have opscenter installed,
Go to data tab > there you will see all keyspcaces created by you and some system keyspaces.
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

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

Related

How to list the available tables in different keyspaces in Amazon Keyspaces/Cassandra?

I am trying to implement an application using AWS Keyspace Cassandra and Cassandra Python driver. I had created 3 keyspaces in my AWS console.
I am trying to find if there is any query which can list down all the available tables in all 3 keyspaces in one go.
I tried below query but it's failing.
SELECT table_name, keyspace_name from system_schema.tables where keyspace_name IN ('mykeyspace','cycling')
Does CQL editor in AWS Keyspace not allow use of IN keyword? Anybody knows such a query or command?
It does allow us to use IN clause in Cassandra and even I am able to access the tables with the same query.
cassandra#cqlsh> SELECT table_name, keyspace_name from system_schema.tables where keyspace_name in ('system_traces','youkudbhelper');
table_name | keyspace_name
------------------------+---------------
(10 rows)
cassandra#cqlsh> exit
bash-4.2$ cqlsh --version
cqlsh 5.0.1
bash-4.2$
References:
https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useQueryIN.html
The below query works in Cassandra to list the available tables in different keyspaces.
SELECT table_name, keyspace_name from system_schema.tables where keyspace_name IN ('mykeyspace','cycling');
But it fails for AWS Keyspaces as IN keyword is not supported in AWS Keyspaces yet.

Cassandra result from running query cqlsh from one of the node cluster

I am new to cassandra and haven't finished read the doc yet.
Just want to know if I run cqlsh from one node of a 3 nodes cluster and run query
e.g.
cqlsh node1host -u username -p passwd -k my_cass_keyspace
> select ...
is the result come from all the 3 nodes or it is just result from the node that I run cqsh in?
Sorry for very noob question.
Thanks.
General answer : from all node.
Detailed answer :
Your node might be the coordinator, then depending on your replication factor, the node might fetch data from other nodes ( for example RF 1, then you query on a partition from another node).
This depends on your replication factor, and also your consistency level.
you can check your consistency when using cqlsh by (default is ONE) :
cqlsh> consistency;
Current consistency level is ONE.
You can change it by ( to QUORUM for example) :
cqlsh> CONSISTENCY QUORUM ;
If you want to know details about your request execution plan, try to activate tracing :
cqlsh> tracing on ;
I hope this helps !
The answer is Yes, it comes from all the nodes but it depends upon you cluster configurations, replication factor and consistency level.
For Example:- You have 3 nodes cluster and replication factor is 3 and consistency level is quorum for read and write both so whenever you will do insert query then your data will replicate to all 3 nodes but 2 nodes acknowledgement is sufficient to coordinator as quorum. same fill follow if you do a select query.
You may also refer Cassandra documentation as below:-
http://cassandra.apache.org/doc/latest/architecture/dynamo.html#replication

Import all existing keyspaces schemas

I have 6 keyspaces in cassandra database. I want to migrate all my keyspaces schemas in another cassandra database. How can I do it at once?
DESCRIBE SCHEMA;
will give you DDL statements needed to recreate the non-system keyspaces and tables on a new cluster.
You can run following command to write the scema in cql file
cqlsh -e "Desc keyspace keyspacename" > 'out.cql'
and then use SOURCE to import cql file on another host OR cqlsh -f out.cql optionalHostname

Cassandra system.hints table is empty even when the one of the node is down

I am learning Cassandra from academy.datastax.com. I am trying the Replication and Consistency demo on local machine. RF = 3 and Consistency = 1.
When my Node3 is down and I am updating my table using update command, the SYSTEM.HINTS table is expected to store hint for node3 but it is always empty.
Do I need to make any configurational changes for hints to work or the defaults are ok?
surjanrawat$ ccm node1 nodetool getendpoints mykeyspace mytable 1
127.0.0.3
127.0.0.4
127.0.0.5
surjanrawat$ ccm status
Cluster: 'mycluster'
--------------------
node1: UP
node3: DOWN
node2: UP
node5: UP
node4: UP
cqlsh:mykeyspace> select * from system.hints ;
target_id | hint_id | message_version | mutation
-----------+---------+-----------------+----------
(0 rows)
Did you use the exact same version of Cassandra to create the cluster? Since version 3+ the hints are stored in the local filesystem of the coordinator. I ask this because the exact same thing happened to me during that Datastax demo (I used 3.0.3 instead of 2.1.5) I replayed the steps but with 2.1.5 and the hints where there in the table just as expected.
Came across this post as I ran into the same issue. Latest versions of cassandra don't store the hints in system.hints table. I am using cassandra 3.9 and the hints are stored in the file system.
It is configured in cassandra.yaml file.
# Directory where Cassandra should store hints.
# If not set, the default directory is $CASSANDRA_HOME/data/hints.
# hints_directory: /var/lib/cassandra/hints
In my case I was using ccm and I was able to find the hints at
${user_home}/.ccm/mycluster/node1/hints
where mycluster is my cluster name and node1 is my node name. Hope this helps someone.
In order to understand why no hints are being written you need a good grasp on how Cassandra replicates data.
A replication factor of 2 means two copies of each row, where each
copy is on a different node. All replicas are equally important; there
is no primary or master replica.
With a replication factor of 3 and 5 nodes in the cluster you could lose another node and still not store hints because your data replication strategy is still valid. Try killing two more nodes and then check the hints table.

What is necessary to backup the schema?

The Datastax documentation states
Cassandra can only restore data from a snapshot when the table schema exists. If you have not backed up the schema, [...]
What is required for a full backup of the schema? Simply backing up the system keyspace?
There's no need to backup the system keyspaces, they will be recreated when DSE is installed on the new node. However, you'll need the schema for any user-defined keyspaces.
To backup the schema:
$ cqlsh -e "DESCRIBE SCHEMA;" > schema.out
To restore on a new node:
$ cqlsh < schema.out
In cqlsh (included with Cassandra), use the DESC SCHEMA command.
DESCRIBE [FULL] SCHEMA
Output CQL commands that could be used to recreate the entire (non-system) schema.
Works as though "DESCRIBE KEYSPACE k" was invoked for each non-system keyspace
k. Use DESCRIBE FULL SCHEMA to include the system keyspaces.

Resources