Import all existing keyspaces schemas - cassandra

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

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 drop keyspace, tables not working

I am starting with cassandra and have had some problems. I create keyspaces and tables to go playing, if I delete them and then run a describe keyspace they keep appearing to me. Other times I delete them and it tells me they don't exist, but I can't create them either because it says it exists.
Is there a way to clear that "cache" or something similar?
I would also like to know if through cqlsh I can execute a .cql file that is on my computer.
[cqlsh 5.0.1 | Cassandra 3.11.0 | CQL spec 3.4.4 | Native protocol v4]
This may be due to the eventually consistent nature of Cassandra. If you are on a small test cluster and just playing around you could try doing CONSISTENCY ALL in cqlsh which will force the nodes to become consistent.
You should always run delete or drop command manually with CONSISTENCY ALL so that it will reflect all the nodes and DCs. Also you need to wait for a moment to replicate into the cluster. Once replicated you will not get deleted data else you need to run a repair in the cluster.

How to use describe keypsaces; command in jupyter notebook

I am reading Cassandra database data in Jupiter notebook. In Cassandra we can use this with command "describe keyspaces;".
Suppose, once I am Jupyter is connected Cassandra and I dont want to use Cassandra, want to input Cassandra commands through Jupyter notebook, how to achieve describe keyspaces in order to know the keyspaces
Tried entering describe keyspaces; command
from cassandra.cluster import Cluster
cluster = Cluster(['127.0.0.1']) # provide contact points and port
session = cluster.connect('fiirstkeyspace')
rows = session.execute('select * from books_by_author limit 5 ;')
for row in rows:
print(row)
In the above code, I know I have a keyspace called 'fiirstkeyspace'
however,I want to know all the keyspaces in Cassandra through Jupyter notebook.
show keyspaces;
File "<ipython-input-62-dd2f479cd0fc>", line 1
show keyspaces;
^
SyntaxError: invalid syntax
describe keyspaces;
File "<ipython-input-67-21f5033a29b3>", line 1
describe keyspaces;
^
SyntaxError: invalid syntax
describe keyspaces, etc. are commands that are implemented in cqlsh - they aren't real CQL commands. In Python you can get all this information via Metadata class, that hides implementation details, as schema for system tables could differ between versions.
The code for getting names of all keyspaces is quite simple (cluster is the name of object that you created to connect to Cassandra cluster):
cluster.metadata.keyspaces.keys()
And then you can fetch data about individual keyspaces from cluster.metadata.keyspaces map.

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.

How to list all the available keyspaces in 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

Resources