Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

Getting Started With Cassandra: CQL Data Types and Using GoCQL

$
0
0

In the first part of this tutorial series, I covered the very basics of Cassandra and used CQLSH to communicate with the database system via shell. In this second part, I will cover in brief the major datatypes available in CQL. Then I will cover the essentials of gocql , a Golang client package which implements the Cassandra driver for Golang. I will cover how to create a session connection with Cassandra with some configuration options and then how to run various queries using the session.

Cassandra provides support for basic datatypes which are available in almost all database systems. Apart from this, it also provides for complex collection types which can store combinations of simple data in the form of list, set, and map. Apart from this, CQL also has support for user-defined types, allowing developers to have their own datatypes which are easy to read and understand.

Basic Data Types ascii: Represents a string of ASCII characters. Insertion of any non-ASCII character into a column of this type would result in an error. bigint: Represents a 64-bit signed long. Used to store long numbers. This should be used only when we are sure we need such long numbers because this occupies more space as compared to int . blob: Used to store arbitrary bytes. This is represented as hexadecimal, and any data without any validation can be stored in this field. boolean: Stores true or false . counter: Represents a 64-bit signed integer, but the value of this column cannot be set. There are only two operations on this column, increment and decrement. In a table with a counter column, only counter types and primary key are allowed. There are no INSERT statements allowed in a table with counter column(s); only UPDATE can be used. For example: > CREATE TABLE website_tracker (
id int PRIMARY KEY,
url text,
visitor_count counter
);
> UPDATE website_tracker
SET visitor_count = visitor_count + 1
WHERE id = 1;
> SELECT * FROM website_tracker;
id | url | count
----+------+------
1 | a.com | 1
(1 rows) date: Represents a date value without a time value. Cassandra encodes the same as an integer value since epoch. Dates can be represented as strings in format yyyy-mm-dd . decimal: Represents a variable-precision decimal value. Best for storing currency or financial values. double: Stores a 64-bit floating point value. float: Stores a 32-bit floating point value. inet: Represents an IP address string in IPv4 or IPv6 format. int: Represents a 32-bit signed integer. Used mostly when storing integer values. smallint: Represents a 2-byte (16-bit) integer. Can be preferred over int for storing small integer values to save space. text: Represents a UTF-8 encoded string. Should be used when we want to store non-ASCII characters. time: Represents a time value. Represented as a string in the format 01:02:03.123 and stored 64-bit signed integer which represents nanoseconds elapsed since midnight. timestamp: Stores both date and time components with millisecond precision. Can be represented as text in the format 2016-12-01 01:02:03.123 . tinyint: Represents a 1-byte (8-bit) integer. Can be preferred over int or smallint for storing small integer values to save space. timeuuid: Stores version 1 UUID. uuid: UUID in standard format. This is a larger value as compared to timeuuid. varchar: Similar to text. Both can be used interchangeably. variant: An integer value with arbitrary precision. It is advised to use a datatype with required precision. Collection Data Types set: This type stores a collection of values. The values are stored as unordered, but CQLSH would return them in a sorted manner. For example, strings would be sorted alphabetically. Let's modify the table we created above: > ALTER TABLE website_tracker ADD tagsSet set<text>;
> UPDATE website_tracker SET tagsSet = {'tag1'} WHERE id = 1;
> SELECT tagsSet FROM website_tracker WHERE id = 1;
tagsSet
----------
{'tag1'}
> UPDATE website_tracker SET tagsSet = tagsSet + {'gat2'} WHERE id = 1;
> SELECT tagsSet FROM website_tracker WHERE id = 1;
tagsSet
------------------
{'gat2', 'tag1'}

You can use the usual set operations like difference to remove elements. To clear out or replace the complete set, do SET tags = {<something>} .

list: A list also stores a collection of values but stores them in ordered fashion, which is by the order of insertion by default. Let's try to do the same thing that we did above with sets with a list now: > ALTER TABLE website_tracker ADD tagsList list<text>;
> UPDATE website_tracker SET tagsList = ['tag1'] WHERE id = 1;
> SELECT tagsList FROM website_tracker WHERE id = 1;
tagsList
----------
['tag1']
> UPDATE website_tracker SET tagsList = tagsList + ['gat2'] WHERE id = 1;
> SELECT tagsList FROM website_tracker WHERE id = 1;
tagsList
------------------
['tag1', 'gat2'] In a list, values can be prepended, subtracted (as in sets), inserted/replaced/deleted by index value ( SET tags[1] = '<somevalue>' ), etc. map: A map contains a collection of key-value pairs. These can be anything except a counter type. Let's have a small description for each tag. > ALTER TABLE website_tracker ADD tagsMap map<text, text>;
> UPDATE website_tracker SET tagsMap = {'tag1': 'Tag One'} WHERE id = 1;
> SELECT tagsMap FROM website_tracker WHERE id = 1;
tagsMap
----------------------
{'tag1': 'Tag One'}
> UPDATE website_tracker SET tagsMap['tag2'] = 'Tag Two' WHERE id = 1;
> SELECT tagsMap FROM website_tracker WHERE id = 1;
tagsMap
------------------
{'tag1': 'Tag One', 'tag2': 'Tag Two'} User-Defined Data Types

It is possible in Cassandra to define our own types. This gives a lot of flexibility and makes overall maintenance of data easier. Let's say we want to store the registration address of the website.

> CREATE TYPE address (
... street text,
... city text,
... state text);
> ALTER TABLE website_tracker ADD reg_address address;

In order to use a user-defined type in a nested collection, we need to specify it as a frozen collection.

> ALTER TABLE website_tracker ADD reg_addresses map<text, frozen<address>>; Using GoCQL

I am assuming that you have some knowledge of using Golang and configuring and installing packages.


Viewing all articles
Browse latest Browse all 6262

Trending Articles