如何重命名 Cassandra 中的键空间?

发布于 2024-12-08 15:17:47 字数 102 浏览 1 评论 0原文

如何通过 cassandra-cli 重命名实时 Cassandra 键空间?以前的版本在 cassandra-cli 中有一个选项(“重命名键空间”)。然而,该选项在最近的版本中已被删除。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-12-15 15:17:47

不再支持重命名键空间(和列族),因为它很容易出现竞争条件。请参阅https://issues.apache.org/jira/browse/CASSANDRA-1585

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.

蓬勃野心 2024-12-15 15:17:47

我最近需要在 Cassandra 4 中重命名命名空间。我在这里发现了类似的问题 - 创建 Cassandra 中现有 Keyspace 的副本(使用新名称)。主要思想是“克隆密钥空间并删除旧的而不是重命名”。但提供的答案不适合我的 Cassandra 版本,因为 nodetoolfresh 已弃用。但是,我稍微修改了那里的建议,并设法做到了:主要区别是我将 sstables 复制到带有表“cassandra/data/keyspace_new/table-*”的文件夹中。
以下是有关如何使用新名称复制命名空间的简短说明。旧的可以稍后删除。

1 - 将架构从旧键空间导出到文件

/opt/cassandra/bin/cqlsh ip_cassandra -u user -p password -e "DESCRIBE keyspace_name;" > old_keyspace.cql

2 - 将文件中的键空间名称替换为新键空间

3 - 将架构应用到新键空间(以及密钥空间本身)从文件

/opt/cassandra/bin/cqlsh ip_cassandra -u user -p password -f old_keyspace.cql

按照步骤 (4-7) 对每个节点重复

4 - 清除快照

nodetool clearsnapshot --all

5 - 从以下位置获取快照旧的键空间

nodetool flush
nodetool snapshot -t copy old_keyspace

6 - 移动将快照复制到新键空间中的表文件夹(请参见下面的脚本)

sudo sh clone.sh old_keyspace new_keyspace

因此,clone.sh 脚本:

#!/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 - 重新启动 Cassandra

sudo systemctl restart cassandra

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文