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', ['f@baggins.com', 'baggins@gmail.com']);
UPDATE users3 SET emails = emails + ['fb@friendsofmordor.org'] WHERE user_id = 'frodo';
SELECT user_id, emails FROM users3 WHERE user_id = 'frodo';  

python代码如下:

from cassandra.cluster import Clustercluster = Cluster(["10.178.209.161"])
session = cluster.connect('my_keyspace')s = session
try:s.execute("CREATE TABLE list_test (a ascii PRIMARY KEY, b list<blob>)")
except:pass
params = ['key1', [bytearray(b'blob1'), bytearray(b'hello world')]]
s.execute("INSERT INTO list_test (a, b) VALUES (%s, %s)", params)
results = s.execute("SELECT * FROM list_test")
print "********************"
for x in results:print x.a, x.b

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', {'f@baggins.com', 'baggins@gmail.com'});

  3. Add an element to a set using the UPDATE command and the addition (+) operator.
    UPDATE usersSET emails = emails + {'fb@friendsofmordor.org'} 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   | {"baggins@caramail.com","f@baggins.com","fb@friendsofmordor.org"}
    

  5. Remove an element from a set using the subtraction (-) operator.
    UPDATE usersSET emails = emails - {'fb@friendsofmordor.org'} 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

转载于:https://www.cnblogs.com/bonelee/p/6516061.html

cassandra 存储list数组相关推荐

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

    JAVA之旅(十七)--StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder 讲完String,我们来聊聊他的小兄弟 一.StringB ...

  2. C#简单实现读取txt文本文件并分页存储到数组

    最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...

  3. iOS中XML解析 (二) libxml2(实例:打印xml内容及存储到数组)

    关联:iOS中XML解析 (一) TBXML (实例:打印xml内容及存储到数组) 关于libxml库的基本使用,在http://xmlsoft.org/网上有文档. 准备工作: project=&g ...

  4. iOS中XML解析 (一) TBXML (实例:打印xml内容及存储到数组)

    关联:iOS中XML解析 (二) libxml2(实例:打印xml内容及存储到数组) 在时间上TBXML占优,libxml2支持了边下载边解析. 来源:http://www.codeios.com/f ...

  5. 编写一个学生类 student,包含的属性有学号、姓名年龄,将所有学生存储在一个数组中

    编写一个学生类 student,包含的属性有学号.姓名年龄,将所有学生存储在一个数组中,自拟数据,用数组的初始化方法给数组赋值,并实现如下操作: ①将所有学生年龄增加一岁 ②按数组中顺序显示所有学生信 ...

  6. 读取CSV文件并将值存储到数组中

    本文翻译自:Reading CSV file and storing values into an array I am trying to read a *.csv -file. 我正在尝试读取*. ...

  7. C语言:从键盘输入10个学生的成绩存储在数组中,求成绩最高者的序号和成绩

    /*从键盘输入10个学生的成绩存储在数组中,求成绩最高者的序号和成绩*/ #include <stdio.h> int main(){int i,n=1;float max,grade[1 ...

  8. jq遍历表格数据存储到数组

    手头上项目需要在日历插件中从前端页面传当前日历的所有日期存储成数组形式传给后台查询当前显示的日期的排班信息表. 可以把页面table 里td里 data-date属性文本内容储存到一个时间数组,传到后 ...

  9. 编写一个程序,用户使用for循环输入5个数字,所有这些数字将存储在一个数组中。之后,程序将添加这五个数字并显示结果。程序必须支持运算符重载的概念。

    Write a program in which users enter 5 numbers using for loop and all these numbers will store in an ...

最新文章

  1. spring cloud微服务分布式云架构-Gateway入门
  2. oracle实现分段,用Oracle分段空间管理功能改进数据库性能
  3. html循环加载多个图片,两行代码实现图片碎片化加载
  4. 《Java8实战》笔记(11):CompletableFuture-组合式异步编程
  5. PKU 学生的反馈 2009-1
  6. 图文+动画讲解排序算法总结!!
  7. 2020微博用户发展报告
  8. 通过子网掩码留一个ip_教大家如何判断俩个IP是不是在同一个网段?什么是子网掩码?...
  9. mysql树形结构的效率_MySQL存储树形数据优化技笔记
  10. 灰度实战(四):Apollo配置中心(4)
  11. linux下桌面快捷方式无法打开,亲测可用:Linux下桌面快捷方式创建实例
  12. 计算机实践学什么作用,大学计算机基础:计算机操作实践
  13. Linux磁盘分区及格式化简介,Linux硬盘分区及格式化学习笔记
  14. Red Hat Enterprise Linux 5.1 Server(正式版)各版本下载2011-04-17 22:21
  15. Python爬虫 抓取大数据岗位招聘信息(51job为例)
  16. 怎么区分静态网页和动态网页
  17. 【Kubernetes离线安装】
  18. 【微信小程序入门到精通】— 配置合法域名、进行网络数据请求(GET、POST)
  19. 修改伪造Flash版本号
  20. 鸿蒙启智 博学多才是什么意思,博学多才是什么意思

热门文章

  1. cannot resolve symbol r_64位ret2_dl_runtime_resolve模版题以及踩坑记录
  2. 九九乘法表用python怎么写_用python做个九九乘法表
  3. 用mysql做文本挖掘_手把手教你做文本挖掘
  4. 容易混淆的php函数,个人笔记
  5. 【django轻量级框架】使用支付宝支付接口(沙箱)
  6. Ubuntu和Linux的区别
  7. ajax核心代码提交,ajax表单在Asp.net核心提交后的RedirectToAction
  8. python中升序降序问题_飘逸的python - 有的升序有的降序的情况下怎么多条件排序...
  9. 计算机在轻化工程中的应用,计算机在基础化学实验当中的应用
  10. 网络推广外包没有效果?很可能是在网络推广外包基础上出现问题!