How to rename keyspace in Cassandra?
Asked Answered
R

2

16

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.

Raguelragweed answered 4/10, 2011 at 14:1 Comment(1)
A manual solution to do that is describe in: #15760167Doro
F
23

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.

Flesh answered 4/10, 2011 at 14:16 Comment(1)
In that case, is it possible for me to export all data from an existing keyspace and then reimport it into a new properly named one, will the sstable importer be of any help?Raguelragweed
P
1

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
Pallor answered 14/9, 2023 at 19:6 Comment(2)
Nice job finding a solution!Megdal
Thank you, it worked for me today. All the steps took around 10 minutes. But the preparation took more time)Pallor

© 2022 - 2024 — McMap. All rights reserved.