How do you rename a live Cassandra keyspace through the cassandra-cli? Previous versions had an option in cassandra-cli ("rename keyspace"). However, that option has been dropped in recent releases.
Renaming keyspaces (and column families) is no longer supported, since it was prone to race conditions. See https://issues.apache.org/jira/browse/CASSANDRA-1585.
I recently faced the need to rename a namespace in Cassandra 4. I found a similar question here - Create a duplicate of an existing Keyspace in Cassandra (with a new name). The main idea is a "clone keyspace and delete old instead rename". but the answer provided did not fit my version of Cassandra because nodetool refresh
is deprecated. However, I slightly modified what was suggested there and I managed to do it: the main difference is that I copied the sstables to the folders with the tables 'cassandra/data/keyspace_new/table-*'.
Below is a mini instruction on how to copy a namespace with a new name. The old one can be deleted afterwards.
1 - Export the schema from the old keyspace to a file
/opt/cassandra/bin/cqlsh ip_cassandra -u user -p password -e "DESCRIBE keyspace_name;" > old_keyspace.cql
2 - Replace the keyspace name in the file to new keyspace
3 - Apply the schema to the new keyspace (and the keyspace itself) from the file
/opt/cassandra/bin/cqlsh ip_cassandra -u user -p password -f old_keyspace.cql
Follow steps (4-7) repeat for each node
4 - Clear snapshots
nodetool clearsnapshot --all
5 - Take snapshots from the old keyspace
nodetool flush
nodetool snapshot -t copy old_keyspace
6 - Move the snapshots to the table folders in the new keyspace (see script bellow)
sudo sh clone.sh old_keyspace new_keyspace
so, clone.sh script:
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 keyspace_from keyspace_to"
exit 1
fi
keyspace_from=$1
keyspace_to=$2
for src_table_dir in /data/cassandra/data/$keyspace_from/*-*
do
src_table=$(basename $src_table_dir)
table=$(echo $src_table | cut -d "-" -f 1)
if [ ! -d $src_table_dir/snapshots/copy ]; then
echo "NO SNAPSHOTS DIRECTORY IN $src_table_dir, SKIPPING..."
continue
fi
dest_table_dir=$(find /data/cassandra/data/$keyspace_to/ -maxdepth 1 -type d -name "$table-*")
if [ ! -d $dest_table_dir ]; then
echo "Destination table directory for $table does not exist in $keyspace_to, creating it..."
continue
fi
echo "Moving snapshots from $src_table_dir/snapshots to $dest_table_dir..."
for file in "$src_table_dir"/snapshots/copy/*; do
if [ -f "$file" ]; then
mv "$file" "$dest_table_dir/"
fi
done
if [ $? -ne 0 ]; then
echo "Error during copying, exiting..."
exit 1
fi
done
echo "Done."
7 - Restart Cassandra
sudo systemctl restart cassandra
© 2022 - 2024 — McMap. All rights reserved.