作为 Blob 的 Cassandra 集合

Cassandra Collection as Blob

有没有办法在 Cassandra 中将集合存储为 blob 而无需将其转换为文本?这似乎是不可能的。 Cassandra 提供 blob conversion functions 声称支持任何原生非 blob 类型。

For every native, nonblob type supported by CQL, the typeAsBlob function takes a argument of type type and returns it as a blob.

可以理解,如果需要集合类型 成员类型,这种语法对于集合来说可能会变得复杂;例如uuidSetAsText.

例子

列族

> create table blob_test (id text primary key, blob_field blob);

INSERT 文本为 blob

> INSERT INTO blob_test (id, blob_field) VALUES ( '5', textAsBlob('test blob text') );
> select * from blob_test ;

 id | blob_field
----+--------------------------------
  5 | 0x7465737420626c6f622074657874

INSERT 设置为 blob

> INSERT INTO blob_test (id, blob_field) VALUES ( '6', setAsBlob({'1', '2', '3'}) );
InvalidRequest: code=2200 [Invalid query] message="Unknown function setasblob called"

> INSERT INTO blob_test (id, blob_field) VALUES ( '6', textSetAsBlob({'1', '2', '3'}) );
InvalidRequest: code=2200 [Invalid query] message="Unknown function textsetasblob called"

> INSERT INTO blob_test (id, blob_field) VALUES ( '6', collectionAsBlob({'1', '2', '3'}) );
InvalidRequest: code=2200 [Invalid query] message="Unknown function collectionasblob called"

Apache 文档比 DataStax 文档提供更多信息。本机类型和集合类型是两组不同的类型。 "native" 并不仅仅意味着 CQL 本身支持的任何类型。

来自Cassandra Apache docs

<type> ::= <native-type>
         | <collection-type>
         | <tuple-type>
         | <string>       // Used for custom types. The fully-qualified name of a JAVA class

<native-type> ::= ascii
                | bigint
                | blob
                | boolean
                | counter
                | decimal
                | double
                | float
                | inet
                | int
                | text
                | timestamp
                | timeuuid
                | uuid
                | varchar
                | varint

<collection-type> ::= list '<' <native-type> '>'
                    | set  '<' <native-type> '>'
                    | map  '<' <native-type> ',' <native-type> '>'
<tuple-type> ::= tuple '<' <type> (',' <type>)* '>'

解决方法是将集合转换为文本,然后使用 blob 函数。

> INSERT INTO blob_test (id, blob_field) VALUES ( '6', textAsBlob('{"1", "2", "3"}') );

在我的例子中,我只是避免了 blob 并为我需要支持的每种类型添加了一列。