This document provides an overview of CouchDB, a NoSQL database that uses JSON documents with a flexible schema. It demonstrates CouchDB's features like replication, MapReduce, and filtering. The presentation then shows how to build a mobile running app called Couch25K that tracks locations using CouchDB and syncs data between phones and a server. Code examples are provided in Objective-C, Java, and JavaScript for creating databases, saving documents, querying, and syncing.
30. CouchDB Mobile
Your app CouchCocoa lib Ektorp lib Your app
TouchDB lib TouchDB lib
SQlite SQlite
31. CouchDB Mobile
“TouchDB is a lightweight CouchDB-
compatible database engine suitable
for embedding into mobile or desktop
apps. Think of it this way: If
CouchDB is MySQL, then TouchDB
is SQLite.” - Jens Alfke, Couchbase
Labs
35. Saving a Trackpoint
NSDictionary *trackpointProperties =
[NSDictionary dictionaryWithObjectsAndKeys: (...)
CouchDocument *trackpointDocument =
[database untitledDocument];
RESTOperation* op = [trackpointDocument
putProperties:trackpointProperties];
[op onCompletion: ^{
if (op.error)
NSLog(@"Couldn't save the new item");
}];
[op start];
36. Saving a Trackpoint
Ektorp: JPA for CouchDB
public class TrackPoint extends CouchDbDocument
Repository Support
public class TrackPointRepository extends
! CouchDbRepositorySupport<TrackPoint>
... so saving a TrackPoint is pretty easy
trackPointRepository.add(trackPoint);
37. Sync with the Server
NSURL *url = [NSURL
URLWithString:
@"http://peterfriese.iriscouch.com/couch25k"];
[self.database replicateWithURL:url exclusively: YES];
38. Sync with the Server
Pull from Server
ReplicationCommand commandPull = new
ReplicationCommand.Builder()
.source(COUCH25K_REMOTE_DB)
.target(COUCH25K_DB)
.continuous(true).build();
try {
couchDBInstance.replicate(commandPull);
} catch (Exception exception) {
Log.e(TAG, exception.getMessage(), exception);
}
39. Sync with the Server
Push to Server
ReplicationCommand commandPush = new
ReplicationCommand.Builder()
.source(COUCH25K_DB)
.target(COUCH25K_REMOTE_DB)
.continuous(true).build();
try {
couchDBInstance.replicate(commandPush);
} catch (Exception exception) {
Log.e(TAG, exception.getMessage(), exception);
}
42. Display List of Runs (Obj-C)
Map
[design defineViewNamed: @"runs" mapBlock: MAPBLOCK({
id run = [doc objectForKey:@"run"];
if (run) emit(run, nil);
})
Reduce
reduceBlock:REDUCEBLOCK({
return [NSNumber numberWithInt:values.count];
})
43. Display List of Runs (Java)
Map
new TDViewMapBlock() {
public void map(Map<String, Object> doc,
TDViewMapEmitBlock emitter) {
if (doc.containsKey("run")) {
emitter.emit(doc.get("run"), doc.get("_id"));
}
}
}
44. Display List of Runs (Java)
(Re-) reduce
new TDViewReduceBlock() {
public Object reduce(List<Object> keys, List<Object>
values, boolean rereduce) {
if (rereduce) {
int sum = 0;
for (Object object : values) {
sum += (Integer) object;
}
return sum;
}
return values.size();
}
};
45. Filtering
Peter Stefan
Filter by name
sync 2-way sync 2-way
Phone IrisCouch Phone 2
52. Query Trackpoints by Run
Map
new TDViewMapBlock() {
public void map(Map<String, Object> document,
TDViewMapEmitBlock emitter) {
if (document.containsKey("run")) {
document.get("run"), document.get("_id"));
}
}
53. Query Trackpoints by Run
Query
ViewQuery viewQuery = new
ViewQuery().designDocId("_design/TrackPoint")
.viewName("trackpoint_by_run")
.key(runId);
ViewResult result = db.queryView(viewQuery);
List<TrackPoint> trackPoints = new
ArrayList<TrackPoint>();
for (Row row : result.getRows()) {
TrackPoint trackPoint = get(row.getValue());
trackPoints.add(trackPoint);
}
return trackPoints;
57. Thanks!
Peter Friese
Principal Consultant
rt @peterfr
Zühlke Engineering GmbH @stef anreiche iese
Am Sandtorkai 66
20457 Hamburg Availa
ble fo
+49 151 108 604 72 discu r con
ssing sultin
all th g,
rt
Reiche Engineer mobil ings
Stefan
Software
e and
Senior H frost
nginee
rin g Gmb y beve
E
Zühlke torkai 66 rages
nd
Am Sa rg
Hambu
20457
6
961 43 3
+ 49 173
PS: we’re hiring...