Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A NoSQL Alternative to Persisting Data in your Android Apps

209 views

Published on

This talk will examine NoSQL as an alternative to locally persisting data on the device. While NoSQL database technologies is gaining traction in large scale distributed applications, we will discuss it’s benefits as an embedded database on mobile platforms. The talk will go over the basics of NoSQL, comparing and contrasting the various styles. We will then introduce Couchbase Mobile and given a sneak peek at the new Couchbase Lite 2.0 architecture. We will see examples of CRUD and Query interface.

Published in: Technology
  • Search for tottoo removal? DON'T USE LASER! Discover a better, cheaper,painless way, 100% natural! click here... ♥♥♥ http://t.cn/A67tToQC
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

A NoSQL Alternative to Persisting Data in your Android Apps

  1. 1. A NoSQL Alternative to Persisting Data in your Android Apps Priya Rajagopal @rajagp
  2. 2. About Me 2 • Priya Rajagopal, Ann Arbor, Michigan • Mobile Developer Advocate, Couchbase • Organizer, Mobile Monday Ann Arbor • 18+ years of professional software development • Long Time iOS Developer *cough cough * • Over a decade in R&D • Twitter : @ rajagp
  3. 3. Modern Data Issues 3 1 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  4. 4. Security 4 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  5. 5. Platform Support 5 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  6. 6. Global Scale 6 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  7. 7. Peer-to-Peer 7 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  8. 8. Offline 8 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  9. 9. Data Synchronization 9 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 1 2 or
  10. 10. Data Conflicts 10 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Image Copyright:https://askfortheworld.files.wordpress.com/2013/02/conflict2.jpg
  11. 11. Data Persistence in Android 11 2 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  12. 12. Why should you care? 12 • Creating “offline first” apps • Airplane mode • Regions of poor or no network connectivity • Improve Perceived User Experience / Start up Time • Prebuilt bundled database • Caching of semi-static /static data • No backend to host data / Local only store • Privacy Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  13. 13. Options in Android 13 • SQLite • Embedded Public Domain SQL Database Engine • SQLiteOpenHelper class • Room • Shared Preferences • Key-Value Store for small datasets • Internal Storage • File System. Persistent or Cached. • External Storage • SD cards Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  14. 14. SQLite - Impedence Mismatch : Table -> Objects 14 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  15. 15. Handling Database Structural Changes 15 • Option 1: Delete the app from Device Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  16. 16. Handling Database Structural Changes 16 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. • Option 2: Silently drop the table public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + this.TABLE_NAME); onCreate(sqLiteDatabase); }
  17. 17. Data Migrations : Simple Case 17 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { if (oldVersion < 2) { String alter_table_add_website = "ALTER TABLE " + this.TABLE_UNIVERSITY + "ADD COLUMN " + "website" ; sqLiteDatabase.execSQL(alter_table_add_website); } if (oldVersion < 3) { String alter_table_rename = "ALTER TABLE " + this.TABLE_UNIVERSITY + "RENAME TO " + "University" ; sqLiteDatabase.execSQL(alter_table_rename); } // Migrate other versions …. }
  18. 18. Data Migrations : Complicated 18 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. • Create new Table with desired format • Transfer content over from old to new Table • Drop old Table • Rename new Table • Recreate indexes
  19. 19. Progressive Migrations 19 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Startup Time for Migrations AppV1 V1 V1 -> V2 mapping AppV2 V2 AppV3 V3 V2 -> V3 mapping AppV4 V4 AppV5 V5 V4 -> V5 mapping AppV6 V6 V5 -> V6 mapping
  20. 20. NoSQL 21 3 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  21. 21. What is NoSQL ? 22 • Non Relational • Unstructured/ Semi-structured • Scalable • SQL-type Queries Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  22. 22. Four popular NoSQL data models 23 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Key-value Email advocates@couchbase.co m Key Value Profile { "name": "A Person", "location": "Someplace" } Logo
  23. 23. Four popular NoSQL data models Author Column 1Row Columnar JK Rowling Column 2 Harry Potter and the Philosopher's Stone 1997 Title Year of release Harry Potter and the Chamber of Secrets 1998 Harry Potter and the NoSQL database 2017
  24. 24. Four popular NoSQL data models Graph This is Euler Priya Shirly Ann Jackson MITMassachusetts RPI Went to President of Went to Is in Lived in
  25. 25. Four popular NoSQL data models Document { "name": "A Person", "location": "Place", "team": "Team A", "interests": "music" } { "name": "A Person", "location": "Place", "team": "Team A", "interests": "music" }
  26. 26. JSON Data Modeling Normalized = Reference De-normalized = Embed
  27. 27. JSON -> Native Object Mapping
  28. 28. NoSQL: Example 1 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. US Umich.edu University Of Michigan US harvard.edu Harvard University -10516645301 { "type":"university", "name":"University of Michigan", "domain":"umich.edu", "country":"US” } { "type":"university", "name":"Harvard University”, "domain":"harvard.edu", "country":"US", "estDate":-10516645301 }
  29. 29. NoSQL : Example 2 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. US harvard.edu Harvard University { "type":"university", "name":"Harvard University", "domain":"harvard.edu”, "country":"US", "estDate":-10516645301 } -10516645301 (Double) US harvard.edu Harvard University “1636” (String) { "type":"university", "name":"Harvard University", "domain":"harvard.edu”, "country":"US", "estYear": “1636” } String estYear = doc.getString("estYear"); if (estYear != null) { // Handle case with estYear. } else { Date estDate = doc.getDate("estDate"); if (estDate != null) { // Handle case with estDate } }
  30. 30. NoSQL For Android : Couchbase Mobile 31 4 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  31. 31. Couchbase Lite 32 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Mobile App Couchbase Lite Native API Native Platform Bindings JSON Lite Core Engine Fleece SQLite Engine • Embedded NoSQL Database • Open Source • JSON Document Store • Android, iOS, .NET… • Community and Enterprise • Production Release 1.4 • Developer Preview 2.0
  32. 32. Couchbase Mobile Platform: Full Stack Database Platform 33 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Couchbase Lite Sync Gateway Couchbase Server EMBEDDED DATABASE SYNCHRONIZATION DATABASE SERVER SECURITY NoSQL Document Style
  33. 33. Getting Started 34 • https://developer.couchbase.com • Developer Builds Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  34. 34. Integrating with your Android Project 35 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  35. 35. Creating / Opening a Database 36 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. // Set Database configuration DatabaseConfiguration config = new DatabaseConfiguration(context); File dir = context.getDir("MyData",Context.MODE_PRIVATE); config.setDirectory(dir); config.setEncryptionKey( /*encryption key object*/ ); config.setConflictResolver(/*Set custom resolver*/ ); // Create / Open a database with specified name and configuration database = new Database(DATABASE_NAME, config);
  36. 36. Indexing 37 // Create value index of "type" property List<Expression> valueProp = new ArrayList<Expression>(); valueProp.add(Expression.property("type")); database.createIndex(valueProp); // Create full text search index List<Expression> ftsProp = new ArrayList<Expression>(); ftsProp.add(Expression.property("overview")); database.createIndex(ftsProp, IndexType.FullText, new IndexOptions("en",true)); Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  37. 37. Inserting Document 38 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. // Create a new document with specified ”props” map // If Id is not provided, system generates Id Document document = new Document(id, props); database.save(document); Supported Data Types : • List • Date • Map • Number • Null • String • Blob Typed Accessors
  38. 38. Deleting Document 39 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Document document = database.getDocument(id); if (document != null) { database.delete(document); }
  39. 39. Deleting Document 40 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Document document = database.getDocument(id); if (document != null) { database.delete(document); }
  40. 40. Queries 41 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. Query query = Query.select(SelectResult.all()) .from(DataSource.database(dbMgr.database)) .where((Expression.property("skills").in("music","movies")).and(Expression.property("type").equalTo("student" ))) .groupBy(Expression.property("grade")) .orderBy(Ordering.property("name").ascending()); try query.explain() try query.run()
  41. 41. Full Text Search 42 let query = Query.select(SelectResult.expression(Expression.meta().id))) .from(DataSource.database(database)) .where (Expression.property("overview").match("music”)) .orderBy(Ordering.property("name").ascending()); try query.explain() try query.run() Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  42. 42. Live Queries 43 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. // Create a liveQuery to fetch all documents from database query = Query. select(SelectResult.all()). from(DataSource.database(dbMgr.database)). toLive(); // Add a live query listener to continually monitor for changes query.addChangeListener(new LiveQueryChangeListener() { @Override public void changed(LiveQueryChange change) { ResultSet resultRows = change.getRows(); Result row; // Iterate over changed rows, corresponding documents and map to University POJO while ((row = resultRows.next()) != null) { // Handle changed row } } }) ; query.run();
  43. 43. Batch Requests 44 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. db.inBatch( new Runnable() { @Override public void run() { // Database Operations } } );
  44. 44. Conflict Resolution 45 Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. • Default Conflict Resolver Provided by System • Conflict Resolver Invoked on Document Save or during replication update // Set custom conflict resolver // In Database configuration config.setConflictResolver(/*Set object that implements the resolver method*/ ); @Override public ReadOnlyDocument resolve(Conflict conflict) { // Get revisions in conflict. Select a winner or merge changes to base ReadOnlyDocument mine = conflict.getMine(); ReadOnlyDocument theirs = conflict.getTheirs(); return mine; }
  45. 45. Threading 46 • Thread Safe API • Replicator updates on Background Thread • Database change notifications on main thread Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  46. 46. Replication / Sync 47 // Sync Gateway Endpoint private final static String SYNC_GATEWAY_URL = "blip://10.0.2.2:4984/mydatabase/"; // Set Replicator Configuration ReplicatorConfiguration config = new ReplicatorConfiguration(database, new URI(SYNC_GATEWAY_URL)); config.setContinuous(true); config.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL); config.setAuthenticator(new BasicAuthenticator("user","Password")); config.setChannels( Arrays.asList("userchannel")); // Start Replicator Configuration with remote Sync Gateway mPushPull = new Replicator(config); mPushPull.addChangeListener(this); mPushPull.start(); Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  47. 47. Resources 48 • Couchbase Developer Portal • https://developer.couchbase.com • Source Code • https://github.com/couchbaselabs • https://github.com/couchbase/ Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
  48. 48. Thank you @rajagp ©2017 Couchbase. All rights reserved. 49

×