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 ...
随机推荐
- CEF General Usage(CEF3预览)
CEF General Usage(CEF3预览) 介绍 CEF全称Chromium Embedded Framework,是一个基于Google Chromium 的开源项目.Google Chro ...
- IntelliJ IDEA常用统一设置2-Inspections检查设置(Linux/Mac/Windows)
Inspections检查设置功能,能检查一些普通问题,比如代码风格等等.当然,这个虽然没有CheckStyle这些插件专业,但也是另一种选择. 官方参考:https://www.jetbrains. ...
- SQL-基础学习2--ORDER BY ,DESC,WHERE, BETWEEN,AND ,OR ,IN ,NOT
所使用的数据库资料在:数据库资料 第三课:排序检索数据 3.1 排序数据 按单列排序 如果不排序,数据一般将以它在底层表中出现的顺序显示,这有可能是数据最初添加到表中的顺序.但是,如果数据随后进行 ...
- Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息。
Windows Server 2003中报PerfDisk “无法从系统读取磁盘性能信息.”的问题解决 2015-01-22 09:49:02 标签:Windows Server2003 PerfDi ...
- 手把手教你安装Hbase,一次成功!
安装环境: OS: Centos 6.5 JDK: jdk1.6.0_18 Hadoop: hadoop-0.20.2 Hbase: hbase-0.90.5 安装准备: 1. Jdk环境 ...
- PS 如何制作WIN7的玻璃化透明窗口效果
1 绘制一个圆角矩形,并将不透明度设为16%以及添加投影效果 2 再次添加外发光效果 3 新建一个图层,再填充一下这个圆角矩形(可以填充为任意颜色,只要和别的颜色区分开来) 4 选中这个区 ...
- npm 安装包无法继续下载? 卡住
一般是由于package.json配置的项目名不对
- C 标准库 - <locale.h>
C 标准库 - <locale.h> 简介 locale.h 头文件定义了特定地域的设置,比如日期格式和货币符号.接下来我们将介绍一些宏,以及一个重要的结构 struct lconv 和两 ...
- Libx264 编码错误 Input picture width(320) is greater than stride (0)
Ffmpeg libx264编码出现 Input picture width(320) is greater than stride (0),问题出在视频格式不正确. libx264 编码要求输入源的 ...
- 【转载】ASP.NET应用程序与页面生命周期
在本文中,我们将了解不同的事件,ASP.NET 应用程序的生命周期以浏览器向 Web 服务器(对于 ASP.NET 应用程序,通常为 IIS)发送请求为起点,直至将请求结果返回至浏览器结束.在这个过程 ...