6. What is Apache Cassandra?
• Apache Cassandra is a distributed (“NoSQL”) database
• massively scalable and available
• providing extreme performance
• designed to handle big data workloads
• across multiple data center
• no single point of failure
7. Why use Cassandra?
• Masterless architecture with read/write anywhere design.
• Continuous availability with no single point of failure.
• Gold standard in multi-data center and cloud availability zone support.
• Flexible data model perfect for time series and other data.
• Linear scale performance with online capacity expansion.
• Security with authentication and authorization.
• Operationally simple.
• CQL – SQL-like language.
100,000
txns/sec
200,000
txns/sec
400,000
txns/sec
9. 9
API (CQL & RPC)
CLUSTER (DYNAMO)
DATA STORE (BIG TABLES)
DISKS
Node1
Client request
API (CQL & RPC)
CLUSTER (DYNAMO)
DATA STORE (BIG TABLES)
DISKS
Node2
History of Cassandra
10. Cassandra Architecture Overview
• Cassandra was designed with the understanding
that system/hardware failures can and do occur
• Peer-to-peer, distributed system
• All nodes the same
• Data partitioned among all nodes in the cluster
• Custom data replication to ensure fault tolerance
• Read/Write-anywhere design
11. Cassandra Architecture Highlights
• Each node communicates with each other through the Gossip
protocol, which exchanges information across the cluster every
second
• A commit log is used on each node to capture write activity.
Data durability is assured
• Data also written to an in-memory structure (memtable) and
then to disk once the memory structure is full (an SStable)
22. Overview of Data Partitioning in Cassandra
There are two basic data partitioning strategies:
1. Random partitioning – this is the default and
recommended strategy. Partitions data as evenly as
possible across all nodes using a hash of every column
family row key
2. Ordered partitioning – stores column family row keys
in sorted order across the nodes in a database cluster
26. 5534023222112865484
26
-9223372036854775808
-5534023222112865485
-1844674407370955162
1844674407370955161
Data Distribution
This node owns the token
range:
1844674407370955162
To
5534023222112865484
27. Overview of Replication in Cassandra
• Replication is controlled by what is called the replication
factor. A replication factor of 1 means there is only one
copy of a row in a cluster. A replication factor of 2 means
there are two copies of a row stored in a cluster
• Replication is controlled at the keyspace level in
Cassandra
Original row
Copy of row
28. Replication
28
The token generated by
hashing the partition
key of the row
determines the *first*
natural replica.
The topology strategy
then determines the
others
First Replica
Partition Key Hash Token
29. Cassandra Replication Strategies
• Simple Strategy: places the
original row on a node determined
by the partitioner. Additional replica
rows are placed on the next nodes
clockwise in the ring without
considering rack or data center
location
• Network Topology Strategy:
allows for replication between
different racks in a data center
and/or between multiple data
centers and the cloud. This strategy
provides more control over where
replica rows are placed
30. Simple Topology – Single Datacenter
Second Replica
RF=2
First Replica
{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 };
30
31. Simple Topology – Single Datacenter
Third Replica
Second Replica
RF=3
First Replica
{ 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
31
47. There are scripts
• You can generate the new tokens using scripts like:
48
cassandra_tokens.py 5 8:
[1] Old Node 1 stays at 0
[2] New Node added at 21267647932558653966460912964485513216
[3] Old Node 2 moves to 42535295865117307932921825928971026432
[4] New Node added at 63802943797675961899382738893456539648
[5] Old Node 3 moves to 85070591730234615865843651857942052864
[6] Old Node 4 moves to 106338239662793269832304564822427566080
[7] New Node added at 127605887595351923798765477786913079296
[8] Old Node 5 moves to 148873535527910577765226390751398592512
53. Reading and Writing to Cassandra Nodes
• Cassandra has a ‘location independence’ architecture,
which allows any user to connect to any node in any data
center and read/write the data they need
• All writes being partitioned and replicated for them
automatically throughout the cluster
59. Tunable Data Consistency
• Choose between strong and eventual consistency
(one to all responding) depending on the need
• Can be done on a per-operation basis, and for both
reads and writes
• Handles multi-data center operations
Writes
• Any
• One
• Quorum
• Local_Quorum
• Each_Quorum
• All
Reads
• One
• Quorum
• Local_Quorum
• Each_Quorum
• All
83. Writes (what happens within each node)
• Data is first written to a commit log for durability. Your data is
safe in Cassandra
• Then written to a memtable in memory
• Once the memtable becomes full, it is flushed to an SSTable
(sorted strings table)
• Writes are atomic at the row level; all columns are written or
updated, or none are. RDBMS-styled transactions are not
supported
INSERT INTO…
Commit log memtable
SSTable
Cassandra is known for being the fastest database in the industry where
write operations are concerned.
86. Reads (what happens within each node)
• Depending on the frequency of inserts and updates, a record will
exist in multiple places. Each place must be read to retrieve the
entire record.
• Data is read from the memtable in memory.
• Multiple SSTables may also be read.
• Bloom filters prevent excessive reading of SSTables.
SELECT * FROM…
memtable
SSTable
Bloom Filter
SSTable SSTable SSTable
87. Bloom Filters
• Bloom Filters = all partition keys in data file
• are a space-efficient probabilistic data structure that
is used to test whether an element is a member of a
set
• Cassandra uses bloom filters to save IO when
performing a key lookup
89
88. Deletes
• Hard to delete things in a distributed systems
• Keep track of Replicas
• SSTables are immutable
• Cassandra does not immediately remove data
marked for deletion from disk.
• Data is marked with tombstones
• The deletion occurs during compaction
• If you use the sized-tiered or date-tiered compaction
strategy, you can drop data immediately using a
manual compaction process
91. DataStax OpsCenter
• Visual, browser-based user
interface negates need to install
client software
• Administration tasks carried out
in point-and-click fashion
• Allows for visual rebalance of
data across a cluster when new
nodes are added
• Contains proactive alerts that
warn of impending issues.
• Built-in external notification
abilities
• Visually perform and schedule
backup operations
93. How Does OpsCenter Work?
• Consists of the OpsCenter Server, Client, and Agents
• Server and Client are written in Python
• Agent is written in Java
• Uses keyspace in monitored Cassandra cluster for storing
historical information
• Client runs in standard Web Browser
• Runs on desktop, laptop, and tablet
97. Components of the Column
The column is the fundamental data type in
Cassandra and includes:
• Column name
• Column value
• Timestamp
• TTL (Optional)
98. Column Name
• Can be any value
• Can be any type
• Not optional
• Must be unique
• Stored with every value
99. Column Names can be Timestamps
Symbol 201408280930 201408280931 201408280932
MSFT 44.50 44.60 45.00
AAPL 97.00 100.01 98.57
100. Column Value
• Any value
• Any type
• Can be empty – but is required
101. Column Names and Values
• The data type for a column value is called a validator.
• The data type for a column name is called a
comparator.
• Cassandra validates that data type of the keys of
rows.
• Columns are sorted, and stored in sorted order on
disk, so you have to specify a comparator for
columns. This can be reversed… more on this later
103. Column TimeStamp
• 64-bit integer
• Best Practice
– Should be created in a consistent manner by all
your clients
SELECT WRITETIME (title)
FROM songs WHERE id = 8a172618-b121-4136-bb10-f665cfc469eb;
writetime(title)
------------------
1353890782373000
104. Column TTL
• Defined on INSERT
• Positive delay (in seconds)
• After time expires it is marked for deletion
INSERT INTO clicks
(userid, date, url)
VALUES (123456, ‘2014-08-28 10:00’, ‘http://www.datastax.com’)
USING TTL 86400;
Select TTL(url) FROM clicks
WHERE userid = 123456;
ttl(url)
-----------------
85908
106. Counters
• Allows for addition / subtraction
• 64-bit value
• No timestamp
update recommendation_summary
set num_products = num_products + 1
where recommendation = 'highly recommend';
107. Collections
• Collections give you three types:
- Set (A distinct group of unordered elements)
- List (An ordered sequence of elements)
- Map (Composed of a collection of key, value pairs)
• Each allow for dynamic updates
CREATE TABLE collections_example (
id int PRIMARY KEY,
set_example set<text>,
list_example list<text>,
map_example map<int,text>
);
108. Cassandra Collections - Set
• Set is sorted by CQL type comparator
set_example set<text>
Collection
name
Collection
type
CQL
Type
INSERT INTO collections_example (id, set_example)
VALUES(1, {'1-one', '2-two'});
109. Set Operations
• Adding an element to the set
UPDATE collections_example
SET set_example = set_example + {'3-three'} WHERE id = 1;
• After adding this element, it will sort to the beginning.
UPDATE collections_example
SET set_example = set_example + {'0-zero'} WHERE id = 1;
• Removing an element from the set
UPDATE collections_example
SET set_example = set_example - {'3-three'} WHERE id = 1;
110. Cassandra Collections - List
Ordered by insertion
list_example list<text>
Collection
name
Collection
type
CQL
Type
INSERT INTO collections_example (id, list_example)
VALUES(1, ['1-one', '2-two']);
111. List Operations
• Adding an element to the end of a list
UPDATE collections_example
SET list_example = list_example + ['3-three'] WHERE id = 1;
• Adding an element to the beginning of a list
UPDATE collections_example
SET list_example = ['0-zero'] + list_example WHERE id = 1;
• Deleting an element from a list
UPDATE collections_example
SET list_example = list_example - ['3-three'] WHERE id = 1;
112. Cassandra Collections - Map
• Key and value
• Key is sorted by CQL type comparator
map_example map<int,text>
Collection
name
Collection
type
Value CQL
Type
Key CQL
Type
INSERT INTO collections_example (id, map_example)
VALUES(1, { 1 : 'one', 2 : 'two' });
113. Map Operations
• Add an element to the map
UPDATE collections_example
SET map_example[3] = 'three' WHERE id = 1;
• Update an existing element in the map
UPDATE collections_example
SET map_example[3] = 'tres' WHERE id = 1;
• Delete an element in the map
DELETE map_example[3]
FROM collections_example WHERE id = 1;
10
9
115. Tables (Column Families)
• Similar to relational tables
• Groupings of Rows
• Tunable Consistency
• De-Normalization
• To avoid I/O
• Simplify the Read Path
• Static or Dynamic
116. Key Structure
CREATE TABLE stock(
symbol text,
quote_ts date,
bid double,
ask double,
PRIMARY KEY (symbol, quote_ts)
)
Partition (row) Key Clustering Key
Clustering keys are used to create dynamic tables.
117. Clustering Order
• Sorts columns on disk by default
• Can change the order
CREATE TABLE stock(
symbol text,
quote_ts date,
bid double,
ask double,
PRIMARY KEY (symbol, quote_ts)
)
WITH CLUSTERING ORDER BY (quote_ts DESC);
118. Sorting
• Clustering columns can be used with
ORDER BY
Select * from stock where symbol = ‘IBM’
ORDER BY quote_ts DESC
LIMIT 1;
Returns the most recent quote for IBM:
Symbol Quote_ts Bid Ask
IBM 2014-08-02 189.00 189.50
120. Static Columns
A static column is a special column that is shared by all
the rows of the same partition
CREATE TABLE stock(
symbol text,
quote_ts date,
name text static,
bid double,
ask double,
PRIMARY KEY (symbol, quote_ts)
)
Select * from stock where symbol = ‘IBM’;
121. Static Columns
CQL Rows:
Symbol Name Quote_ts Bid Ask
IBM Inter Bus Mach 2014-08-01 190.00 191.00
IBM Inter Bus Mach 2014-08-02 189.00 189.50
IBM
Storage Row:
Name 2014-08-01:Bid 2014-08-01:Ask 2014-08-02:Bid 2014-08-02:Ask
Inter Bus Mach 190.00 191.00 189.00 189.50
122. Size Limits
• Column family component size considerations
• Data from a one row must fit on one node
• Data from any given row never spans multiple nodes
• For best performance, 100MB per partition
• Maximum columns per row is 2 billion
• In practice – 10 to 20 thousand
• Maximum data size per cell (column value) is 2 GB
• In practice – 10 MB
124. Keyspaces
• Are groupings of Column Families
• Replication strategies
• Replication factor
CREATE KEYSPACE demodb
WITH REPLICATION = {‘class’ : ‘SimpleStrategy’, ‘replication_factor’ : 3};
ALTER KEYSPACE demodb
WITH REPLICATION = {‘class’ : ‘NetworkTopologyStrategy’, ‘DC1’ : 3};
125. Cassandra Query Language - CQL
• Very similar to RDBMS SQL syntax
• Create objects via DDL (e.g. CREATE…)
• Core DML commands supported: INSERT, UPDATE,
DELETE
• Query data with SELECT
SELECT *
FROM USERS
WHERE STATE = ‘TX’;
126. Cassandra Query Language - CQL
DataStax DevCenter – a free, visual query tool for creating and running CQL
statements against Cassandra and DataStax Enterprise.
127. BATCH Statements
BEGIN BATCH
DELETE FROM albums_by_performer
WHERE performer = 'The Beatles' AND
year = 1966 AND title = 'Revolver';
INSERT INTO albums_by_performer (performer, year, title,
genre)
VALUES ('The Beatles', 1966, 'Revolver', 'Rock');
APPLY BATCH;
128. LWT
BEGIN BATCH
INSERT INTO bills (user, balance) VALUES ('user1', -8) IF NOT EXISTS;
INSERT INTO bills (user, expense_id, amount, description, paid)
VALUES ('user1', 1, 8, 'burrito', false);
APPLY BATCH;
BEGIN BATCH
UPDATE bills SET balance = -208 WHERE user='user1' IF balance = -8;
INSERT INTO bills (user, expense_id, amount, description, paid)
VALUES ('user1', 2, 200, 'hotel room', false);
APPLY BATCH;
129. Cassandra Schema Review
• The schema used in Cassandra is modeled after after Google
Bigtable. It is a row-oriented, column structure
• A keyspace is akin to a database in the RDBMS world
• A column family is similar to an RDBMS table but is more
flexible/dynamic
Portfolio Keyspace
Customer Column Family
ID Name SSN DOB
131. User Defined Types
137
CREATE TYPE address (
street text,
city text,
zip_code int,
phones set<text>
)
CREATE TABLE users (
id uuid PRIMARY KEY,
name text,
addresses map<text, address>
)
SELECT id, name, addresses.city, addresses.phones FROM users;
id | name | addresses.city | addresses.phones
--------------------+----------------+--------------------------
63bf691f | chris | Berlin | {’0201234567', ’0796622222'}
132. Secondary Index on Collections
138
CREATE TABLE songs (
id uuid PRIMARY KEY,
artist text,
album text,
title text,
data blob,
tags set<text>
);
CREATE INDEX song_tags_idx ON songs(tags);
SELECT * FROM songs WHERE tags CONTAINS 'blues';
id | album | artist | tags | title
----------+---------------+-------------------+-----------------------+------------------
5027b27e | Country Blues | Lightnin' Hopkins | {'acoustic', 'blues'} | Worrying My Mind
134. It´s all about data
• Sensors
• CPU, Network Card, Electronic Power Meter, Resource Utilization,
Weather
• Clickstream data, WebAnalytics
• Historical trends
• Stock Ticker
• Anything that varies on a temporal basis
• Top Ten Most Popular Videos
140
135. Why Cassandra for Time Series
• Cassandra is based on BigTable storage model
• One key row and lots of (variable) columns
• Single layout on disk
• Cassandra works very well with data in sequence
141
136. Time Series - Table Definition
• Data partitioned by weather station ID and time
• WeatherStationID is PRIMARY KEY (CQL) = PARTITION KEY (Cassandra),
event_time is Clustering Column (Together = Compound Primary Key)
• Clustering determines clustering (storage process that creates index and keeps
data in order based on the index)
• When rows for a partition key are stored in order based on clustering columns
retrieval is very efficient
CREATE TABLE temperature (
weatherstation_id text,
event_time timestamp,
temperature text,
PRIMARY KEY (weatherstation_id,event_time)
);
142
137. Time Series - Example
• Storing weather data, One weather station
• Temperature measurement every minute
• Retrieving data by row key (WeatherStationID) and
column key (event_time) is efficient
143
138. Time Series - INSERT and QUERY
• Inserts are simple and easy
INSERT INTO
temperature(weatherstation_id,event_time,temperature)
VALUES (’1234ABCD’,’2013-04-03 07:01:00′,’72F’);
• Row can be retrieved by Row Key
• Column Value can be retrieved by Row Key and Column Key
• WHERE Statement possible on Primary Key and Indexed Columns
(event_time)
SELECT event_time,temperature
FROM temperature
WHERE weatherstation_id=’1234ABCD’;
144
139. Time Series - Queries
• Queries based on Date and Date ranges are easy
145
140. Time Series - Partitioning
• With the previous table, you can end up with a very large row on 1 partition
i.e. (per millisecond for example)
• This would have to fit on 1 node, Cassandra can store 2 billion columns per
storage row (on one node reads = hotspots)
• The solution is to have a composite Partition Key (date) to split things up:
CREATE TABLE temperature_by_day (
weatherstation_id text,
date text,
event_time timestamp,
temperature text,
PRIMARY KEY ((weatherstation_id,date),event_time)
);
146
141. Time Series - Partitioning
• Using date (portion of timestamp) as available value
• Query all data from a single day
SELECT *
FROM temperature_by_day
WHERE weatherstation_id=’1234ABCD’
AND date=’2013-04-03′;
147
WeatherStationI
D:date
timestamp timestamp timestamp
temperature temperature temperature
142. Data Modeling
• Any questions?
• Feel free to learn more about data modeling online:
Part 1: The Data Model is Dead, Long Live the Data Model
http://www.youtube.com/watch?v=px6U2n74q3g
Part 2: Become a Super Modeler
http://www.youtube.com/watch?v=qphhxujn5Es
Part 3: The World's Next Top Data Model
http://www.youtube.com/watch?v=HdJlsOZVGwM
148
144. DataStax Enterprise Analytics
Cassandra Cluster – Nodes Ring – Column Family Storage
High Performance – Alway Available – Massive Scalability
150
Hadoop
Offline
Application
DataStax Cassandra Enterprise
External Hadoop Distribution
Cloudera, Hortonworks
Spark
Solr
OpsCenter
Hadoop
Monitoring
Operations
Operational
Application
Real Time
Search
Real Time
Analytics
Batch
Analytics
SGBDR
Analytics
Transformations
145. DataStax Enterprise Analytics
• Designed for running analytics on Cassandra data
• There are 4 ways to do Analytics on Cassandra data:
1. Integrated Search (Solr)
2. Integrated Batch Analytics (MapReduce, Hive, Pig,
Mahout) on Cassandra
3. External Batch Analytics (Hadoop; certified with
Cloudera, HortonWorks)
4. Integrated Near Real-Time Analytics (Spark)
151
146. How to analyse?
• If you need to isolate resources for different uses (Transactions vs
Analytics for example) , Cassandra is a great fit.
• You can create separate virtual data centres optimised as required –
different workloads, hardware, availability etc..
• Cassandra will replicate the data for you – no ETL is necessary
Transactions Analytics
152
Cassandra
Replication
148. What is Solr?
• Created by CNet as “enterprise search” wrapper to
Lucene
• Enterprise = Admin Interface, Extensions, API to
manage and structure support (than just free form
text)
• Lucene alone is a powerful information retrieval
engine
• Set of jar´s, lower building blocks
• Elasticsearch is another Lucene wrapper
• Indexes JSON documents
• focus on distributed search server vs. “enterprise
search”
154
149. Solr Search
• Search != Query
• Query: implies an exact set of results
• “give me all cafe visits of this year for user xyz”
• Search: fuzzier, with typical inexact sets of results
• “give me the top web pages containing “apache”
and “cassandra”
155
152. Integrated Hadoop
• Integrated Hadoop 1.0.4
• CFS (Cassandra File System) , no HDFS
• No Single Point of failure
• No Hadoop complexity – every node is built the same
• Hive / Pig / Sqoop / Mahout
Cassandra
Replication
158
Customer
Facing
Hadoop
Nodes
153. Bring Your Own Hadoop
159
Hive
Request
External Hadoop
Resource
Manager
• Analytics from external Hadoop
distribution
• Hadoop 2.x support
• Certified with Cloudera,
Hortonworks
Cassandra
Nodes
156. Apache Spark
• Data model independent queries
• cross-table operations (JOIN, UNION, etc.)!
• complex analytics (e.g. machine learning)
• data transformation, aggregation etc.
• -> Migration, Mutation and Aggregation scenarios!
• stream processing (coming soon)
• all nodes are Spark workers
• by default resilient to worker failures
• first node promoted as Spark Master
• Standby Master promoted on failure
• Master HA available in Datastax Enterprise!
162
168. Why DataStax Enterprise?
DataStax supports both the open source community and modern
business enterprises.
Open Source/Community Enterprise Software
174
• Apache Cassandra (employ
Cassandra chair and 90+% of the
committers)
• DataStax Community Edition
• DataStax OpsCenter
• DataStax DevCenter
• DataStax Drivers/Connectors
• Online Documentation
• Online Training
• Mailing lists and forums
• DataStax Enterprise Edition
• Certified Cassandra
• Built-in Analytics
• Built-in Enterprise Search
• Enterprise Security
• DataStax OpsCenter
• Expert Support
• Consultative Help
• Professional Training
169. DataStax Enterprise vs. Open Source
Feature Open Source Datastax Enterprise
175
Database Software
Data Platform Latest Community Cassandra Production Certified Cassandra
Core security features Yes Yes
Enterprise security features No Yes
Built-in automatic management services No Yes
Integrated analytics No Yes
Integrated enterprise search No Yes
Workload/Workflow Isolation No Yes
Easy migration of RDBMS and log data No Yes
Certified Service Packs No Yes
Certified platform support No Yes
Management Software
OpsCenter Basic functionality Advanced functionality
Services
Community Support Yes Yes
Datastax 24x7x365 Support No Yes
Quarterly Performance Reviews No Yes
Hot Fixes No Yes
Bug Escalation Privilege No Yes
Custom Builds No Option
170. DataStax Enterprise vs. Open Source
176
Standard Pro Max
Server Data Management Components
Production-certified Cassandra Yes Yes Yes
Advanced security option Yes Yes Yes
Repair service Yes Yes Yes
Capacity planning service Yes Yes Yes
Enterprise search (built-in Solr) No Yes Yes
Analytics (built-in Hadoop) No No Yes
Management Tools
OpsCenter Enterprise Yes Yes Yes
Support Services
Expert Support 24x7x1 24x7x1 24x7x1
Partner Development Support Business
hours
Business hours Business
hours
Certified service packs Yes Yes Yes
Hot fixes Yes Yes Yes
Bug escalation Yes Yes Yes
Quarterly performance reviews No No Yes
Bi-weekly call with support team No No Yes
SimpleStrategy places the first replica on a node determined by the partitioner. Additional replicas are placed on the next nodes clockwise in the ring without considering rack or data center location.
The NetworkTopologyStrategy determines replica placement independently within each data center as follows:
The first replica is placed according to the partitioner (same as with SimpleStrategy).
Additional replicas are placed by walking the ring clockwise until a node in a different rack is found. If no such node exists, additional replicas are placed in different nodes in the same rack.
Cassandra uses a snitch to define how nodes are grouped together within the overall network topology (such as rack and data center groupings). The snitch is defined in the cassandra.yaml file
class cassandra.policies.RoundRobinPolicy[source]
A subclass of LoadBalancingPolicy which evenly distributes queries across all nodes in the cluster, regardless of what datacenter the nodes may be in.
This load balancing policy is used by default.
class cassandra.policies.DCAwareRoundRobinPolicy(local_dc, used_hosts_per_remote_dc=0)[source]
Similar to RoundRobinPolicy, but prefers hosts in the local datacenter and only uses nodes in remote datacenters as a last resort.
The local_dc parameter should be the name of the datacenter (such as is reported by nodetool ring) that should be considered local.
used_hosts_per_remote_dc controls how many nodes in each remote datacenter will have connections opened against them. In other words, used_hosts_per_remote_dc hosts will be considered REMOTE and the rest will be considered IGNORED. By default, all remote hosts are ignored.
class cassandra.policies.WhiteListRoundRobinPolicy(hosts)[source]
A subclass of RoundRobinPolicy which evenly distributes queries across all nodes in the cluster, regardless of what datacenter the nodes may be in, but only if that node exists in the list of allowed nodes
This policy is addresses the issue described in https://datastax-oss.atlassian.net/browse/JAVA-145 Where connection errors occur when connection attempts are made to private IP addresses remotely
The hosts parameter should be a sequence of hosts to permit connections to.
class cassandra.policies.TokenAwarePolicy(child_policy)[source]
A LoadBalancingPolicy wrapper that adds token awareness to a child policy.
This alters the child policy’s behavior so that it first attempts to send queries to LOCAL replicas (as determined by the child policy) based on the Statement‘s routing_key. Once those hosts are exhausted, the remaining hosts in the child policy’s query plan will be used.
If no routing_key is set on the query, the child policy’s query plan will be used as is.
You can define the validator and comparator when you create your table schema (which is recommended), but Cassandra does not require it. Internally,
Cassandra stores column names and values as hex byte arrays (BytesType). This is the default client encoding used if data types are not defined in the table schema (or if not specified by the client request).
Cassandra comes with the following built-in data types, which can be used as both validators (row key and column value data types) or comparators (column name data types). One exception is CounterColumnType, which is only allowed as a column value (not allowed for row keys or column names).
SELECT WRITETIME (title) FROM songs WHERE id = 8a172618-b121-4136-bb10-f665cfc469eb;
writetime(title)
------------------
1353890782373000
A counter is a special kind of column used to store a number that incrementally counts the occurrences of a particular event or process. For example, you might use a counter column to count the number of times a page is viewed.
Counter column tables must use Counter data type. Counters may only be stored in dedicated tables.
After a counter is defined, the client application then updates the counter column value by incrementing (or decrementing) it. A client update to a counter column passes the name of the counter and the increment (or decrement) value; no timestamp is required. Internally, the structure of a counter column is a bit more complex. Cassandra tracks the distributed state of the counter as well as a server-generated timestamp upon deletion of a counter column. For this reason, it is important that all nodes in your cluster have their clocks synchronized using a source such as network time protocol (NTP).
Unlike normal columns, a write to a counter requires a read in the background to ensure that distributed counter values remain consistent across replicas. Typically, you use a consistency level of ONE with counters because during a write operation, the implicit read does not impact write latency.
CREATE TABLE playlists (
id uuid,
song_id uuid,
title text,
album text,
artist text,
PRIMARY KEY (id, song_id)
);
Using clustering order
You can order query results to make use of the on-disk sorting of columns. You can order results in ascending or descending order. The ascending order will be more efficient than descending. If you need results in descending order, you can specify a clustering order to store columns on disk in the reverse order of the default. Descending queries will then be faster than ascending ones.
The following example shows a table definition that changes the clustering order to descending by insertion time.
create table timeseries ( event_type text, insertion_time timestamp, event blob, PRIMARY KEY (event_type, insertion_time) ) WITH CLUSTERING ORDER BY (insertion_time DESC);
ORDER BY clauses can select a single column only. That column has to be the second column in a compound PRIMARY KEY. This also applies to tables with more than two column components in the primary key. Ordering can be done in ascending or descending order, default ascending, and specified with the ASC or DESC keywords.
Basically we are saying that you get sorting for free with Cassandra, but it’s not always as straight forward as you would expect.
CREATE KEYSPACE demodb
WITH REPLICATION = {‘class’ : ‘SimpleStrategy’, ‘replication_factor’ : 3};
ALTER KEYSPACE dsg
WITH REPLICATION = {‘class’ : ‘SimpleStrategy’, ‘replication_factor’ : 3};
(1) Netty is a framework for creating Java network applications
Uses Java's efficient NIO capabilities
Provides for asynchronous event notification
Enables quick and easy development of networked applications
See http://netty.io/ for more information
As Cassandra became mature, Thrift turned out to be a limitation: communication limited to the request-response paradigm, no notifications, no streaming, client side interface made of generated code, etc. In Cassandra 1.2, a solution to this second problem was introduced with the CQL Native Protocol, a protocol designed exclusively for CQL 3 and with enough flexibility to enable new features in Cassandra for the years to come.
DataStax Blog: DataStax Java Driver: A new face for Cassandra - Michael Figuiere: http://www.datastax.com/dev/blog/new-datastax-drivers-a-new-face-for-cassandra
Cassandra 2 introduced version 2 (V2) of the native protocol to support some of its additional features