: kdom8a.ru/seminar/materials/23.05.2014/cassandra.pdf · · 2014-06-28kdvvdqgud z kdvvdqgud²...
TRANSCRIPT
ы : ы ы 1101
. . . .
КssКnНrК
КssКnНrК — , ё
“ - ”.
(kОвspКМО) - (НКtКЛКsО sМСОmК)
(Мolumn ПКmТlв) -
(Мolumn) - (roа kОв) (roа)
: (Мolumn nКmО), (tТmОstКmp) (ЯКluО).
.
, , ( )
— .
http://habrahabr.ru/post/155115/
, :
— ( ).
; — ; — .
ё ; — .
.
— ,
: , , .
BвtОsTвpО: ( ) AsМТТTвpО: ASCII
UTF8Type: UTF-8
IntОРОrTвpО:
Int32Type: 4-
LongType: 8- UUIDType: UUID 1- 4-
TimeUUIDType: UUID 1-
DateType: 8-
BoolОКnTвpО: : truО = 1 ПКlsО = 0
FloatType: 4- DoubleType: 8-
DОМТmКlTвpО:
CounterColumnType: 8- ё
Х
Х
,
(consistent hashing).
(token)
(rОplТМКtТon ПКМtor).
ё (НКtК rОНunНКnМв).
, , .
(ring).
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) — ,
- .
- (anti-entropy node repair) — ,
“noНОtool rОpКТr”
, , .
Ч ё ё , .
http://habrahabr.ru/post/155115/
Х
— — (tombstone)
щ
.
,
. CКssКnНrК
– ,
.
. HBКsО: 4 .
Cassandra
cluster-level
Compaction
: Thrift
Cassandra
ASF :
/ОtМ/Кpt/sourМОs.lТst : deb http://www.apache.org/dist/cassandra/debian 11x main
deb-src http://www.apache.org/dist/cassandra/debian 11x main
su root –
mc –
/ОtМ/Кpt/sourМОs.lТst f4,
f2, y –
:
/etc/apt/sources.list
В Х щ Х:
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 — C* CQL.
CQL — SQL- .
Cassandra Query Language.
cqlsh.
kОвspКМО ( ).
CREATE KEYSPACE twissandra
АITH strКtОgвИМХКss ='SТЦpХОStrКtОgв‘ AND strategy_options: replication_factor = '1';
Column FКmТlв ( ).
C ( Column FКmТlв, SQL CQL М tКЛlО) 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В .
, МouponИМoНО varchar:
ALTER TABLE users ADD coupon_code varchar;
Э , .
Х Х
ALTER COLUMNFAMILВ,
, . ,
МouponИМoНО ,
: ALTER TABLE users ALTER coupon_code TYPE int;
Х Х
INSERT UPDATE (TTL).
УsmТtС, ,
86400 , .
, UPDATE
: UPDATE users USING TTL 432000 SET 'password' = 'ch@ngem3a' WHERE KEY = 'jsmith';
Х Х
, ,
, ALTER TABLE <columnFamily> DROP <column>.
, :
ALTER TABLE users DROP coupon_code;
, -
МouponИМoНО , , .
Х Х
stКtОИkОв ЛТrtСИвОКr
usОrs.
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
.Ю. . .
1000000 CКssКnНrК ,
.
К Х
Х
(SELECT * FROM names) :
: CКssКnНrК PostРrОSQL
SQLТtО, BОrkОlОв
Х№з
ы : ы ы 1101
. . Ш ы . .
PвtСon ApКМСО Cassandra
Python Apache
Cassandra
PвtСon ApКМСО CКssКnНrК pвМКssК,
pТp
- pip install pycassa
ColumnFamily
:
from pycassa.pool import ConnectionPool pooХ = ConnОМtТonPooХ('KОвspКМО1‘,Д'ХoМКХСost:9160']) ColumnFКmТlв:
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='Keyspace1', 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
ProУОМt VТОаОr
Maven > Add Dependency
НКtКstКб
Мom.НКtКstКб.МКssКnНrК
Х
com.example.cassandra.SimpleClient
К Х п
п
:
Х№4
ы : ы ы 1101
. . . . . .
Apache Cassandra
Хcassandra.yaml
cluster_name - . , , .
data_file_directories - , . Э
.
commitlog_directory - , - . Э
.
saved_caches_directory - , .
seeds - IP- , . . , . 5% 30%
.
listen_address - . - loМКlСost. Э , .
Single-Node Cluster
МКssКnНrК.вКml
МХustОrИnКЦО: 'MвCХustОrNКЦО‘ initial_token: 0
SТnРlО-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 (IP- - 192.168.1.101)
- АТnНoаs БP (IP- - 192.168.1.100)
Х :
cluster_name - : 'JDОЯNotОs ClustОr'
data_file_directories - : /var/lib/cassandra/data. АТnНoаs : d:\cassandra_data\data
commitlog_directory - :/var/lib/cassandra/commitlog.
АТnНoаs : d:\cassandra_data\commitlog
saved_caches_directory - :/var/lib/cassandra/saved_caches. Windows: d:\cassandra_data\saved_caches
seeds - - UЛuntu. : 192.168.1.101.
. - 192.168.1.101.
listen_address - Ubuntu - 192.168.1.101, АТnНoаs БP - 192.168.1.100. 0.0.0.0 -
, . . GossТp.
Х
./cassandra -f
Multi-Node or Multi-Data Center Cluster
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
МlustОrИnКmО: 'MвDОmoClustОr‘ 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
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
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
6 МКssКnНrК.вКml:
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
Х№5
ы : ы ы 1101
. . . . . .
Apache Cassandra PostgreSQL
В Х
1000
0 1000 10000 100000
Cassandra 0,000752 0,000315 0,00087 0,000864
Postgre 0,000237 0,000225 0,000184 0,000162
Х
0 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.NEБT, JDBC ODBC
Х№6
ы : ы ы 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 )? <ТНОntТПТОr>? ON <tКЛХОnКЦО> '(‘ <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 - NoSQL- » [Э ]: -,
, - : http://profyclub.ru/docs/172, .
CКssКnНrК 2.0 CQL3 [Э ]: -, : http://habrahabr.ru/post/203200/, .
[Э ]: , - : http://wiki.apache.org/cassandra, .
КpКМСО МКssКnНrК [Э ]: -, :
http://habrahabr.ru/post/155115/, .