欢迎投稿

今日深度:

cassandra 存储list数组,cassandralist数组

cassandra 存储list数组,cassandralist数组


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,如需转载请自行联系原作者

www.htsjk.Com true http://www.htsjk.com/cassandra/11275.html NewsArticle cassandra 存储list数组,cassandralist数组 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 ( '...
评论暂时关闭