SlideShare a Scribd company logo
A NoSQL Alternative
to Persisting Data in
your Android Apps
Priya	Rajagopal
@rajagp
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
Modern Data
Issues
3
1
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Security
4
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Platform Support
5
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Global Scale
6
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Peer-to-Peer
7
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Offline
8
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Data Synchronization
9
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.	
1
2
or
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
Data Persistence
in Android
11
2
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
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.
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.
SQLite - Impedence Mismatch : Table -> Objects
14
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
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.
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);
}
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	….
}
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
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
A NoSQL Alternative to Persisting Data in your Android Apps
NoSQL
21
3
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
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.
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.c
om
Key Value
Profile {
"name": "A Person",
"location": "Someplace"
}
Logo
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
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
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"
}
JSON Data Modeling
Normalized	=	Reference De-normalized	=	Embed
JSON -> Native Object Mapping
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
}
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
}
}
NoSQL For
Android :
Couchbase Mobile
31
4
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
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
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
Getting Started
34
• https://developer.couchbase.com
• Developer Builds
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
Integrating with your Android Project
35
Confidential	and	Proprietary.	Do	not	distribute	without	Couchbase	consent.	©	Couchbase	2017.	All	rights	reserved.
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);
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.
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
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);
}
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);
}
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()
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.
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();
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
}
}
);
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;
}
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.
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.
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.
Thank	you
@rajagp
©2017	Couchbase.	All	rights	reserved. 49

More Related Content

PPTX
Intro to Neo4j with Ruby
Max De Marzi
 
PPTX
Introducing Neo4j graph database
Amirhossein Saberi
 
PPTX
Windy City DB - Recommendation Engine with Neo4j
Max De Marzi
 
PDF
No sql data-storage for-your-ios-apps-using-couchbase-mobile
Priya Rajagopal
 
PDF
Graph databases and the #panamapapers
darthvader42
 
PPTX
A Real-World Implementation of Linked Data
Dimitri van Hees
 
PDF
Polyglot Persistence with MongoDB and Neo4j
Corie Pollock
 
PPTX
Graph of Thrones - Neo4j + Game of Thrones
Jhonathan de Souza Soares
 
Intro to Neo4j with Ruby
Max De Marzi
 
Introducing Neo4j graph database
Amirhossein Saberi
 
Windy City DB - Recommendation Engine with Neo4j
Max De Marzi
 
No sql data-storage for-your-ios-apps-using-couchbase-mobile
Priya Rajagopal
 
Graph databases and the #panamapapers
darthvader42
 
A Real-World Implementation of Linked Data
Dimitri van Hees
 
Polyglot Persistence with MongoDB and Neo4j
Corie Pollock
 
Graph of Thrones - Neo4j + Game of Thrones
Jhonathan de Souza Soares
 

Similar to A NoSQL Alternative to Persisting Data in your Android Apps (20)

PPTX
Bloor Research & DataStax: How graph databases solve previously unsolvable bu...
DataStax
 
PDF
Introducción a Neo4j
Neo4j
 
PPT
Neo4J : Introduction to Graph Database
Mindfire Solutions
 
PDF
Intro to Neo4j and Graph Databases
Neo4j
 
PDF
Intro to Neo4j Webinar
Neo4j
 
PPTX
Graph Databases
thai
 
PDF
DrupalCamp NJ 2014 Solr and Schema.org
scorlosquet
 
PDF
Contextual Computing: Laying a Global Data Foundation
Richard Wallis
 
PDF
Schema.org where did that come from?
Richard Wallis
 
PDF
The Future of Search and SEO in Drupal
scorlosquet
 
PDF
Applying large scale text analytics with graph databases
Data Ninja API
 
PDF
Schema.org: Where did that come from!
Richard Wallis
 
PPTX
GraphDB
Ömer Taşkın
 
PDF
Sparkler at spark summit east 2017
Thamme Gowda
 
PDF
Sparkler Presentation for Spark Summit East 2017
Karanjeet Singh
 
PDF
Sparkler—Crawler on Apache Spark: Spark Summit East talk by Karanjeet Singh a...
Spark Summit
 
PDF
Data Science Popup Austin: Back to The Future for Data and Analytics
Domino Data Lab
 
PDF
From Big Data to Fast Data
Sina Sheikholeslami
 
PPTX
Neo4j Training Introduction
Max De Marzi
 
PDF
The Connected Data Imperative: The Shifting Enterprise Data Story
Neo4j
 
Bloor Research & DataStax: How graph databases solve previously unsolvable bu...
DataStax
 
Introducción a Neo4j
Neo4j
 
Neo4J : Introduction to Graph Database
Mindfire Solutions
 
Intro to Neo4j and Graph Databases
Neo4j
 
Intro to Neo4j Webinar
Neo4j
 
Graph Databases
thai
 
DrupalCamp NJ 2014 Solr and Schema.org
scorlosquet
 
Contextual Computing: Laying a Global Data Foundation
Richard Wallis
 
Schema.org where did that come from?
Richard Wallis
 
The Future of Search and SEO in Drupal
scorlosquet
 
Applying large scale text analytics with graph databases
Data Ninja API
 
Schema.org: Where did that come from!
Richard Wallis
 
Sparkler at spark summit east 2017
Thamme Gowda
 
Sparkler Presentation for Spark Summit East 2017
Karanjeet Singh
 
Sparkler—Crawler on Apache Spark: Spark Summit East talk by Karanjeet Singh a...
Spark Summit
 
Data Science Popup Austin: Back to The Future for Data and Analytics
Domino Data Lab
 
From Big Data to Fast Data
Sina Sheikholeslami
 
Neo4j Training Introduction
Max De Marzi
 
The Connected Data Imperative: The Shifting Enterprise Data Story
Neo4j
 
Ad

Recently uploaded (20)

PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
The Future of Artificial Intelligence (AI)
Mukul
 
Ad

A NoSQL Alternative to Persisting Data in your Android Apps

Editor's Notes

  • #11: Concurrent Updates to Data Conflict Handling Automatic On Device In the Cloud By an External System By a Human
  • #14: Room : remove biolerplate code, simpler migrations (still same issues), compile time checked queries, no db ops on main thread Entities, DaO
  • #17: In fact AFAIK, Room If u don’t provide migration, it will just delete all the data in incompatible store