w e ha v e th e b a s ic C a s s a n d ra d a ta s tru c tu re s : T h e c o lu m n , w h ic h is a n a m e /v a lu e p a ir
T h e ro w , w h ic h is a c o n ta ine r fo r c o lu m n s refe ren c ed b y a p rim a ry k e y Th e ta b le, w h ic h is a
c o n ta in e r fo r ro w s T h e k eys p a c e, w h ic h is a c o n ta in e r fo r ta b le s T h e c lu s ter, w h ic h is a
c o n ta in e r fo r k e ys p a c es tha t s p a n s o n e o r m o re n o d e s
CASSANDRA CLIENTS
T h e re a re a n u m b er o f c lien t d rive rs a v a ila b le fo r C a s s a n d ra a s w e ll, in c lu d in g s u p p o rt fo r
m o s t p o p u la r la n g u a g es . T h ere a re b e n efits to th e s e c lie n ts , in th a t yo u c a n e a s ily em b e d th em
in yo u r o w n a p p lic a tio n s (w h ic h w e ’ ll s e e h o w to d o ) a n d th a t the y freq u en tly o ff er m o re
fe a tu res th a n th e C Q L n a tive in te rfa c e d o e s , in c lu d in g c o n n ec tio n p o o lin g a n d J M X in teg ra tio n
a n d m o n ito rin g .
DataStax Java Driver
T he in tro d u c tio n o f C Q L w a s th e im p etu s fo r a m a jo r s h ift in th e la n d s c a p e o f C a s s a n d ra
c lien t d riv ers . T h e s im p lic ity a n d fa m ilia r s yn ta x o f C Q L m a d e the d ev elo p m en t o f c lien t
p ro g ra m s s im ila r to tra d itio n a l re la tio n al d a ta b a s e d rive rs . D a ta S ta x m a d e a s tra teg ic
in ve s tm e nt o f o p en s o u rc e d rive rs fo r J a va a nd s e ve ral a d d itio n a l la n g u a g es in o rd e r to
c o n tin u e to f ue l C a s s a n d ra a d o p tio n .
T h es e d riv ers q u ic k ly b e c a m e th e d e fa c to s ta n d a rd fo r n e w d e ve lo p m en t p ro je c ts . Y o u
ca n a c c es s th e d riv ers as w ell as a d d itio n a l c o n n ec to rs and to o ls at
h ttp s :// g ith u b .c o m /d a ta s ta x .
Development Environment Configuration
F irs t, w e’ ll n eed to a c c es s th e d rive r in o u r d e ve lo p m en t en v iro n m e n t. W e c o u ld d o w n lo a d
th e d rive r d ire c tly fro m th e U R L lis te d b efo re a n d m a n a g e th e d ep en d e n c ie s m a n u a lly, b u t
it is m o re typ ic a l in m o d e rn J a va d ev elo p m e n t to u s e a to o l lik e M a v en to m a n a g e
d e p e n d e n c ies .
If yo u ’ re u s in g M a ve n , yo u ’ ll n ee d to a d d s o m e th in g lik e th e fo llo w in g to yo u r p ro jec t p o m .
xml
file:
<de pe nd ency >
<gro u pId>c om .da ta sta x.c assa ndra</g rou pId>
<a rtifa ctId> ca ssa ndra -driv er-co re</a rtifa ctId>
<v ersio n>3.0.0</v ersio n>
</de pend ency>
A ll o f th e D a ta S ta x d rive rs a re m a n a g ed as o p e n s o u rc e p ro jec ts o n G itH u b . If yo u ’ re
in te res ted in s e ein g th e J a v a d riv er s o u rc e , yo u c an g et a rea d -o nly tru n k v ers io n u s in g th is
co m m a nd :
$ g it c lo ne h ttp s :/ /g ith u b .c o m / d ata s ta x/ ja v a -d rive r. g it
T h e C lu s te r c la s s is th e m a in e n try p o in t o f the d riv er. It s u p p o rts a flu en t-s tyle A PI u s in g th e
b u ild e rp a tte rn.
F o r ex a m p le, th e fo llo w in g lin es c re a te a c o n n e c tio n to a n o d e ru n n in g o n th e lo c a l h o s t:C lu s ter
c lu s ter
= C lu s te r.b u ild e r( ). a d d C o n ta c tP o in t("1 2 7 .0 .0 . 1 ").b u ild ( );
T h is o n e s ta te m e n t re p res e n ts th e m in im u m re q u ired in fo rm a tio n to c rea te a c lu s te r: a s in g le
c o n ta c t p o in t. W e c a n a ls o s p e c ify m u ltip le c o n ta c t p o in ts . C o n ta c t p o in ts a re s im ila r to th e
c o n c e p t s ee d n o d es th a t a C a s s a n d ra n o d e u s es to c o n n ec t to o th e r n o d e s in th e s a m e c lu s te r.
Protocol version
B y d e fa u lt, th e d rive r u s e s th e p ro to c o l ve rs io n s u p p o rted b y th e firs t n o d e it c o n n ec ts to .
W h ile in m o s t c a s e s th is is s u ffic ie n t, yo u m a y n ee d to o ve rrid e th is b eh a v io r if yo u ’ re
w o rk in g w ith a c lu s ter b a s e d o n a n o ld er v ers io n o f C a s s a n d ra . Y o u c a n s ele c t yo u r p ro to c o l
v ers io n b y p a s s in g th e d es ired v a lu e fro m th e c o m .d a ta s ta x. d rive r.c o re .P ro to c o lV ers io n
en u m e ra tio n to the C lu s ter.B u ild er. w ith P ro to c o lV ers io n () o p e ra tio n .
Compression
T h e d rive r p ro v id es th e o p tio n o f c o m p res s in g m es s a g e s b etw ee n yo u r c lie n t a n d
C a s s a n d ra n o d es ,ta k in g a d v a n ta g e o f th e c o m p re s s io n o p tio n s s u p p o rted b y th e C Q L n a tive
p ro to c o l.
Authentication and encryption
T h e d riv er p ro v id es a p lu g g a b le a u th en tic a tio n m e c h a n is m th a t c a n b e u s e d to s u p p o rt a
s im p le u s ern a m e /p a s s w o rd lo g in , o r in te g ra tio n w ith o th e r a u th e n tic a tio n s ys tem s . B y d ef au lt,
n o a u th en tic a tio n is p erfo rm ed .
Y o u c a n s e le c t a n a u th en tic a tio n p ro vid er b y p a s s in g a n im p le m en ta tio n o f th e
c o m .d a ta s tax .d riv er.c o re.A u th P ro v id e r in terfa c e s u c h a s th e P la in T ex tA u th Pro vid e r to th e
C lu s ter.B u ild er.w ith A u thP ro v id e r( ) o p e ra tio n .
Sessions and Connection Pooling
A f te r w e c re a te o u r C lu s te r in s ta n c e , it is n o t c o nn e c te d to a n y C a s s a n d ra n o d e s u n til w e
in itia liz e it b yc a llin g th e in it() m eth o d :
c lu s [Link] it( );
The Java driver provides several RetryPolicy implementations: T
T h e D e fa u ltR etryPo lic y is a c o n s e rv a tiv e im p lem en ta tio n th a t o n ly retrie s q u eries
u n d e r a n a rro w s e t o f c o n d itio n s . T h e F a llth ro u g h R e tryP o lic y n e ve r rec o m m e n d s a retry, a lw a ys
rec o m m e n d in g th a t th e e xc ep tio n b e re th ro w n .
T h e D o w n g ra d in g C o n s is te n c yR etryP o lic y is a m o re a g g re s s iv e p o lic y w h ic h d o w n g ra d es
th e c o n s is te n c y lev el re q u ired , a s an a ttem p t to g et th e q u e ry to s u c c ee d .
D a ta S ta x P yth o n D riv er
T h e m o s t p o p u la r ea rly P yth o n c lie n t w a s P yc a s s a , w h ic h w a s b u ilt o n th e T h rift
in te rfa c e . T h e P yc a s s a p ro je c t is n o lo n g e r m ain ta in ed , h o w ev er, an d th e rec o m m e n d a tio n is to
u s e th e D a ta S ta x P yth o n D rive r fo r a ll n e w d ev elo p m e n t.
Y o u c a n in s ta ll th e d riv er b y ru n nin g th e P yth o n in s ta ller p ip :
$ pip install cassandra-driver
H ere’ s a s im p le ex a m p le o f c o n n ec tin g to a c lu s te r a n d in s ertin g a ro w in th e h o te ls ta b le :
fro m c a s s a n d ra .c lu s te r im p o rt C lu s te r c lu s te r =
C lu s ter(['1 2 7 . 0 .0 .1 '])s es s io n = c lu s ter.c o n n ec t('h o tel')
s es s io n .ex ec u te ("""
in s ert in to h o tels (id , n a m e, p h o n e ) va lu es
(% s , % s , % s )"""
('A Z 1 2 3 ', 'S u p er H o tel at W e s tW o rld ', '1 -8 8 8 -9 9 9 -9 9 9 9 ')
) D a ta S ta x N o d e .js D rive r
DataStax Ruby Driver
relea s e d in N o ve m b er 2 0 1 4 . Y o u c a n ac c e s s th e R u b y d riv er a t
h ttp s :// g ith u b .c o m /d a ta s ta x /ru b y-d riv er.Y o u c a n in s ta ll th e d rive r u s in g R u b y G em s :
$ g em in s ta ll c a s s a n d ra -d rive r
H ere’ s a n e xa m p le o f c rea tin g a c lu s ter a n d s es s io n a n d ex ec u tin g a s im p le as y nc h ro n o u s
q u e ry th a titera tes o v er th e c o n te n ts o f o u r h o te ls ta b le :
req u ire 'c a s s an d ra '
c lu s ter = C a s s a n d ra .c lu s ter(h o s ts : ['1 2 7 . 0 .0 .1 ']) s e s s io n = c lu s te r. c o n n ec t('h o te l')
fu tu re = s e s s io n .e xe c u te_ a s yn c ( 'S E LE C T * FR O M h o tels ') fu tu re .o n _ s u c c es s d o
|ro w s |ro w s . ea c h d o |ro w |
p u ts "H o te l: # {ro w ['id ']} N a m e : # {ro w ['n a m e']}" en d
en d fu tu re .jo in
CASSANDRA EXAMPLE
G o o d P rim a ry K ey in C a s s a n d ra
Le t’ s ta k e a n ex a m p le a n d fin d w h ic h p rim a ry k ey is g o o d .
M u s ic P la ylis t.
C re ate ta b le M u s ic P la ylis t (
S o n g Id in t, S o n g N a m e tex t, Y ea r
in t,S in g er te xt,
P rim a ry k e y(S o n g Id , S o n g N a m e)
);
S o n g id is th e p a rtitio n k e y, a n d
S o n g N a m e is th e c lu s te rin g
c o lu m n
D a ta w ill b e c lu s te re d o n th e b a s is o f S o n g N a m e .
O n ly o n e p artitio n w ill b e c rea te d w ith th e So n g Id . T h e re w ill n o t b e a n y o th e r p a rtitio n in
th eta b le M u s ic P la ylis t.
D a ta re triev a l w ill b e s lo w b y th is d ata m o d e l d u e to th e b a d p rim a ry k e y.
H ere is a n o th er ta b le M u s ic Pla ylis t.
C re ate ta b le
M u s ic P la ylis t ( S o n g Id in t,
S o n g N a m e tex t,Y ea r in t,
S in g e r tex t,P rim a ry
k ey ((S o n g Id , Y ea r),