5. NoSQL is...
Not Only
SQL or
SQL
I vote for this one
6. Origins of the Name
Carlo Strozzi ‐ NoSQL app
started in 1998. RDBMS sans SQL
applied to non‐relational DB’s around
2008‐ish
no:sql(east)
select fun, profit from real_world
where relational=false;
7. Origin of the Species
non‐relational databases pre‐date
relational ones
it’s sorta like AJAX
been doin’ it for awhile
gets a name
now it’s cool!!1!
8. Types of NoSQL DBs
graph (RDF/Semantic Web/triples)
key‐value (just what it says)
document (k/v + queriability)
object (big in the 1980s)
multivalue (old tech...like 1960s)
NewSQL?
23. (un)expected extras
at least in CouchDB
Built in Web Server
or App Server
Geospatial bounding box queries
n‐master replication
binary file storage
scales up and down
28. Ubiquitous/Distributed
Data
CouchDB
server <=> desktop <=> mobile
Riak Mobile?
go to Erlang Factory in June
29. FALE Scenarios
power outages
data loss
failed persistence, no persistence
network unavailability
special thanks to @coats who runs fale.ca for the FALE stamp
37. CouchDB has Super
Powers!
Schemaless
Document centric
Replication/Sync
Fail Fast Architecture
stateless API
append only file storage
38. Document centric
“natural” data model
store data like it exists
everywhere else: as a document
map/reduce vs. sql
sorting documents out of a drawer
vs. reassembling them from bits of
data
40. Fail Fast
append only database file
everything goes on the end of the
file
querys are cached there too
bounce back from errors rather than
spin wheels indefinitely
42. CouchDB Scaling
scales up and down
server <=> desktop <=> mobile
thanks to n‐master replication
BigCouch plugin for sharding
HTTP API can be load balanced
reverse proxies and caching
44. Time to Relax
(command line)
Apache CouchDB 1.0.2 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [<0.31.0>] Apache CouchDB has
started on http://127.0.0.1:5984/
49. HTTP Clients
curl
ugh...messy
pecl_http
lovely (next to curl), but takes
some install time, lacks examples
Zend_HTTP & PEAR HTTP_Request2
Most major frameworks have their own
50. Client Suggestions
HTTP
pick one that’s flexible (can
handle COPY)
CouchDB
I use Sag currently. Caching,
Cookie Auth, nice name.
PHP‐on‐Couch seems great, but watch
the license (GPL)
51. Today’s Stack
In PHP:
Sag ‐ saggingcouch.com/
For HTTP API Demoing/Testing:
Resty ‐ github.com/micha/resty
Poster for Firefox
code.google.com/p/poster‐
extension/
52. Other Handy HTTP
Clients
HTTPClient for Mac OS X
Charles Proxy ($$)
http‐twiddle for Emacs
Fiddler for Windows
Solex for Eclipse
58. I got tired of having to
pick -> or []
$j = new ArrayObject(
json_decod($json),
ArrayObject::ARRAY_AS_PROPS
);
print_r($j[‘with’][0]);
print_r($j‐>with[0]);
// “an” ‐‐ same result! no errors!
59. HTTP / REST
basics
GET read
PUT create or update
DELETE delete
POST bulk operation
66. PUT (failure)
on purpose...
PUT /pouch/tek ‘{“php”:”tek”}’
{"error":"conflict","reason":"Documen
t update conflict."}
# we need that _rev value now
68. PUT binary attachments
PUT /pouch/tek/photo?rev=2‐
fff750985c2c2602e859fe38cd1d347e
‐Q filename.png
‐H “Content‐Type: image/png”
{"ok":true,"id":"tek",
"rev":"3‐18d519e58b569e43a6fd5e87491f0c4c"}
GET /pouch/tek
{"_id":"tek",
"_rev":"3‐18d519e58b569e43a6fd5e87491f0c4c",
"_attachments":{"photo":{"content_type":"image/
png",
"revpos":1,"length":18,"stub":true}}}
69. A bit about
attachments
Each attachment to a doc has it’s own
URL:
/pouch/tek/photo
/pouch/tek/schedule.pdf
each attachments has it’s own
mimetype
attachments can be added/updated via
their own URLs or inline
70. DELETE (failure)
on purpose...again
DELETE /pouch/tek
{"error":"conflict",
"reason":"Document update conflict."}
# you always have to send a “rev”
when changing a doc in any way
73. That was just the very
basics
GET /_stats (server stats)
GET /_all_dbs (list all DB’s)
GET /_all_docs (list all docs)
GET /db/_changes (list recent
changes)
super powers!
74. Map/Reduce Queries
searching the file drawer
essentially stored queries
a.k.a. “Views”
written in JavaScript (or Python or
Ruby or Erlang or PHP?)
similar to array_map()/
array_reduce(), but scalable
no ad‐hoc queries
76. i want to cover
schema‐less JSON docs _security
Binary Attachments server side rendering
stuff
Map/Reduce
_show
Javascript
_list
several examples
URL Rewriting
replication
_rewrite
security
vhosts
validate_doc_updates
77. Pouch is...
a Database of
JSON docs of file meta‐data
with the file attached!
a filesystem importer
a Web‐based CouchApp for browsing
78. 0.1 Files into CouchDB
gather EXIF data
add attachment
PUT to CouchDB
let us know how it went
79. code preamble
#!/usr/bin/php
<?php
if ($argc < 2) die('I need a file name.'."n");
// CouchDB config
$user = 'admin';
$password = 'passwd';
// gather and clean EXIF data
$exif = exif_read_data($argv[1]);
unset($exif['MakerNote']);
unset($exif['ComponentsConfiguration']);
unset($exif['JPEGThumbnail']);
unset($exif['TIFFThumbnail']);
80. require_once dirname(realpath(__FILE__)).'/../libs/Sag/src/Sag.php';
$sag = new Sag('localhost', 5984);
$sag->setDatabase('pouch');
// PUT the binary attachment first
$sag->setAttachment('original', // the attachment name
file_get_contents($argv[1]), // the file
image_type_to_mime_type(exif_imagetype($argv[1])),
$id = md5_file($argv[1])); // the doc id
// then add the EXIF data
// GET the full doc as we need to add our info to what's there
// atomicity is at a document level in CouchDB
$doc = $sag->get($id)->body;
$doc->exif = $exif;
print_r($sag->put(md5_file($argv[1]), $doc));
121. CouchDB as REST API
server
Building another API for Pouch
In addition to (or instead of) the
standard CouchDB API
Putting that into CouchDB
_list ‐ index pages
_show ‐ single document page
_update ‐ document modification
_rewrite ‐ URL Rewriting