如何将 int 列转换为 Cassandra 数据库中的 float/double 列 table

How to convert int column to float/double column in Cassandra database table

我在production.I中使用cassandra数据库,在
中有一个列字段 cassandra table 例如 coin_deducted 是 int 数据类型。

我需要将 coin_deducted 转换为 float/double 数据类型。 但我尝试使用 alter
更改数据类型 table 命令,但 cassandra 在
时抛出不兼容的问题 将 int 转换为 float。有什么办法吗?

例如:目前显示为:

   user_id | start_time | coin_deducted (int)                    
   122     | 26-01-01   | 12

I want to be 

  user_id | start_time | coin_deducted (float)                    
   122     | 26-01-01   | 12.0

是否可以将整个一列字段复制到新添加的列中 相同的字段 table?

只有在新旧类型兼容的情况下,才能更改列的类型。来自文档:

To change the storage type for a column, the type you are changing to and from must be compatible.

另一个证明这是不可能做到的是当你写声明时:

ALTER TABLE table_name ALTER int_column TYPE float;

它会告诉你类型不兼容。这也是合乎逻辑的,因为 float 是比 int 更广泛的类型(有小数点)并且数据库不知道在小数点上放什么 space。这是 类型的列表,可以毫无问题地将它们更改为另一种类型。

解决方案 1

您可以在应用程序级别执行此操作,在 table 中再创建一个浮动列并创建后台作业,该作业将遍历所有记录并将您的 int 值复制到新的 float列。

我们创建了 cassandra migration tool for DATA and SCHEMA migrations for cases like this, you add it as dependency and can write SCHEMA migration which will add new column and add DATA migration which will fire in background and copy values from old column to new column. Here is a link 到 Java 示例应用程序以查看用法。

解决方案 2

如果您没有应用程序级别并且只想在 CQL 中执行此操作,您可以使用 COPY 命令将数据提取到 CSV,使用浮点创建新的 table,手动对 int 值进行排序CSV 和 return 数据到新的 table.