couchbase mobile on android

38
Developing with Couchbase Mobile David Maier May the 27 th ‘14

Upload: philipp-fehre

Post on 10-May-2015

399 views

Category:

Engineering


2 download

DESCRIPTION

This presentation was given by David Maier @magicable @munichnosql may 2014. The code can be found https://github.com/dmaier-couchbase/cbl-android-tasklist

TRANSCRIPT

Page 1: Couchbase Mobile on Android

Developing  with  Couchbase  Mobile  

 David  Maier  May  the  27th  ‘14  

Page 2: Couchbase Mobile on Android

Agenda  

About Couchbase

Further Examples

Questions?

Introduction to Couchbase Mobile

Demo

Page 3: Couchbase Mobile on Android

About  Couchbase  

Page 4: Couchbase Mobile on Android

Fastest growing NoSQL database company���> 4x growth in 2013; Widely recognized as industry leader

Worldwide operations with > 150 employees���>75 in engineering; more than doubling every year

Support offices in US (multiple), UK, India, Japan, China (soon)

Leader in scalability & high performance Easily & reliable scale your app; Get consistent low latency & high throughput

Only NoSQL vendor with mobile database + sync Recognized as most innovative NoSQL vendor

Provide KV & document database for web & mobile apps Flexible, schemaless JSON data model; 100% open source

Mission critical deployments at large enterprises & internet companies

> 400 customers; >10,000 paid production nodes deployed; worldwide customer base

Couchbase, Inc. Confidential

Page 5: Couchbase Mobile on Android

Market  Adop9on  

> 400 customers; > 10,000 production nodes deployed

Internet Companies Enterprises

Page 6: Couchbase Mobile on Android

Overview  

Couchbase offers a full range of Data Management solutions

High Availability

Cache

Key Value

Document

Mobile device

SSN: 400 658 9993 Pass: ******

Pass: ******

Page 7: Couchbase Mobile on Android

Couchbase  Server  Is  The  Complete  Solu9on  

One click scalability and no app changes.

Sub millisecond latency with high throughput for reads and writes.

Maintenance, upgrades and cluster resizing all online

without application downtime

JSON document model with no fixed schema.

Consistent High Performance

Flexible ���Data Model

Easy Scalability

Always On 24x7x365

Page 8: Couchbase Mobile on Android

Introduc9on  to  Couchbase  Mobile  

Page 9: Couchbase Mobile on Android

The  Next  Wave  of  Mobile  Apps  …will  be  ALWAYS  available  

Data  

Data  

Page 10: Couchbase Mobile on Android

JSON  Anywhere    

Couchbase  Server  

•  JSON  on  the  wire  ­  No  need  for  data  

transforma;on  

•  JSON  in  the  cloud  ­  Flexible  data  model  ­  High  performance  ­  Easy  scalability  

ServerSync GatewayLiteJS N

JS N

JS N •  JSON  on  the  device  

­  Developers  increasingly  prefer  NoSQL  database  

Page 11: Couchbase Mobile on Android

Architecture  

   

SERVER  3      

SERVER  1      

SERVER  2  

Couchbase  Server  

   

Channel

Sync  Gateway  

Channel

   

Channel

Sync  Gateway  

Channel

Couchbase  Lite  for  iOS  and  Android  

On Premise In the cloud

Page 12: Couchbase Mobile on Android

Couchbase  Lite  The  only  NoSQL  Database  for  Mobile  Devices  

•  Features  ­  Ultra-­‐lightweight,  secure  JSON  database    

­  Na;ve  support  for  iOS,  Android  and  REST/HTML5  

­  Full  document,  index  and  querying  and  sync  capabili;es  

­  Powerful  conflict  resolu;on  Lite

Couchbase  Lite    

Android  

Couchbase  Lite    iOS  

Page 13: Couchbase Mobile on Android

Couchbase  Lite  Full  Feature  List  

Features   Benefits  

Na9ve  APIs     Manage  your  mobile  database  using  APIs  op;mized  specifically  for  iOS  and  Android  

REST  APIs   REST  APIs  provide  an  alterna;ve  access  method  based  on  your  development  needs  

JSON  support     Use  a  flexible  data  model  designed  for  mobile  object-­‐oriented  apps.  Adapt  to  your  applica;on  needs  with  immediacy  and  li[le  impact  

Easy  sync  with  Couchbase  Sync  Gateway   Get  sync-­‐ready  in  less  than  a  few  lines  of  code.  Focus  on  applica;on  development,  not  syncing  

Peer  to  peer  support  via  REST  APIs      

Communicate  with  nearby  devices,  even  offline,  with  our  REST  API-­‐enabled  P2P  support  

Data  rou9ng  via  channels     Get  users  only  the  data  they  need  for  a  focused,  relevant  app  experience  

Changes  Feed        

Provides  developers  visibility  and  no;fica;on  into  data  changes    

Indexing  and  querying  for  JSON   Use  powerful  secondary  indexes  to  query  your  data  on  the  device  

Authen9ca9on  plug-­‐ins   Use  popular  exis;ng  third-­‐party  authen;ca;on  services  like  Facebook  and  Mozilla  Persona  instead  of  wri;ng  your  own  

ATachment  support  on  device  and  in  the  cloud   Manage  binary  data,  like  photos  and  large  files,  separately  from  your  JSON  documents  for  op;onal,  speedier  sync  

Page 14: Couchbase Mobile on Android

What  you  get  

Lite

Couchbase  Lite    

Android  

Couchbase  Lite    iOS  

­  Enable  new  class  of  rich  data  intensive  local  applica;ons    

­  Rapid  development  using  na;ve  JSON  data  

­  Highly  responsive  interac;ve  applica;ons  

­  Always  available  -­‐    online  or  offline  

Page 15: Couchbase Mobile on Android

Couchbase  Sync  Gateway  Easy,  Reliable  Data  Sync  to  the  Cloud  

•  Features  •  Dynamic  sync  capabili;es  via  

Sync  Func;on  APIs  •  Easy  Administra;on  •  Seamless  scaleout  

•  Benefits  ­  10x  reduc;on  in  development  

;me  ­  Scales  to  support  millions  of  

users  

Sync Gateway

Page 16: Couchbase Mobile on Android

Collaborate  using  Channels  

•  For  each  document,  you  specify  a  set  of  channels  it  belongs  to    ­  E.G  doc.channels  :  [  “food”,  “sports”  ]  

•  For  each  user  or  device,  you  control  which  channels  they  can  access  

•  Replicate  only  a  subset  of  documents  down  to  the  device  ­  User-­‐defined  filter  func;ons    ­  Simply  lets  you  know  whether  a  document  should  be  replicated  

•  And  you  can  authen9cate  users      

Page 17: Couchbase Mobile on Android

Sync Gateway!

Dat

a!D

ata!

Page 18: Couchbase Mobile on Android

Sync Gateway!

Dat

a!

Data!

Dat

a!

Page 19: Couchbase Mobile on Android

Sync Gateway!

Data!

Dat

a!D

ata!

Page 20: Couchbase Mobile on Android

Sync Gateway!

Dat

a!Da

ta!

Page 21: Couchbase Mobile on Android

Sync Gateway!

Data!

Data!

Dat

a!D

ata!

Page 22: Couchbase Mobile on Android

Sync Gateway!

Data!

Dat

a!D

ata!

Page 23: Couchbase Mobile on Android

What  you  get  

Easy  Sync  

•  Effortlessly  sync  local  data  with  a  database  in  the  cloud  for  updates,  replica;on  or  collabora;ve  sharing.  

•  In  addi;on,  scale  your  data  ;er  horizontally  and  reliably  as  your  data  and  sync  needs  grow.    

JSONJSONJSON

JSONJSON          JSON  Anywhere  

   •  Flexible  JSON  on  device  lets  you  work  with  local  data  &  modify  your  data  structure  on  the  fly  without  impac;ng  your  back-­‐end.  

•  Your  users  can  count  on  having  an  amazing  app  experience  with  a  fast  and  unbreakable  local  database      

Page 24: Couchbase Mobile on Android

Integra9ons  Couchbase  Lite  Plug-­‐in  for  PhoneGap  

•  Support  for  HTML5  developers  •  Develop  once  in  JavaScript  and  deploy  

on  iOS  &  Android  •  Available  on  GitHub,  Cordova  Plugin  

Registry,  and  coming  soon  to  build.phonegap.com  

 

h[ps://github.com/couchbaselabs/Couchbase-­‐Lite-­‐PhoneGap-­‐Plugin  

h[p://plugins.cordova.io/#/com.couchbase.lite.phonegap  

Page 25: Couchbase Mobile on Android

Integra9ons  Couchbase  Lite  for  Xamarin  

•  Partnered  to  support  C#  community  •  Available  on  Developer  Center  •  Includes  sample  applica;on  to  help  

developers  get  started  today  

h[p://components.xamarin.com/view/couchbase-­‐lite/  

Page 26: Couchbase Mobile on Android

Get  Started  

mobile.couchbase.com  

Sample  App  

hTps://github.com/couchbaselabs/TodoLite-­‐iOS  

Page 27: Couchbase Mobile on Android

Addi9onal  Resources  

Webinar  Series:  www.couchbase.com/webinars  ­  Developing  with  Couchbase  Lite  iOS  

­  Developing  with  Couchbase  Lite  Android  ­  Couchbase  Lite  Under  the  Hood  

 

Community  Forum  

hTps://groups.google.com/forum/#!forum/mobile-­‐couchbase  

Page 28: Couchbase Mobile on Android

Demo  

Page 29: Couchbase Mobile on Android

Demo  App  Overview  

•  Task  Model  ­  User  Id  ­  Crea;on  Time  ­  Title  

•  Task  Service  ­  Add  Task  ­  Get  Task  ­  Get  Tasklist  

•  Replica9on  ­  Bidirec;onal  Setup  ­  Con;nuous  ­  Change  Listener  for  

Monitoring    

•  Handle  Data  Changes  ­  Database  Change  Listener  ­  Valida;on  

•  Layout  ­  EditText  for  the  Task  Title  ­  GridView  for  the  Tasklist  ­  Bu[on  to  add  a  Task    

Page 30: Couchbase Mobile on Android

Demo  App  Overview  

•  Sync  Gateway  ­  Configura;on  ­  Guest  Access  ­  Sync  Func;on    

•  Couchbase  Server  ­  Bucket  Setup  ­  Stored  Sync  Info  

Page 31: Couchbase Mobile on Android

Further  Examples  

Page 32: Couchbase Mobile on Android

Couchbase  Lite  Views  

com.couchbase.lite.View  viewItemsByDate  =  database.getView(String.format("%s/%s",  designDocName,  byDateViewName));  

 

viewItemsByDate.setMap(new  Mapper()  {  

       @Override  

       public  void  map(Map<String,  Object>  document,  Emi[er  emi[er)  {  

               Object  createdAt  =  document.get("created_at");  

               if  (createdAt  !=  null)  {  

                       emi[er.emit(createdAt.toString(),  document);  

               }  

       }  

},  "1.0");  

com.couchbase.lite.View  view  =  database.getView("byDate");  

Query  query  =  view.createQuery();  

query.setLimit(10);  

query.setDescending(true);  

 

try  {  

       QueryEnumerator  rowEnum  =  query.run();  

       for  (Iterator<QueryRow>  it  =  rowEnum;  it.hasNext();)  {  

               QueryRow  row  =  it.next();  

               Log.d("Document  ID:",  row.getDocumentId());  

 

       }  

}  catch  (CouchbaseLiteExcep;on  e)  {  

       e.printStackTrace();  

}  

 

 

 

Crea9ng  Views   Querying  Views  

Page 33: Couchbase Mobile on Android

Filtered  Replica9ons  

database.setFilter("sharedItems",  new  Replica;onFilter()  {  

       @Override  

       public  boolean  filter(SavedRevision  revision,  Map<String,  Object>  params)  {  

               return  ((Boolean)  revision.getProper;es().get("shared")).booleanValue();  

       }  

});  

push.setFilter  =  "sharedItems";  

 

 

 

Crea9ng  a  Push  Filter  Func9on   Adding  the  Filter  by  Name  

Page 34: Couchbase Mobile on Android

Sync  Gateway  Authen9ca9on    

String  url  =  “h[ps://frank:[email protected]/database/”;  

private  void  addFacebookAuthoriza;on(Replica;on  replica;on)  {  

       //  start  Facebook  Login  

       Session.openAc;veSession(this,  true,  new  Session.StatusCallback()  {  

               //  callback  when  session  changes  state  

               @Override  

               public  void  call(Session  session,  SessionState  state,  Excep;on  excep;on)  {  

                       if  (excep;on  !=  null  ||  !session.isOpened())    {  

                               return;  

                       }  

                       //  make  request  to  the  Facebook  /me  API  

                       Request.executeMeRequestAsync(session,  new  Request.GraphUserCallback()  {  

 

                           

 

 

 

 

Basic   Facebook  

Page 35: Couchbase Mobile on Android

Sync  Gateway  Authen9ca9on    

 //  callback  azer  Facebook  Graph  API  response  with  user  object  

                               @Override  

                               public  void  onCompleted(GraphUser  user,  Response  response)  {  

                                       if  (user  !=  null)  {  

                                               String  email  =  (String)  user.getProperty("email");  

                                               FacebookAuthorizer  authorizer  =  new  FacebookAuthorizer(email);  

                                               authorizer.registerAccessToken(session.getAccessToken(),  email,  replica;on.getRemoteUrl());  

                                               replica;on.setAuthorizer(authorizer);  

                                       }  

                               }  

                       });  

               }  

       });  

}  

 

 

 

 

Facebook  (cont.)  

Page 36: Couchbase Mobile on Android

Sync  Func9on  Examples    

//Default  

func;on  (doc)  {  

   channel(doc.channels);  

}  

 

//Valida9on  

func;on  (doc)  {  

   throw  ({forbidden  :  "read  only!"})  

}  

 

 

 

//Channel  mapping  

func;on  (doc,  oldDoc)  {  

   if  (doc.published)  {  

       channel  ("public");  

   }    

}  

//Access  Control  

func;on  (doc,  oldDoc)  {  

   access  (doc.members,  doc.channel_name);  

   channel  (doc.channel_name)  

}  

 

Page 37: Couchbase Mobile on Android

Ques9ons?  

Page 38: Couchbase Mobile on Android

Thank  you!  

mobile.couchbase.com