cassandra 存储list数组

Posted 将者,智、信、仁、勇、严也。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cassandra 存储list数组相关的知识,希望对你有一定的参考价值。

demo如下:

CREATE TABLE users3 (
  user_id text PRIMARY KEY,
  first_name text,
  last_name text,
  emails list<text>
);
INSERT INTO users3 (user_id, first_name, last_name, emails) VALUES(frodo, Frodo, Baggins, [[email protected]‘, ‘[email protected]]);
UPDATE users3 SET emails = emails + [[email protected]] WHERE user_id = frodo;  
SELECT user_id, emails FROM users3 WHERE user_id = frodo;  

Collection type 

A collection column is declared using the collection type, followed by another type, such as int or text, in angle brackets. For example, you can create a table having a list of textual elements, a list of integers, or a list of some other element types.

list<text>
list<int>

Collection types cannot be nested, but frozen collection types can be nested inside frozen or non-frozen collections. For example, you may define a list within a list, provided the inner list is frozen:

list<frozen <list<int>>>

Indexes may be created on a collection column of any type.

Using frozen in a collection 

A frozen value serializes multiple components into a single value. Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen type as a blob. The entire value must be overwritten.

column_name collection_type<data_type, frozen<column_name>>

For example:

CREATE TABLE mykeyspace.users (
  id uuid PRIMARY KEY,
  name frozen <fullname>,
  direct_reports set<frozen <fullname>>,     // a collection set
  addresses map<text, frozen <address>>     // a collection map
  score set<frozen <set<int>>>              // a set with a nested frozen set
);


list的话针对下面的{}修改为[]即可!

Using the set type

A set stores a group of elements that are returned in sorted order when queried. A column of type set consists of unordered unique values. Using the set data type, you can solve the multiple email problem in an intuitive way that does not require a read before adding a new email address.

Procedure

  1. Define a set, emails, in the users table to accommodate multiple email address.
    CREATE TABLE users (
      user_id text PRIMARY KEY,
      first_name text,
      last_name text,
      emails set<text>
    );
  2. Insert data into the set, enclosing values in curly brackets.
    Set values must be unique.
    INSERT INTO users (user_id, first_name, last_name, emails)
      VALUES(‘frodo‘, ‘Frodo‘, ‘Baggins‘, {[email protected]‘, [email protected]‘});
  3. Add an element to a set using the UPDATE command and the addition (+) operator.
    UPDATE users
      SET emails = emails + {[email protected]‘} WHERE user_id = ‘frodo‘;
  4. Retrieve email addresses for frodo from the set.
    SELECT user_id, emails FROM users WHERE user_id = ‘frodo‘;
    When you query a table containing a collection, Cassandra retrieves the collection in its entirety; consequently, keep collections small enough to be manageable, or construct a data model to replace collections that can accommodate large amounts of data.

    Cassandra returns results in an order based on the type of the elements in the collection. For example, a set of text elements is returned in alphabetical order. If you want elements of the collection returned in insertion order, use a list.

     user_id | emails
    ---------+-------------------------------------------------------------------
     frodo   | {"[email protected]","[email protected]","[email protected]"}
    
  5. Remove an element from a set using the subtraction (-) operator.
    UPDATE users
      SET emails = emails - {[email protected]‘} WHERE user_id = ‘frodo‘;
  6. Remove all elements from a set by using the UPDATE or DELETE statement.
    A set, list, or map needs to have at least one element; otherwise, Cassandra cannot distinguish the set from a null value.
    UPDATE users SET emails = {} WHERE user_id = ‘frodo‘;
    
    DELETE emails FROM users WHERE user_id = ‘frodo‘;
    A query for the emails returns null.
    SELECT user_id, emails FROM users WHERE user_id = ‘frodo‘;
     user_id | emails
    ---------+------------------------------------------------
     frodo   | null


    参考:http://docs.datastax.com/en/archived/cql/3.0/cql/cql_using/use_list_t.html

以上是关于cassandra 存储list数组的主要内容,如果未能解决你的问题,请参考以下文章

Cassandra List<UDT> 未使用 datastax java driver 4.0.0 反序列化

如何使用Apache Flink阅读Cassandra?

如何从片段内的列表视图打开链接网址?

在 Cassandra 中存储地理位置详细信息和在 Elastic Search 中存储索引的最佳方式?

如何从 cassandra 或 hbase 中提取 leveldb 类型的数据存储(sstable + memtable)?

VSCode自定义代码片段—— 数组的响应式方法