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 ...
随机推荐
- Linux下批量替换文件内容和文件名(转)
1.批量替换指定多个文件的文件内容 在指定目录/your/path里,查找包含old_string字符串的所有文件,并用new_string字符串替换old_string字符串. sed -i &qu ...
- 【GitHub】删除GitHub上的文件
想要删除已经提交上GitHub上的文件, 删除之后,如果这个文件夹下没有文件了,这个文件夹也会被删除! 并且在它的上层文件夹后面 有提示删除了这个文件的信息!!
- cocos2d-x step by step(3) Double Kill
喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈! 这就是最终界面了,没画一个球形 就用一个白色方框代替吧. 啊哈! public ...
- Fresco的使用<一>
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 引入Fresco dependencies { // 添加依赖 compile 'com.facebook.fresco:fre ...
- C# 使用xenocode混淆加密【转】
http://www.cnblogs.com/chendaoyin/archive/2013/05/03/3056692.html 之前就了解过混淆加密工具,但这还是第一次使用,选择了xenoco ...
- Mac 安装配置Mysql
Mac下安装配置Mysql By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 下载安装 去官网下载Community版本号的mysql安装文件. ...
- java性能监控工具jps-windows
jps Lists the instrumented Java Virtual Machines (JVMs) on the target system. This command is experi ...
- Unix系统介绍
一.基础知识 操作系统 用户与计算机硬件之间的界面,是控制.管理计算机内各种硬件与软件资源.它是一个多用户.多任务.分时的操作系统. 对于分时系统:假如a进程需要16个时间片,现在根据优先级只分配了1 ...
- anaconda的所有版本大全--下载地址
地址: https://repo.continuum.io/archive/ 内容: Anaconda installer archive Filename Size Last Modified MD ...
- .NET MVC 4 实现用户注册功能
初学MVC,踩了不少坑,所以通过实现一个用户注册功能把近段时间学习到的知识梳理一遍,方便以后改进和查阅. 问题清单: l 为什么EF自动生成的表名后自动添加了s? l 如何为数据库初始化一些数据? l ...