1

I am trying YugaByte's Cassandra API (YCQL) and interested in using the JSONB data type extensions.

But I am having trouble both updating an attribute in an existing JSONB column as well as adding a new attribute to an existing JSONB column.

Is this supported in YugaByte? Here is what I tried:

Consider the following example whichhas have one row with a simple key and JSONB column.

cqlsh:k> CREATE TABLE T (key int PRIMARY KEY, value jsonb);
cqlsh:k> INSERT INTO T(key, value) VALUES(1, '{"author": "Charles", "title": "Hello World"}');
cqlsh:k> SELECT * FROM T;

 key | value
-----+--------------------------------------------
   1 | {"author":"Charles","title":"Hello World"}

(1 rows)

So far so good.

If I try to update an existing attribute inside the doc, I see the following error:

cqlsh:k> UPDATE T SET value->'author' = 'Bruce' WHERE key=1;
InvalidRequest: Error from server: code=2200 [Invalid query] message="SQL error: \
Invalid Arguments. Corruption: JSON text is corrupt: Invalid value.

If I try to add a new attribute into an existing JSONB attribute, I get the following error;

cqlsh:k> UPDATE T SET value->'price' = '10' WHERE key=1;
InvalidRequest: Error from server: code=2200 [Invalid query] message="SQL error: \
Execution Error. Could not find member:

Is this supported, and if so what is the correct syntax?

4

1 回答 1

1

更新字符串值时,您必须在单引号内用双引号将新值括起来。例如:

cqlsh:k> UPDATE T SET value->'author' = '"Bruce"' WHERE key=1;
cqlsh:k> SELECT * FROM T;

 key | value
-----+------------------------------------------
   1 | {"author":"Bruce","title":"Hello World"}

(1 rows)

关于添加新属性的能力的第二个问题:

对于 UPDATE,目前(从 1.1 开始)YugaByte DB 允许在该属性/字段已经存在的情况下更新特定属性,但不允许将新属性添加到现有 JSONB 列中。如果需要后者,则需要将旧值读入应用程序,并完整写入新的 json。

于 2019-01-30T18:27:58.627 回答