Last active
April 24, 2018 08:33
-
-
Save bcachet/52f1cf2b1d5cf21a7b70107a2b0f53d0 to your computer and use it in GitHub Desktop.
Cassandra: Export all tables from keyspaces on server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function join | |
{ | |
local IFS="$1"; shift; echo "$*"; | |
} | |
function cqlsh_on | |
{ | |
server=$1 | |
cmd="${@:2}" | |
ssh -T $server << EOSSH | |
cqlsh -e "$cmd" | |
EOSSH | |
} | |
function export_cassandra_keyspaces | |
{ | |
server=$1 | |
keyspaces="${@:2}" | |
out_dir=export-keyspaces-$(join - $keyspaces) | |
for namespace in ${keyspaces}; do | |
tables=$(cqlsh_on $server "USE ${namespace}; DESCRIBE tables;") | |
mkdir -p ${out_dir}/${namespace} | |
for table in ${tables}; do | |
cmd="COPY ${namespace}.${table} TO STDOUT WITH HEADER=true;" | |
cqlsh_on $server $cmd | tee ${out_dir}/${namespace}/${table}.csv | |
done; | |
done; | |
} | |
export_cassandra_keyspaces "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage is: ./export-cassandra-keyspaces.sh HOSTNAME KEYSPACE1 KEYSPACE2
It will generate and export-KEYSPACE1-KEYSPACE2 directory with a directory for each keyspace inside.