向Cassandra数据库添加列而不丢失数据

huangapple go评论72阅读模式
英文:

Add Column to Cassandra db with out losing data

问题

我正在使用集成到Spring Boot应用程序中的Cassandra数据库。

我的问题涉及到模式操作。如果我需要对数据库进行结构更改,比如向表中添加列,那么需要重新创建数据库,但这意味着所有现有数据都会被删除:

schema-action: CREATE_IF_NOT_EXISTS

我唯一成功解决这个问题的方法是使用RECREATE模式操作,但正如前面提到的,这会导致数据丢失。

如何处理这个问题的最佳方法是什么?如何添加结构更改,比如列名,而不必重新创建数据库并且不会丢失所有现有数据?

谢谢

英文:

I am using Cassandra database integrated into a spring boot application.

My Question is around the schema actions. If I need to make structural changes to the DB, say add a column to a table, the database needs to be recreated, however this means all the existing data gets deleted:

schema-action: CREATE_IF_NOT_EXISTS

The only way I have managed to solve this is by using the RECREATE scheme action, but as mentioned earlier, this results in data-loss.

What would be the best approach to handle this? To add structural changes such as a column name with out having to recreate the database and lose all existing data?

Thanks

答案1

得分: 2

卡桑德拉允许您使用 cqlsh 通过 ALTER TABLE 语句修改现有表的架构,而无需从头开始重新创建表。然而,正如链接中所解释的那样,对您可以进行的更改有一些重要限制。您无法完全修改表的主键,可以添加或删除常规列,但不能将列的类型更改为不兼容的类型。

大多数这些限制的原因在于卡桑德拉需要处理已经存在于表中的旧数据。例如,现在要将之前包含字符串的列 A 更改为包含整数,这是没有意义的——我们应该如何处理列 A 中所有旧的非整数值?

正如亚伦在评论中所说,您不太可能希望将这些架构更改作为应用程序的一部分来执行。这些通常是罕见的操作,需要手动完成,或者通过某些管理应用程序来完成,而不是通常的应用程序。

英文:

Cassandra does allow you to modify the schema of an existing table without recreating it from scratch, using the ALTER TABLE statement via cqlsh. However, as explained in that link, there are some important limitations on the kind of changes you can do. You cannot modify the primary key of the table at all, you can add or delete regular columns, and you can't change the type of a column to a non-compatible one.

The reason for most of these limitations is how Cassandra needs to deal with the old data that already exists in the table. For example, it doesn't make sense to say that a column A that until now contained strings - will now contain integers - how are we supposed to handle all the old values in column A which weren't integers?

As Aaron rightly said in a comment, it is unlikely you'll want to do these schema changes as part of your application. These are usually rare operations which are done manually, or via some management application - not your usual application.

huangapple
  • 本文由 发表于 2020年9月17日 21:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63939694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定