Top Banner
Couchbase in the Enterprise with Spring Data and Hibernate OGM Michael Nitschinger Developer Advocate, Couchbase Inc.
55

Couchbase in the Enterprise with Spring Data and Hibernate OGM

Jul 17, 2015

Download

Documents

Couchbase
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase in the Enterprise with Spring Data and Hibernate OGM

Michael Nitschinger Developer Advocate, Couchbase Inc.

Page 2: Couchbase in the Enterprise with Spring Data and Hibernate OGM

•  Developer(Advocate(at(Couchbase,(Inc.(•  Maintainer(of(the(Couchbase(Java(SDK(

•  Speaking(at(Conferences(and(Meetups(

•  Living(and(Working(here(in(Vienna,(Austria(

{“about”:*“me”}*

Page 3: Couchbase in the Enterprise with Spring Data and Hibernate OGM
Page 4: Couchbase in the Enterprise with Spring Data and Hibernate OGM

1.  IntroducCon(to(Couchbase(Server(2.0(2.  OperaCons(&(Document(Design(

3.  Developing(in(Java(with(Couchbase(4.  Spring(Data(&(Hibernate(OGM(IntegraCon(

Agenda*

Page 5: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Workshop*Reminder*

•  Tomorrow*a<er*Lunch*?*Room*4*(13:00*–*17:00):**Discover*NoSQL*Development*with*Couchbase*Server*2.0*

•  Hands?On*&*In?Depth*•  More*on*2.0**Views!*

Page 6: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase*Server*2.0*

Page 7: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Big*Release*in*December*2012*

Page 8: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase*Open*Source*Project*

•  One(of(the(leading(NoSQL(database(projects(focused(on(distributed(database(technology(and(surrounding(ecosystem(

•  Supports(both(keyRvalue(and(documentRoriented(use(cases(

•  All(components(are(available(under(the(Apache*2.0*Public*License*

•  Obtained(as(packaged(soSware(in(both(enterprise(and(community(ediCons.(

Page 9: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Easy*Scalability*

Consistent*High*Performance*

Always*On*24x365*

Grow(cluster(without(applicaCon(changes,(without(downCme(with(a(single(click(

Consistent(subRmillisecond((read(and(write(response(Cmes((with(consistent(high(throughput(

No(downCme(for(soSware(upgrades,(hardware(maintenance,(etc.(

JSONJSONJSON

JSONJSON

PERFORMANCE

Flexible*Data*Model*

JSON(document(model(with(no(fixed(schema.(

Core*Principles*

Page 10: Couchbase in the Enterprise with Spring Data and Hibernate OGM

New*in*2.0*

JSON*support* Indexing*and*Querying*

Cross*data*center*replicacon*Incremental*Map*Reduce*

JSONJSONJSON

JSONJSON

Page 11: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase*Server*2.0*Architecture*

Heartbe

at(

Process(mon

itor(

Global(singleton

(sup

ervisor(

Confi

guraCo

n(manager(

on(each(node(

Rebalance(orchestrator(

Nod

e(he

alth(m

onito

r(

one(per(cluster(

vBucket(state(and

(replicaC

on(m

anager(

hdp*RE

ST*m

anagem

ent*A

PI/W

eb*UI*

HTTP(8091*

Erlang(port(mapper(4369*

Distributed(Erlang(21100*?*21199*

Erlang/OTP*

storage(interface(

Couchbase*EP*Engine*

11210*Memcapable((2.0(

Moxi*

11211*Memcapable((1.0(

Memcached*

New*Persistence*Layer*

8092*Query(API(

Que

ry*Engine*

Data*Manager* Cluster*Manager*

Page 12: Couchbase in the Enterprise with Spring Data and Hibernate OGM

3(3( 2(

Single*node*?*Couchbase*Write*Operacon*

Managed(Cache(

Disk(Que

ue(

Disk(

ReplicaCon(Queue(

App(Server(

Couchbase(Server(Node(

Doc*1*Doc*1*

Doc*1*

To(other(node(

Page 13: Couchbase in the Enterprise with Spring Data and Hibernate OGM

GET*

Doc*1*

3(3( 2(

Single*node*?*Couchbase*Read*Operacon*

Disk(Que

ue(

ReplicaCon(Queue(

App(Server(

Doc*1*

Doc*1*Doc*1*

Managed(Cache(

Disk(

To(other(node(

Couchbase(Server(Node(

Page 14: Couchbase in the Enterprise with Spring Data and Hibernate OGM

COUCHBASE*SERVER**CLUSTER*

Basic*Operacon*

•  Docs*distributed*evenly*across*servers**

•  Each*server*stores*both*accve*and*replica*docs*Only(one(server(acCve(at(a(Cme(

•  Client*library*provides*app*with*simple*interface*to*database*

•  Cluster*map*provides*map**to*which*server*doc*is*on*App(never(needs(to(know(

•  App*reads,*writes,*updates*docs*•  Mulcple*app*servers*can*access*same*document*at*same*cme*

User(Configured(Replica(Count(=(1(

READ/WRITE/UPDATE*

**ACTIVE*

Doc*5*

Doc*2*

Doc*

Doc*

Doc*

SERVER*1* **ACTIVE*

Doc*4*

Doc*7*

Doc*

Doc*

Doc*

SERVER*2*

Doc*8*

**ACTIVE*

Doc*1*

Doc*2*

Doc*

Doc*

Doc*

REPLICA*

Doc*4*

Doc*1*

Doc*8*

Doc*

Doc*

Doc*

REPLICA*

Doc*6*

Doc*3*

Doc*2*

Doc*

Doc*

Doc*

REPLICA*

Doc*7*

Doc*9*

Doc*5*

Doc*

Doc*

Doc*

SERVER*3*

Doc*6*

APP*SERVER*1*

COUCHBASE*Client*Library***CLUSTER*MAP*

COUCHBASE*Client*Library***CLUSTER*MAP*

APP*SERVER*2*

Doc*9*

Page 15: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Add*Nodes*to*Cluster*

•  Two*servers*added*One?click*operacon*

•  Docs*automaccally*rebalanced*across*cluster*Even(distribuCon(of(docs(Minimum(doc(movement(

•  Cluster*map*updated*

•  App*database**calls*now*distributed**over*larger*number*of*servers**

**

REPLICA*

ACTIVE*

Doc*5*

Doc*2*

Doc*

Doc*

Doc*4*

Doc*1*

Doc*

Doc*

SERVER*1* **

REPLICA*

ACTIVE*

Doc*4*

Doc*7*

Doc*

Doc*

Doc*6*

Doc*3*

Doc*

Doc*

SERVER*2* **

REPLICA*

ACTIVE*

Doc*1*

Doc*2*

Doc*

Doc*

Doc*7*

Doc*9*

Doc*

Doc*

SERVER*3* **

SERVER*4* **

SERVER*5*

REPLICA*

ACTIVE*

REPLICA*

ACTIVE*

Doc*

Doc*8* Doc*

Doc*9* Doc*

Doc*2* Doc*

Doc*8* Doc*

Doc*5* Doc*

Doc*6*

READ/WRITE/UPDATE* READ/WRITE/UPDATE*

APP*SERVER*1*

COUCHBASE*Client*Library***CLUSTER*MAP*

COUCHBASE*Client*Library***CLUSTER*MAP*

APP*SERVER*2*

COUCHBASE*SERVER**CLUSTER*

User(Configured(Replica(Count(=(1(

Page 16: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Fail*Over*Node*

**

REPLICA*

ACTIVE*

Doc*5*

Doc*2*

Doc*

Doc*

Doc*4*

Doc*1*

Doc*

Doc*

SERVER*1* **

REPLICA*

ACTIVE*

Doc*4*

Doc*7*

Doc*

Doc*

Doc*6*

Doc*3*

Doc*

Doc*

SERVER*2* **

REPLICA*

ACTIVE*

Doc*1*

Doc*2*

Doc*

Doc*

Doc*7*

Doc*9*

Doc*

Doc*

SERVER*3* **

SERVER*4* **

SERVER*5*

REPLICA*

ACTIVE*

REPLICA*

ACTIVE*

Doc*9*

Doc*8*

Doc* Doc*6* Doc*

Doc*

Doc*5* Doc*

Doc*2*

Doc*8* Doc*

Doc*

•  App*servers*accessing*docs*

•  Requests*to*Server*3*fail*

•  Cluster*detects*server*failed*Promotes(replicas(of(docs(to(acCve(Updates(cluster(map(

•  Requests*for*docs*now*go*to*appropriate*server*

•  Typically*rebalance**would*follow*

Doc*

Doc*1* Doc*3*

APP*SERVER*1*

COUCHBASE*Client*Library***CLUSTER*MAP*

COUCHBASE*Client*Library***CLUSTER*MAP*

APP*SERVER*2*

User(Configured(Replica(Count(=(1(

COUCHBASE*SERVER**CLUSTER*

Page 17: Couchbase in the Enterprise with Spring Data and Hibernate OGM
Page 18: Couchbase in the Enterprise with Spring Data and Hibernate OGM
Page 19: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Operacons*&*Document*Design*

Page 20: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Fundamentals*

•  Every(Document(has(a(Key(assigned(to(it(

•  Keys(­  must(be(max.(255(UTFR8(chars(long(­  must(be(unique(in(a(bucket((“database”)(­  are(completely(under(the(control(of(the(applicaCon(

•  Values(­  can(be(any(binary(blob((but(bonus(points(when(JSON!)(­  can(be(up(to(20MB(in(size(

*

Page 21: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Basic*Store*&*Retrieve*Operacons*

•  get(key)*Retrieve(a(document((

•  set(key,*value)*Store(a(document(or(replace(if(it(exists(

•  add(key,*value)*Store(a(document(and(fail(if(it(exists(

•  replace(key,*value)*Replace(a(document(and(fail(if(it(does(not(exist(

*

Page 22: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Lots*of*other*Operacons*

•  View*operacons*•  Atomic*Counters*(increment,*decrement)*•  Append/Prepend*•  CAS*(Compare*and*Set)*­  OpCmisCc(Locking(

•  “Get*with*Lock”*­  Write(Lock(on(Objects(

•  Bulk*Operacons*­  Saves(network(overhead(

•  Stats*

Page 23: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase*2.0*Bonus*Points:*JSON*

•  JSON(is(a(lightweight*format*to(represent(document(structure(in(a(languageRindependent(manner.(

•  If(JSON(documents(are(stored,(the(View*engine*can(be(used.(

•  Allows(to(build(secondary*indexes*on(your(datasets.(

•  Makes(it(possible(to(ask(quesCons(like(“Give(me(all(user(documents(by(lastname”.(

Page 24: Couchbase in the Enterprise with Spring Data and Hibernate OGM

The*BIG*mental*adjustment*

•  In*SQL,*we*tend*to*avoid*hilng*the*database*as*much*as*possible.*­  Use(JOINs(and(let(the(DB(opCmizer(figure(out(what(to(do(

•  In*Couchbase,*get’s*and*set‘s*are*so*fast*they*are*trivial,(not(boglenecks,(this(is(hard(for(many(people(to(accept(at(first;(Mulcple*get*statements*are*commonplace,*don�t*avoid*it!**

*

Page 25: Couchbase in the Enterprise with Spring Data and Hibernate OGM

meta({(�id�:(�u::[email protected]�,(�rev�:(�1R0002bce0000000000�,(�flags�:(0,(�expiraCon�:(0,(�type�:(�json�(}(((document({(�uid�:(123456,(�firstname�:(�Michael�,(�lastname�:(�Nitschinger�,(�age�:(25,(�favorite_colors�:([�blue�,(�black�],(�email�:(�michael.nitschinger@...�(}(

Meta(InformaCon(Including(Key((All(Keys(Unique(and(Kept(in(RAM(

Document(Value((Most(Recent(In(Ram(And(Persisted(To(Disk(

Document*Structure*with*JSON*

Page 26: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Views*&*Indexes*

• Views*can*cover*a*few*different*use*cases*­ Simple(secondary(indexes((the(most(common)(­ Complex(secondary,(terCary(and(composite(indexes(­ AggregaCon(funcCons((reducCon)(• Example:(count(the(number(of(North(American(Ales(

­ Organizing(related(data(• Built*using*Map/Reduce*­ Map(funcCon(creates(the(index((bRtree)(­ Reduce(funcCon(summarizes((reduces)(informaCon(­ Wrigen(using(superfast(Javascript((V8)(

Page 27: Couchbase in the Enterprise with Spring Data and Hibernate OGM

A*View*in*Accon*

Page 28: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Couchbase*&*Java*

Page 29: Couchbase in the Enterprise with Spring Data and Hibernate OGM

www.couchbase.com/develop(

Python(Ruby(

Go( Clojure(

Official*SDKs*

Community*&*Libraries*

Page 30: Couchbase in the Enterprise with Spring Data and Hibernate OGM

•  Current*Version:*1.1.5*(supports*1.8*and*2.0)*•  Extends*the*Spymemcached*Library*•  Asynchronous*where*possible*•  Dependencies:*­  Negy,(Apache(HTTPCore((+NIO),(Jetson(

•  Either*Download**(hdp://www.couchbase.com/develop/java/current)*

•  Or*include*through*Maven*(hdp://files.couchbase.com/maven2/)*

About*the*SDK*(1)*

Page 31: Couchbase in the Enterprise with Spring Data and Hibernate OGM

•  Hides*the*complexity*of*­  different(protocols(used((memcached,(hgp)(­  cluster(configuraCon(changes((add/remove)(­  failover(scenarios(

•  Provides*­  a(unified(API(for(keyR(and(viewRbased(access(­  APIs(for(bucket(and(design(document(management(­  extended(debugging(and(logging(faciliCes(

About*the*SDK*(2)*

Page 32: Couchbase in the Enterprise with Spring Data and Hibernate OGM

•  Either(directly(through(CouchbaseClient*or(customize(opCons(by(using(the(CouchbaseConnecconFactoryBuilder.(­  You(need(at(least:(List(of(Nodes,(Bucket(Name(and(Password(

Simple*Instancacon*

Page 33: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Key?Based*Access*

•  Mutacng*Documents*

•  Retrieving*Documents*

*

Page 34: Couchbase in the Enterprise with Spring Data and Hibernate OGM

View*Access*

•  Steps*to*Follow:*1.  Fetch(the(View(2.  Init(Query(object(3.  Query(the(View(

Page 35: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Spring*&*Hibernate*Integracon*

Page 36: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Spring*Data*

•  Umbrella*project*to*­  (provide(unified(access(pagerns(­  on(a(variety(of(databases/frameworks(­  without(loosing(flexibility(­  and(beger(integraCon(into(the(Spring(ecosystem(

•  Support*for*­  JPA(­  Hadoop(­  MongoDB(­  Neo4J(­  ....(

Page 37: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Spring*Data*Couchbase*

•  Integracon*with*Spring*Data*­  Templates(­  Repositories(­  ExcepCon(Mapping(­  also(@Cacheable(

•  Maps*POJO*Encces*to*JSON*(and*back)*

•  Scll*under*development!*

Page 38: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Transparent*Caching*

•  Very(easy(and(transparent(Caching(mechanism(

•  Use(@Cacheable,(@CacheEvict(or(@CachePut(

•  Support(for(SpEL(in(those(annotaCons(

•  Translates(to(get/set/delete(calls(behind(the(scenes(•  Get(distributed,(high(performance(caching(in(a(mager(of(

minutes!(

Page 39: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Cache*Configuracon*(XML)*

Page 40: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Cache*Configuracon*(Java)*

Page 41: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Cache*Annotacon*

Page 42: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Run*and*Execute*

Page 43: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Spring*Data*Encty*

Page 44: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Applicacon*Config*

Page 45: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Spring*Data*Template*

Page 46: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Hibernate*OGM*

•  Hibernate*„Object/Grid“*Mapper*

•  JPA*for*NoSQL*•  Hibernate*Sub*Project*

•  Mulcple*Persistence*Store*­  Infinispan(­  EHCache(­  MongoDB(­  Couchbase*­  Neo4J(

*

Page 47: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Hibernate*OGM*+*Couchbase*

•  ParCal(Query(Support(•  RelaConships(•  SCll(under(heavy(development(

•  Work(with(your(EnCCes((POJOs)(in(a(tradiConal((and(natural(way(

•  Integrated(ConfiguraCon(for(your(Persistence.xml(

(

Page 48: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Example*Encty*

Page 49: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Store*a*Document*with*Relacons*

Page 50: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Fetch*the*Document*back*

Page 51: Couchbase in the Enterprise with Spring Data and Hibernate OGM

What*got*stored*in*Couchbase?*

Key: {tbl:Dog,id:1} !{! "id": 1,! "name": "Dina",! "breed_id": "fc5ba547-b86a-47a4-ad84-9aed4b8c7024"!}!

Key: {tbl:Breed,id:fc5ba547-b86a-47a4-ad84-9aed4b8c7024} !{! "id": "fc5ba547-b86a-47a4-ad84-9aed4b8c7024",! "name": "Collie"!}!

Page 52: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Shameless*Plug*

•  Couchbase*DeveloperDays*­  Munich,(April(23th(­  Vienna,(April(24th((

(use(„MICHAEL“(as(the(discount(code!)(­  Prague,(April(26th(

•  Couchbase*[UK]:*April*11th*

Page 53: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Q*&*A*

Page 54: Couchbase in the Enterprise with Spring Data and Hibernate OGM

Thank*you!*[email protected](

@daschl((

Get(Couchbase(Server(at((hgp://www.couchbase.com/download(

Page 55: Couchbase in the Enterprise with Spring Data and Hibernate OGM