【问题标题】:Cassandra CQL2: Unable to update a columnCassandra CQL2:无法更新列
【发布时间】:2015-06-12 00:12:57
【问题描述】:

嗨,我在 cassandra db 中有一个列族,当我检查表的内容时,它在用作时显示不同

./cqlsh -2
select * from table1;

KEY,31281881-1bef-447a-88cf-a227dae821d6 | A,0xaa| Cidr,10.10.12.0/24 | B,0xac | C,0x01 | Ip,10.10.12.1 | D,0x00000000 | E,0xace | F,0x00000000 | G,0x7375626e657431 | H,0x666230363 | I,0x00 | J,0x353839

虽然输出是这样的

./cqlsh -3
select * from table1;

 key                                  | Cidr          | Ip
--------------------------------------+---------------+------------
 31281881-1bef-447a-88cf-a227dae821d6 | 10.10.12.0/24 | 10.10.12.1

这个值是使用运行的java程序插入的。

我想假设只有在数据库中手动使用 -2 选项时才能看到库尔“B”的更新值,它给我的错误是它是十六进制值。 我正在使用此命令进行更新,但总是出错

cqlsh:sdnctl_db> update table1 SET B='0x7375626e657431' where key='31281881-1bef-447a-88cf-a227dae821d6';

错误请求:无法将“0x7375626e657431”解析为十六进制字节

cqlsh:sdnctl_db> update table1 SET B=0x7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';

错误请求:第 1:30 行在输入“x7375626e657431”处没有可行的替代方案

cqlsh:sdnctl_db> update table1 SET B=7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';

错误请求:第 1:37 行不匹配的字符 '6' 需要 '-'

我只需要插入应用程序会选择但无法插入的十六进制值。

请帮我纠正语法。

【问题讨论】:

    标签: cassandra


    【解决方案1】:

    这取决于 B 列的数据类型。 Here is the reference of the CQL data types. 文档说blob 数据表示为十六进制字符串,所以我的假设是您的B 列也是blob。从 this other cassandra question (and answer) 你可以看到如何将字符串插入到 blob 中。

    cqlsh:so> CREATE TABLE test (a blob, b int, PRIMARY KEY (b));
    cqlsh:so> INSERT INTO test(a,b) VALUES (textAsBlob('a'), 0);
    cqlsh:so> SELECT * FROM test;
    
    b | a
    ---+------
    0 | 0x61
    cqlsh:so> UPDATE test SET a = textASBlob('b') WHERE b = 0;
    cqlsh:so> SELECT * FROM test;
    
     b | a
    ---+------
     0 | 0x62
    

    在您的情况下,您可以将您的十六进制字符串转换为代码中的字符字符串,然后使用 cqlsh 的textAsBlob 函数。

    如果B列的数据类型是int,为什么不在执行插入前将十六进制数转换为int?

    【讨论】:

    • 这些命令使用 CQL 3.0 可以完美运行,但问题是我想编辑的数据只能使用 CQL 2 或通过 cqlsh 附加“-2”选项看到,假设我现在从 -2 选项开始并检查测试表的内容,它们如下 [cqlsh 2.3.0 |卡桑德拉 0.0.0 | CQL 规范 2.0.0 | Thrift 协议 19.35.0] 使用 HELP 寻求帮助。 cqlsh> 使用 so ; cqlsh:so> SELECT * FROM 测试;乙 | '\x00\x00\x00' | '\x00\x01a\x00' ---+----------------+----------------- 0 | 0x | 0x62
    • 现在更新它并添加另一个库作为 cqlsh:so> update test set x='' where b=0; cqlsh:so> SELECT * FROM 测试;乙 | '\x00\x00\x00' | '\x00\x01a\x00' | '\x00\x01x\x00' ---+----------------+-----------------+--- -------------- 0 | 0x | 0x62 | 0x 但是这个命令失败如下 cqlsh:so> update test set x=textAsBlob('5') where b=0;错误请求:第 1:28 行在输入 '(' cqlsh:sdnctl_db> 更新测试集 x=5 where b=0; 错误请求:无法将 '5' 解析为十六进制字节 处没有可行的替代方案
    • reference for blobs in cassandra 1.2 提到set col=0xABC123 命令应该可以工作。但是,其他几个站点表示不推荐使用 cqlsh v2,您应该迁移到 v3。我知道这不能回答你的问题,但我不能在我的机器上真正测试它:cqlsh: error: '2.0.0' is not a supported CQL version.
    猜你喜欢
    • 2014-09-12
    • 2016-02-06
    • 2013-04-08
    • 2023-03-05
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多