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
时间: 2024-08-10 02:04:38

cassandra 存储list数组的相关文章

JAVA之旅(十七)——StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder

JAVA之旅(十七)--StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder 讲完String,我们来聊聊他的小兄弟 一.StringBuffer概述 关于StringBuffer这个对象,Buffer是什么意思?缓冲区的意思,String一旦初始化时不可以被改变的,而StringBuffer是可以的,这就是区别,特点: StringBuffer是一个容器 可以字节操作多个数据类型 最终会通过toString方法变成字符串 存储 S

线性存储结构--数组

#include<stdio.h> #include<stdlib.h> //定义了一个数据类型叫struct Arr 该数据类型包含 3个成员 struct Arr{ //12个字节 int *pBase;//存储数组第一个元素的地址 int len;//数组所能容纳的最大元素个数 int cnt;//当前数组有效元素的个数 }; //不用指针 用值得话 导致方法里存在局部变量 不能使主方法的改变 只能传地址 void Init_arr(struct Arr *p,int len

Cassandra存储time series类型数据时的内部数据结构?

因为我一直想用Cassandra来存储我们的数字电表中的数据,按照之前的文章(getting-started-time-series-data-modeling)的介绍,Cassandra真的和适合用于存储time series类型的数据,那么我就想要弄清楚,对于下面这张表 CREATE TABLE temperature ( weatherstation_id text, event_time timestamp, temperature text, PRIMARY KEY (weathers

NSUserDefaults 存储可变数组问题

废话不多说上代码,项目中遇到的问题是: NSMutableArray * arrayLoginNames = [NSMutableArray array]; arrayLoginNames  = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:KLastLoginNameForAutoSendMsgToKeFu]; 这句话取出了数组,可是当对数组添加元素后,进行存储时卡在了下面这句话: [[NSUserDefaults

树的存储方式数组链表+vector

图的邻接矩阵存储法,它的空间和时间复杂度都是N2,现在我来介绍另外一种存储图的方法:邻接表,这样空间和时间复杂度就都是M.对于稀疏图来说,M要远远小于N2 模板如下: struct edge{ int u,v,w,next; }edge[N*3]; int first[N],t; void init() { t = 0; memset(first,-1,sizeof(first)); } void add(int u,int v,int w) { edge[t].u = u;     //u:第

4 C 语言 数值存储方式 数组

源码 补码 反码 数组定义,初始化,使用,随机数 找最大数,逆置,冒泡排序, scanf 输入字符串 字符串处理 字符串溢出等问题 scanf() gets() puts() fputs() strlen() strcat() strncat() strcmp() strncmp() strchr() strstr() strtok() atoi() atof() atol() C 字符串数组 定义数组 遍历输出数组每个元素的值 //GCC 编译方式: C:\MinGW\project>gcc 

线性结构——连续存储【数组】的建立与实现

2015-12-09  20:49:18 1 #include<stdio.h> 2 #include<malloc.h> 3 #include<stdlib.h>//exit() 4 5 struct Arr 6 { 7 int * pBase;//数组第一个元素的地址 8 int len;//长度 9 int cnt;//当前数组有效元素的个数 10 int increment;//自动增长因子 11 }: 12 13 void init_arr(struct Ar

cassandra 存储二进制data

Blob type The Cassandra blob data type represents a constant hexadecimal number defined as 0[xX](hex)+ where hex is a hexadecimal character, such as [0-9a-fA-F]. For example, 0xcafe. The maximum theoretical size for a blob is 2 GB. The practical limi

c++ 将输入存储到数组,然后反转数组,最后输出

// 输入一个包含多个double元素的数组,先打印结果,然后反转出头和尾元素之外的所有元素,最后再打印结果 #include <iostream> using namespace std; int fill_array(double arr[], int size); void show_array(double arr[], int size); void reverse_array(double arr[], int size); int main() { int size; int i