The demo is as follows:
CREATE TABLEUsers3 (user_id text PRIMARY KEY, first_nametext, Last_Nametext, emails list<text>);INSERT intoUSERS3 (user_id, first_name, last_name, emails)VALUES('Frodo','Frodo','Baggins',[' [email protected] ', ' [email protected] ']);UPDATEUsers3SETEmails=Emails+ [' [email protected] '] WHERE user_id = 'Frodo'; SELECT user_id, emails fromUsers3WHERE user_id = 'Frodo';
Collection type
A Collection column is declared using the collection type, followed by another type, such int
as or text
, in angle BRAC Kets. For example, you can create a table has a list of textual elements, a list of integers, or a list of some other element Types.
list<text>list<int>
Collection types cannot is nested, but frozen Collection types can be nested inside frozen or Non-frozen collections. For example, define a list within a list, provided the inner list is frozen:
list<frozen <list<int>>>
Indexes May is created on a collection column of any type.
Using frozen in a collection
A frozen value serializes multiple components to 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.
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 words for the following {} modified to [] can!
Using the set type
A set stores a group of elements that is 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 e-mail problem in an intuitive-to-a-to-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 ( Span class= "hljs-string" > ' Frodo ', ' Frodo ', ' Baggins ', { ' [email protected] '});
- Add an element to a set using the UPDATE command and the addition (+) operator.
Users SET emails = emails + {' Frodo ';
- Retrieve email addresses for Frodo from the set.
' Frodo ';
When you query a table containing a collection, Cassandra retrieves the collection with its entirety; Consequently, keep collections small enough to being manageable, or construct a data model to replace collections that can AC Commodate large amounts of data.Cassandra returns results in a order based on the type of the elements in the collection. For example, a set of the text elements is returned in alphabetical order. If you want elements of the collection returned on Insertion order, use a list.
user_id | Emails---------+-------------------------------------------------------------------Frodo | {"[Email protected]", "[email protected]", "[Email protected]"}
- The Remove an element is from a set using the subtraction (-) operator.
Users SET emails = emails-{' Frodo ';
- Remove all elements from a set by using the UPDATE or DELETE statement.A set, list, or map needs to has at least one element; Otherwise, Cassandra cannot distinguish the set from a null value.
' Frodo '; ' Frodo ';
A query for the emails returns NULL.' Frodo ';
user_id | Emails---------+------------------------------------------------Frodo | null
Reference: http://docs.datastax.com/en/archived/cql/3.0/cql/cql_using/use_list_t.html
Cassandra Storage List Array