1 of 37

"NoSQL":Apache Cassandra

Introduktion til et geospatielt publikum

2 of 37

Citat

"You know you have a distributed system when the crash of a computer you’ve never heard of stops you from getting any work done"

- Leslie Lamport

3 of 37

Oversigt

  • Hvad er Apache Cassandra?
  • Raison d'être (ja, jeg slog det op i en ordbog)
  • Apache Cassandra versus CAP
  • Datamodel
  • Hands-on eksempel
  • Distribution og replikering
  • Konklusion

4 of 37

Hvad er Apache Cassandra?

1966

1970

1975

2006

2007

2008

2012

IBM opfinder IMS til Saturn V måneraket

The Maintenance of Duplicate Databases, Johnson og Thomas

5 of 37

Hvad er Apache Cassandra?

Typer af "NoSQL"

  • Key/Value�F.eks. Dynamo, Memcached
  • Document stores�F.eks. CouchDB, MongoDB
  • Column-stores�F.eks. Cassandra, BigTable

6 of 37

Hvad er Apache Cassandra?

7 of 37

Hvad er Apache Cassandra?

Write anywhere

(ALL, QUORUM, SINGLE)

8 of 37

Raison d'être

"Starbucks Does Not Use Two-Phase Commit"

- Gregor Hohpe (2004) [link]

Coffee ready!

One coffee please

$2 please

Great!

9 of 37

Raison d'être

  • Høj tilgængelighed
  • Replikation på tværs af flere data centre
  • Fleksibel datamodel

10 of 37

Raison d'être

  • Kan man ikke gøre det samme med RDBMS?
  • Jo, der er specifikke trade-offs (CAP)
  • No silver bullet / no free lunch

11 of 37

CAP

Consistency

Availability

Partition tolerance

Vælg 2 ud af 3

http://en.wikipedia.org/wiki/CAP_theorem

12 of 37

Databaser versus CAP

A

C

P

Neo4J, Google Bigtable, MongoDB, HBase, Hypertable, Redis

"ACID", MySQL, SQL Server, Postgres

DynamoDB, Cassandra, Voldemort, CouchDB, Riak

13 of 37

Postgres (Oracle?) versus CAP

Consistent

Available*

Partition-Tolerant

"Kryds fingre for at det virker"

14 of 37

Google Bigtable versus CAP

Consistent

Available*

Partition-Tolerant

"Kom tilbage når det virker"

15 of 37

Dynamo og Cassandra versus CAP

Consistent

Available*

Partition-Tolerant

"Tak fordi du

handlede hos os"

16 of 37

CAP: Eksempel "Postgres"

Client

Server

Replica

Write

Error

17 of 37

CAP: Eksempel "Cassandra"

Client

Server

Replica

Write

OK

Inconsistent

18 of 37

Datamodel

Har du nogensinde kodet noget à la det her i Java?

... = new HashMap<byte[], HashMap<byte[], HashMap<byte[], byte[]>>>();

19 of 37

Datamodel

Eller måske det her i C#?

... = new Dictionary<byte[], Dictionary<byte[], Dictionary<byte[], byte[]>>>();

Så forstår du allerede Cassandras datamodel!

20 of 37

Datamodel: Byggesten

Column:�( byte[] name, byte[] value, IClock clock )�

Super column:�( byte[] name, Map<byte[], IColumn> cols )�

Row:�( byte[] key, Map<byte[], IColumn cols> )�

Column Family:�( byte[] name, Map<byte[], Row> rows )

21 of 37

Datamodel: Cassandra VS Postgres

*) Minder en anelse om, d.v.s. "på niveau"

Cassandra

PostgreSQL

Column

Column*

(Super) Column

Column (hstore)*

Row

Row*

Column Family

Tabel*

Keyspace

Database*

22 of 37

Datamodel: Datatyper

  • Rå bytes
  • Strings (flere variationer)
  • Tal (flere variationer)
  • Boolean
  • Timestamp
  • Ingen geospatielle datatyper

http://www.datastax.com/docs/1.0/ddl/column_family

23 of 37

Datamodel: Indeksering af geodata

Gør-det-selv løsningen (SimpleGeo)

24 of 37

Hands-on Apache Cassandra

  1. Afhængigheder�java -version # 1.6 u21 eller nyere�Installation�tar xzvf apache-cassandra-1.0.8.tar.gz�sudo mkdir -p /var/log/cassandra�sudo mkdir -p /var/lib/cassandra�sudo chown -R `whoami` /var/log/cassandra�sudo chown -R `whoami` /var/lib/cassandra
  2. Start�cd apache-cassandra-1.0.8�bin/cassandra -f

25 of 37

Hands-on Apache Cassandra

Connected to Test Cluster at localhost:9160.

[cqlsh 2.0.0 | Cassandra 1.0.8 | CQL spec 2.0.0 | Thrift protocol 19.20.0] Use HELP for help.

cqlsh>

$ bin/cqlsh localhost 9160

26 of 37

Hands-on Apache Cassandra

cqlsh> CREATE KEYSPACE wkt_craft WITH

... strategy_class = 'SimpleStrategy'

... AND strategy_options:replication_factor = 1;

cqlsh>

27 of 37

Hands-on Apache Cassandra

cqlsh> USE wkt_craft;

cqlsh> CREATE COLUMNFAMILY players (

... KEY varchar PRIMARY KEY,

... xp bigint);

cqlsh>

28 of 37

Hands-on Apache Cassandra

cqlsh> INSERT INTO players (KEY, xp) VALUES ('kostas', 0);

cqlsh> INSERT INTO players (KEY, xp) VALUES ('lola', 0);

cqlsh> SELECT * from players;

KEY | xp

--------+----

lola | 0

kostas | 0

29 of 37

Hands-on Apache Cassandra

cqlsh> SELECT * from players where xp=0;

Bad Request: No indexed columns present in by-columns clause with "equals" operator

cqlsh> CREATE INDEX xp_key ON players(xp);

cqlsh> SELECT * from players where xp=0;

KEY | xp

--------+----

lola | 0

kostas | 0

30 of 37

Hands-on Apache Cassandra

cqlsh> UPDATE players USING consistency all SET 'x' = 0, 'y' = 0 WHERE key='lola';

cqlsh> SELECT * from players;

KEY,lola | x,0 | xp,0 | y,0

KEY,kostas | xp,0

31 of 37

Replikering

  • Hvad sker der i Cassandra cluster ved inserts?
  • Afhænger af hvordan replikering er sat op...

32 of 37

Replikering

33 of 37

Replikering

Token ring

Placering af data bestemmes af en Partitioner, replication placement strategy og flere andre ting

34 of 37

Replikering

p2p protokol

Udveksling af Merkle-trees

Hinted hands-offs

35 of 37

Konklusion

Fordele:

  • Høj tilgængelighed: Du kan altid skrive til en replica
  • Høj tilgængelighed: Flere datacentre
  • Højt write through-put: Ingen 2PC
  • Høj skalerbarhed: Bygget til "scale out"
  • Skemaløs
  • Kan du lide Java i produktionsmiljøet?

36 of 37

Konklusion

Ulemper:

  • Replicas kan være "out-of-sync", dog kun kortvarigt (eventual consistency)
  • Ingen indbyggede spatielle indexer
  • Skemaløs

37 of 37

Links