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', ['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 Cluster cluster = 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
- 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>
); - 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'}); - Add an element to a set using the UPDATE command and the addition (+) operator.
UPDATE users
SET emails = emails + {'fb@friendsofmordor.org'} WHERE user_id = 'frodo'; - 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"} - Remove an element from a set using the subtraction (-) operator.
UPDATE users
SET emails = emails - {'fb@friendsofmordor.org'} WHERE user_id = 'frodo'; - 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数组的更多相关文章
- 关于本地存储构成数组以及jquery的inArray方法的使用
for (var i=0, j = _self.sessionStorage.length; i < j; i++){ var key = _self.sessionStorage.key(i) ...
- JAVA之旅(十七)——StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder
JAVA之旅(十七)--StringBuffer的概述,存储,删除,获取,修改,反转,将缓存区的数据存储到数组中,StringBuilder 讲完String,我们来聊聊他的小兄弟 一.StringB ...
- C#简单实现读取txt文本文件并分页存储到数组
最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...
- Cassandra存储time series类型数据时的内部数据结构?
因为我一直想用Cassandra来存储我们的数字电表中的数据,按照之前的文章(getting-started-time-series-data-modeling)的介绍,Cassandra真的 ...
- 线性表(存储结构数组)--Java 实现
/*线性表的数组实现 *特点:插入删除慢需要平均移动一半的数据,查找较快 *注意:有重复和无重复的数据对应的操作会有些不同 *注意数组一旦创建其大小就固定了 *Java集合长度可变是由于创建新的数组将 ...
- 优先队列(存储结构数组)--Java实现
/*优先队列--是对队列的一种改进 *要存储的数据存在优先级--数值小的优先级高--在队头 *优先队列的实现 *1.数组:适合数据量小的情况(没有用rear+front实现) *优先队列头在items ...
- 队列(存储结构数组)--Java实现
/*队列:其实也是一种操作受限的线性表 *特点:先进先出 *队尾指针:负责元素的进队 *队头指针:负责元素的出队 *注意:普通队--容易浪费空间,一般队列使用最多的就是循环队列--指针环绕 *队列的实 ...
- int 存储大小 数组元素个数
为了得到某个类型或某个变量在特定平台上的准确大小,您可以使用 sizeof 运算符.表达式 sizeof(type) 得到对象或类型的存储字节大小.下面的实例演示了获取 int 类型的大小: 实例 # ...
- 图->存储结构->数组表示法(邻接矩阵)
文字描述 用两个数组分别存储顶点信息和边/弧信息. 示意图 算法分析 构造一个采用邻接矩阵作存储结构.具有n个顶点和e条边的无向网(图)G的时间复杂度是(n*n + e*n), 其中对邻接矩阵G.ar ...
随机推荐
- Leetcode总结之Union Find
package UnionFind; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; p ...
- http://blog.csdn.net/zh521zh/article/details/52687922
http://blog.csdn.net/zh521zh/article/details/52687922
- 修改ViewPager调用setCurrentItem时,滑屏的速度 ,解决滑动之间切换动画难看
在使用ViewPager的过程中,有需要直接跳转到某一个页面的情况,这个时候就需要用到ViewPager的setCurrentItem方法了,它的意思是跳转到ViewPager的指定页面,但在使用这个 ...
- sshpass结合ssh和scp可以自动完成密码登录,无需手动输入密码
使用方法: 1.sshpass -p 123456 ssh admin@1.1.1.1 "touch file" 远程创建文件file 2.sshpass -p 123456 s ...
- odoo税金处理
税金可以设置为'税金包含在价格中',或者'税金不包含在价格中'. 在税金计算处理过程中,odoo会将价格/金额按 total_included/ total_exincluded 分开 ...
- 【CODEFORCES】 B. Dreamoon and Sets
B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 构建基于Javascript的移动web CMS——Hello,World
在一篇构建基于Javascript的移动web CMS入门--简单介绍中简单的介绍了关于墨颀CMS的一些原理,其极框架组成.于是開始接着应该说明一下这个CMS是怎样一步步搭建起来. RequireJS ...
- Activiti实战
说实话,接触Activiti已经是3年前的事情,那时候组里想做一个流程自动化的application,并且记录用户点击.做单量等.第一次听说Activiti,感觉挺好奇的,遂看了下相关的文档跟同事的代 ...
- unity3d开发的android应用中增加AD系统的详细步骤
unity3d开发的android应用中增加AD系统的详细步骤 博客分类: Unity3d unity3d Unity3d已经支持android,怎样在程序里增加admob? 试了一下,确实能够, ...
- 怎样使用在线Webapp生成器生成安装包
在这篇文章中,我们来介绍怎样使用在线(online)的Webapp生成器来生产在Ubuntu手机或模拟器中能够安装的click安装包. Webapp生成器的地址:https://developer.u ...