You are on page 1of 80

:

1101
. .
. .

ssnr

ssnr

(ts sm)

(olumn ml) -

(ksp) -

(olumn) (ro)

(ro k)
:
(olumn nm),
(lu).

(tmstmp)

,
.

http://habrahabr.ru/post/155115/

).

;
.

BtsTp:
AsTp: ASCII
UTF8Type: UTF-8
IntrTp:
Int32Type: 4LongType: 8UUIDType: UUID 1TimeUUIDType: UUID 1DateType: 8BoolnTp:
FloatType: 4DoubleType: 8DmlTp:
CounterColumnType: 8-

4: tru = 1

ls = 0

(consistent hashing).
(token)

(rplton tor).

(ring).

(t runn).
,

http://habrahabr.ru/post/155115/

ONE;
TWO
THREE
QUORUM
LOCAL_QUORUM
EACH_QUORUM
ALL
ANY

,
:

ONE
TWO
THREE
QUORUM
LOCAL_QUORUM
EACH_QUORUM
ALL

http://habrahabr.ru/post/155115/

http://habrahabr.ru/post/155115/

:
(read repair)

(hinted handoff)

notool rpr
,

(anti-entropy node repair)


,

http://habrahabr.ru/post/155115/

(tombstone)

HBs: 4

. Cssnr

,
.

Cassandra

cluster-level
Compaction
:

Thrift

Cassandra
:

ASF

/t/pt/sours.lst

deb http://www.apache.org/dist/cassandra/debian 11x main


deb-src http://www.apache.org/dist/cassandra/debian 11x main

su root

mc

/etc/apt/sources.list

/t/pt/sours.lst

f4,

f2, y

PUBLIC_KEY

gpg --keyserver pgp.mit.edu --recv-keys F758CE318D77295D


gpg --export --armor F758CE318D77295D | sudo apt-key add (

debian packet 0.7.5)

gpg --keyserver pgp.mit.edu --recv-keys 2B5C1B00


gpg --export --armor 2B5C1B00 | sudo apt-key add

Cassandra

apt-get update
apt-get install cassandra
service cassandra restart

. CQLSH

cqlsh
CQL.
CQL SQLCassandra Query Language.
cqlsh.

C*
.

ksp (

).

CREATE KEYSPACE twissandra


ITH strtgss ='SpStrtg
AND strategy_options: replication_factor = '1';

Column Fml (

(
CQL

SQL
.

).
Column Fml,

tl)

CQL

USE twissandra;
CREATE COLUMNFAMILY users (
KEY varchar PRIMARY KEY,
password varchar,
gender varchar,
session_token varchar,
state varchar,
birth_year bigint);

C*
,

PRIMAR KE.

INSERT INTO users (KEY, password) VALUES


('jsmith', 'ch@ngem3a') USING TTL 86400;
SELECT * FROM users;

ALTER COLUMNFAMIL
,

.
oupono

varchar:
ALTER TABLE users ADD coupon_code varchar;

ALTER COLUMNFAMIL,
,

.
oupono

,
,

ALTER TABLE users ALTER coupon_code


TYPE int;


INSERT

86400

UPDATE
smt,

(TTL).

UPDATE

UPDATE users USING TTL 432000 SET 'password' =


'ch@ngem3a' WHERE KEY = 'jsmith';

,
<columnFamily> DROP <column>.
,
:

ALTER TABLE

ALTER TABLE users DROP coupon_code;

oupono
,

sttk

rtr
usrs.

CREATE INDEX state_key ON users


(state);
CREATE INDEX birth_year_key ON users
(birth_year);

DELETE session_token FROM users where


KEY = 'jsmith';

DELETE FROM users where KEY ='jsmith';

DROP COLUMNFAMILY users;


DROP KEYSPACE twissandra;

:
1101
..
. .

Cssnr

1000000
.

(SELECT * FROM

names) :

:
SQLt,

Cssnr
PostrSQL
Brkl

:
1101
. .
. .

Pton

Cassandra

Ap

Python

Apache

Cassandra
Pton

Cssnr

pp
- pip install pycassa

Ap
pss,

ColumnFamily

from pycassa.pool import ConnectionPool


poo = ConntonPoo('Ksp1,'oost:9160'])

ColumnFml:

from pycassa.columnfamily import ColumnFamily


col_fam = ColumnFamily(pool, 'ColumnFamily1')

col_fam.insert('row_key', {'col_name': 'col_val'})


:

col_fam.insert('row_key',
{'col_name':'col_val','col_name2':'col_val2'})

col_fam.batch_insert({'row1': {'name1': 'val1', 'name2': 'val2'},


'row2': {'foo': 'bar'}})

col_fam.get('row_key')

col_fam.get('row_key', columns=['col_name', 'col_name2'])

col_fam.get('row_key', column_start='5', column_finish='7')

col_fam.multiget(['row1', 'row2'])

col_fam.get_count('row_key')

pool =
pycassa.ConnectionPool(keyspace='Keyspace
1', server_list=['10.0.0.4:9160', '10.0.0.5:9160'])

:
1101
. .
. .
. .

Apache Cassandra

Java

Apache Cassandra
Eclipse IDE
Maven 2 Eclipse plug-in

Eclipse

Maven
:

groupId: com.example.cassandra
artifactId: simple-client

Prot Vr
Maven > Add Dependency

tst

om.tst.ssnr

com.example.cassandra.SimpleClient

:
1101
. .
. .
. .

Apache Cassandra

cassandra.yaml
.

cluster_name ,

data_file_directories -

.
,

commitlog_directory -

saved_caches_directory .

seeds -

IP.

listen_address ,

,
, . .

5%

30%

- lolost.

Single-Node Cluster

ssnr.ml
ustrn: 'MCustrN
initial_token: 0

Snl-No,

seeds: <IP of node>


listen_address: <IP of node>
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch|PropertyFileSnitch

root

# service cassandra start

Ubuntu (IP192.168.1.101)
nos P (IP192.168.1.100)

:
: 'JDNots Clustr'

cluster_name -

data_file_directories : d:\cassandra_data\data

nos

commitlog_directory :/var/lib/cassandra/commitlog.
nos
: d:\cassandra_data\commitlog
saved_caches_directory :/var/lib/cassandra/saved_caches.
Windows: d:\cassandra_data\saved_caches
seeds Uuntu.

192.168.1.101.

: /var/lib/cassandra/data.

listen_address 192.168.1.100.

: 192.168.1.101.

Ubuntu - 192.168.1.101,
, . .

0.0.0.0 -

nos P Gossp.

./cassandra -f

Multi-Node or Multi-Data Center Cluster

ssnr.ml:
node2
cluster_name: 'MyDemoCluster'
initial_token:56713727820156410577229101238628035
seed_provider:
- seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.2
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch

node0
cluster_name: 'MyDemoCluster'
initial_token: 0
seed_provider:
- seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.0
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch

node1
lustrnm: 'MDmoClustr
initial_token:2835686391007820528861455061931
seed_provider:
-seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.1
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch

node3
cluster_name: 'MyDemoCluster'
initial_token:850705917302346158658436518579420528
seed_provider:
- seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.3
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch

node4

cluster_name: 'MyDemoCluster'
initial_token: 113427455640312821154458202477256070485
seed_provider:
- seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.4
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch
node5
cluster_name: 'MyDemoCluster'
initial_token: 141784319550391026443072753096570088106
seed_provider:
- seeds: "110.82.155.0,110.82.155.3"
listen_address: 110.82.155.5
rpc_address: 0.0.0.0
endpoint_snitch: RackInferringSnitch

:
1101
. .
. .
. .

Apache Cassandra
PostgreSQL

1000

1000

10000

100000

Cassandra

0,000752

0,000315

0,00087

0,000864

Postgre

0,000237

0,000225

0,000184

0,000162

1000

10000

100000

Cassandra

0,0005

0,000616

0,000638

0,000878

Postgre

0,000395

0,000706

0,00122

0,009146

Cassandra

PostgreSQL

2008

1989

Java
BSD, Linux, OS X, Windows

HP_UX, Linux, OS X, Solaris,


Unix, WIndows

Wide column store

Relational DBMS

C#, C++, Clojure, Erlang, Go,


Haskell, Java, JavaScript,
Perl, PHP, Python, Ruby,
Scala

.Net, C, C++, Java, Perl,


Python, Tcl

CQL

PSQL

API

, ADO.NET,
JDBC ODBC

:
1101
. .
. .
..

CQL

SQL,

SELECT

UPDATE,

INSERT

,
CREATE INDE.

<create-keyspace-stmt> ::= CREATE


KEYSPACE (IF NOT EXISTS)?
<identifier> WITH <properties>

CREATE KEYSPACE Excelsior WITH replication = {'class':


'SimpleStrategy', 'replication_factor' : 3};

<drop-keyspace-stmt> ::= DROP


KEYSPACE ( IF EXISTS )? <identifier>

DROP KEYSPACE myApp;

CREATE TABLE monkeySpecies (


species text PRIMARY KEY,
common_name text,
population varint,
average_size int )
WITH comment='Important biological records'
AND read_repair_chance = 1.0;

<drop-table-stmt> ::= DROP TABLE ( IF EXISTS


)? <tablename>

DROP TABLE worldSeriesAttendees;

<insertStatement> ::= INSERT INTO <tablename>


'(' <identifier> ( ',' <identifier> )* ')'
VALUES '(' <term-or-literal> ( ',' <term-or-literal> )* ')'
( IF NOT EXISTS )?
( USING <option> ( AND <option> )* )?
<term-or-literal> ::= <term>
| <collection-literal>
<option> ::= TIMESTAMP <integer>
| TTL <integer>

INSERT INTO NerdMovies (movie, director, main_actor, year)


VALUES ('Serenity', 'Joss Whedon', 'Nathan Fillion', 2005)
USING TTL 86400;

SELECT name, occupation FROM users WHERE userid IN (199, 200,


207);

SELECT name AS user_name, occupation AS user_occupation


FROM users;

SELECT time, value


FROM events
WHERE event_type = 'myEvent'

AND time > '2011-02-03'


AND time <= '2012-01-01'

SELECT COUNT(*) FROM users;


SELECT COUNT(*) AS user_count FROM users;

<create-index-stmt> ::= CREATE (


CUSTOM )? INDEX ( IF NOT EXISTS )?
<ntr>? ON <tn> '(
<identifier> ')' ( USING <string> ( WITH
OPTIONS = <map-literal> )? )?

CREATE INDEX userIndex ON NerdMovies (user);


CREATE INDEX ON Mutants (abilityId);
CREATE CUSTOM INDEX ON users (email) USING 'path.to.the.IndexClass';
CREATE CUSTOM INDEX ON users (email) USING 'path.to.the.IndexClass' WITH
OPTIONS = {'storage': '/mnt/ssd/indexes/'};

Apache Cassandra [
]:
,http://profyclub.ru/docs/172,

NoSQL-,
:

.
Cssnr 2.0
[
]:
-,
http://habrahabr.ru/post/203200/,
[
]:
: http://wiki.apache.org/cassandra,
p ssnr [
-,
:
http://habrahabr.ru/post/155115/,

CQL3
:

.
,-

.
]:

You might also like