vpggopal
vpggopal

Reputation: 439

Import and export schema in cassandra

How to import and export schema from Cassandra or Cassandra cqlsh prompt?

Upvotes: 41

Views: 57853

Answers (6)

ADITYA BHATIA
ADITYA BHATIA

Reputation: 3

I am currently working with cassandra 1.1 and moving towards 1.2

For older Cassandra versions (<2.0), I found the below to be a handy way:

$ cqlsh -f testFile > user_schema_export.cql 

Usage:

cqlsh [options] [host [port]]
CQL Shell for Apache Cassandra

Options:

  -f FILE, --file=FILE  Execute commands from FILE, then exit

The testFile can contain the DESC command as needed for our keyspace.

 $ vagrant@vagrant-cass-VM:/cassandra/bin$ cat testFile 
   use testkeyspace;
   DESC KEYSPACE testkeyspace;

Upvotes: 0

Shakeel
Shakeel

Reputation: 2015

With authentication

cqlsh -u <user-name> -e "DESC KEYSPACE user" > user_schema.cql

password will be promted.

Upvotes: 0

rouble
rouble

Reputation: 18171

Everything straight from the command line. No need to go into cqlsh.

Import schema (.cql file):

$ cqlsh -e "SOURCE '/path/to/schema.cql'"

Export keyspace:

$ cqlsh -e "DESCRIBE KEYSPACE somekeyspace" > /path/to/somekeyspace.cql

Export database schema:

$ cqlsh -e "DESCRIBE SCHEMA" > /path/to/schema.cql

Upvotes: 14

Raman Yelianevich
Raman Yelianevich

Reputation: 1137

To export keyspace schema:

cqlsh -e "DESC KEYSPACE user" > user_schema.cql

To export entire database schema:

cqlsh -e "DESC SCHEMA" > db_schema.cql

To import schema open terminal at 'user_schema.cql' ('db_schema.cql') location (or you can specify the full path) and open cqlsh shell. Then use the following command to import keyspace schema:

source 'user_schema.cql'

To import full database schema:

source 'db_schema.cql'

Upvotes: 70

dillip
dillip

Reputation: 1842

For someone who comes in future, just to get ddl for schema/keyspace with "myschema" in "CassandraHost" server.

echo -e "use myschema;\nDESCRIBE KEYSPACE;\n" | cqlsh  CassandraHost > mySchema.cdl

and you can use following to import just DDL (without data):

cqlsh  CassandraNEWhost -f mySchema.cdl

Upvotes: 12

Richard
Richard

Reputation: 11100

If using cassandra-cli, you can use the 'show schema;' command to dump the whole schema. You can restrict to a specific keyspace by running 'use keyspace;' first.

You can store the output in a file, then import with 'cassandra-cli -f filename'.

If using cqlsh, you can use the 'describe schema' command. You can restrict to a keyspace with 'describe keyspace keyspace'.

You can save this to a file then import with 'cqlsh -f filename'.

Upvotes: 12

Related Questions