如何使用 Scala 在 Cassandra 的集合文字中绑定变量?

How to bind variables inside collection literals in Cassandra using Scala?

我正在尝试使用我的 Spark Scala 应用程序将数据绑定到 Cassandra 中的映射类型列。

val updateTemplate = s"""UPDATE test_ks.test_table_ttl USING TTL 5
                 |SET ttl_col = ttl_col + {:mapKey : (':value1', ':value2')}
                 |WHERE consumer_id=:consumer_id""".stripMargin
val prep_statement: PreparedStatement = cqlSession.prepare(updateTemplate)

这是一个错误。

Invalid map literal for ttl_col: bind variables are not supported inside collection literals

我的 Cassandra DDL table 是这样的。

CREATE KEYSPACE test_ks
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

CREATE TABLE test_ks.test_table_ttl (
    consumer_id TEXT PRIMARY KEY,
    ttl_col map<text, frozen<tuple<text, text>>>
);

如错误本身所述,集合文字中不支持绑定变量。

因此我们必须更改 updateTemplate 本身。

val updateTemplate = s"""UPDATE $keySpace.$tableName USING TTL 30
                        |SET one_time_pa = one_time_pa + :mapData
                        |WHERE id=:id""".stripMargin

val tupleType: TupleType = DataTypes.tupleOf(DataTypes.TEXT, DataTypes.TEXT)

val rowKey =  // some row key value
val mapKey = // some map key value
val mapValue = mapValueTupleType.newValue(tuple)
val mapData = ImmutableMap.builder().put(mapKey, mapValue).build()
prep_statement.bind(mapData, rowKey)