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

  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 users
      SET 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 users
      SET 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



















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6516061.html ,如需转载请自行联系原作者

相关文章
|
7月前
|
Java
Java 中数组Array和列表List的转换
Java 中数组Array和列表List的转换
59 0
|
9月前
|
数据处理 Python
|
26天前
使用List中的remove方法遇到数组越界
使用List中的remove方法遇到数组越界
16 2
|
2月前
|
Java
java List数组根据给定大小分割数组
在获取到很长的数组时,一次性处理数据量太大,需要分批处理,这就需要分批处理了。 1、使用List的subList,封装方法 2、google工具类型Lists的partition 经测试个人推荐使用第一种方法,效率上快了10几倍,估计是因为没有重新生成数组的原因
41 8
|
3月前
|
存储 消息中间件 缓存
Redis不止能存储字符串,还有List、Set、Hash、Zset,用对了能给你带来哪些优势?
Redis不止能存储字符串,还有List、Set、Hash、Zset,用对了能给你带来哪些优势?
|
4月前
|
存储 SQL 关系型数据库
Mysql鸡础(从数据库中导入学生数据用list集合存储emp成员)
Mysql鸡础(从数据库中导入学生数据用list集合存储emp成员)
|
4月前
Spring-数组、List、Set、Map、Properties依赖注入格式
Spring-数组、List、Set、Map、Properties依赖注入格式
23 0
|
4月前
|
存储 C# 索引
C# | 比较IEnumerable、List、数组
IEnumerable`定义了一组用于枚举集合的方法,包括`GetEnumerator`方法,该方法返回一个实现了`IEnumerator`接口的对象,用于枚举集合中的每个元素。`List`和数组都可以使用`foreach`循环来遍历其中的元素,这是因为它们都实现了`IEnumerable`接口。 由于数组在内存中开辟了一段连续的空间,因此可以直接通过索引访问元素,访问速度很快。而 List 则需要通过指针或引用来访问元素,速度相对较慢。 由于数组的大小是固定的,当需要添加或删除元素时,需要重新创建一个新数组,将原数组中的元素复制到新数组中,并添加或删除元素。
56 0
C# | 比较IEnumerable、List、数组
|
4月前
|
Python
Python(十七)python列表List(数组)(3)
九:列表循环 python 复制代码 list_one = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] for item in list_one: print(item) # 获取列表长度 print(len(list_one)) i = 0 # 通过数组长度遍历数组 while(i < len(list_one) ): print(list_one[i]) i += 1 十:列表的切片操作 划重点,列表的切片操作很重要 使用切片操作,切片之后,将产生一个新的列表对象 scss 复制代码 list_one =
25 0
|
4月前
|
索引 Python
Python(十七)python列表List(数组)(2)
四:更新列表 更新列表中的元素。 ini 复制代码 *#* *定义一个列表 *lists = ['Google', 'Runoob', 1997, 2000] *#* *打印更新前元素 *print("第三个元素为 : ", lists[2]) lists[2] = 2001 *#* *打印更新后元素 *print("更新后的第三个元素为 : ", lists[2]) 输出: 第三个元素为 : 1997 更新后的第三个元素为 : 2001 五:删除列表元素 删除元素,可以通过下标删除,也可以通过元素(指定要删除的元素)来删除 Del 关键字是可以将变量从内存
28 0