通过另一列的值初始化列表

Initializing a list by a value from another column

我在 Cassandra DB 中有一个 table,其中包含一些列,例如:

id (text), ..., data (text).

出于迁移目的,我需要将“数据”的值复制到新列中:data_list (list<text>)。 如何通过 data 列中的值初始化 data_list 列?

我试过:

update t1 set data_list[0] = data where ...;
update t1 set data_list = data where ...;
update t1 set data_list = [ data ] where ...;
update t1 set data_list [0] = (select data from t1 where ...) where ...;
以上的

None 有效。

这可能吗?

不,仅使用 CQL 是不可能的 - 你需要一些代码或工具来做到这一点 - 它应该扫描整个数据库,读取数据并将它们放入目标列。除了尝试编写自己的代码之外,通常很难正确编写代码,您可以使用:

  • DSBulk - you can unload data into CSV or JSON file, convert the data into specific representation by using sed or something like, and load data into the new column。但是转换步骤是你需要实现的,如果你有带引号的数据等,可能很难调试。
  • Spark + Spark Cassandra Connector (even in the local mode) - although it's still a piece of code, it would be easier to implement from my point of view. Just start pyspark with options specified in the documentation,从 Cassandra 读取数据,转换,并将它们存储回 Cassandra。像这样(未测试):
import pyspark.sql.functions as F

df = spark.read\
    .format("org.apache.spark.sql.cassandra")\
    .options(table="tbl", keyspace="ks")\
    .load()

df_with_list = df.select("id", "other_primary_key_columns....", 
    F.array(F.col("data")).alias("data_list"))

df_with_list.write\
    .format("org.apache.spark.sql.cassandra")\
    .mode('append')\
    .options(table="tbl", keyspace="ks")\
    .save()

这里的关键部分是 F.array(F.col("data"))data 列创建一个数组列