【问题标题】:Cassandra - Update Set type columnCassandra - 更新集类型列
【发布时间】:2018-10-28 07:58:11
【问题描述】:

我们在 cassandra 中有一个表,结构如下:

cities_in_state(state TEXT, zip TEXT, cities SET<TEXT>, PRIMARY KEY ((zip, 
state)))

我需要使用 java 驱动程序为州更新城市的值, 有这个代码:

BoundStatement bound = session().prepare("UPDATE cities_in_state SET cities 
= cities + ? WHERE zip = ? and state = ?").bind();
bound.setSet(1, cities);
bound.setString(2, "ZIP1");
bound.setString(3, "state1");

出现“HashSet 无法转换为字符串”之类的错误 而且我应该总是需要在现有城市中添加更多城市。 那么我将如何在 cassandra 中使用绑定参数附加 set 列。

【问题讨论】:

    标签: collections cassandra datastax


    【解决方案1】:

    以下代码可以正常工作:

    PreparedStatement prepared = session.prepare("UPDATE test.st SET cities = cities + ? WHERE zip = ? and state = ?");
    BoundStatement bound = prepared.bind(Collections.singleton("t2"), "2", "1");
    session.execute(bound);
    

    您的问题是您从 1 开始计数,而 Java 驱动程序使用基于 0 的索引。下面的代码工作只是找出如果我将每个索引减少 1:

    BoundStatement bound2 = prepared.bind();
    bound2.setSet(0, Collections.singleton("t3"));
    bound2.setString(1, "2");
    bound2.setString(2, "1");
    session.execute(bound2);
    

    【讨论】:

    • 我的错,我错过了绑定参数索引从零开始的点。 :)
    猜你喜欢
    • 2016-08-10
    • 2018-07-17
    • 1970-01-01
    • 2015-10-31
    • 2017-01-05
    • 2017-04-22
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多