Date post: | 01-Nov-2014 |
Category: |
Technology |
Upload: | kchodorow |
View: | 31 times |
Download: | 1 times |
Non-Relational Databases
San Francisco Perl MongersKristina ChodorowSeptember 3, 2009
Who am I?
• Software Engineer at 10gen• Perl, PHP, Java drivers
Scaling
Scaling
Technique #1: literally scale
Technique #1: literally scale
$3,200 $4,000,000x 1250 =
(Courtesy of Ask Bjorn Hansen)
Technique #2: shard
now-yesterday yesterday-the day before
the day before-the day before that
Technique #3: master-slave replication
R/W?W
W
W
R
R
Cassandra
• Designed by Facebook• Distributed - eventually
consistent• Column-oriented
Cassandra
• Writes ALWAYS work• Two types of reads: – high performance– guaranteed correct
• R/W atomic within column family
Cassandra
Row-oriented: joe, 34, karen, 25, bob, 50
Column-oriented:joe, karen, bob, 34, 25, 50
Project Voldemort
• Key/value store• Automatic replication/partitioning• 75 Github forks
“It is basically a just a big, distributed, persistent, fault-tolerant hash table.”
Tokyo Cabinet
• Another key/value store• Blazing fast… theoretically• Lua for server-side scripting
Tokyo Cabinet
• Key/value pairs• Array store• Index-able Hash
{ name : “Chris” age : 27, DOB : “January 1”}
CouchDB
• Master-Master Replication• Map/Reduce• REST API
CouchDB Futon
MongoDB
MongoDB
• Ease of use• Scalable• Dynamic queries - similar “feel” to
SQL• Speed of key/value stores (almost)• Power of RDBMSs (almost)
The Perl Driver
Available at Github: www.github.com/mongodb/mongo-perl-driver/
Install the Perl driver $ perl Makefile.PL && sudo make install
Available on CPAN (but old):http://search.cpan.org/~flora/MongoDB-0.01/
Downloading MongoDB
www.mongodb.org
Binaries available for Linux, Mac, Windows, Solaris
Start the DB Server
$ mkdir ~/db$$ tar zxvf mongodb-<OS>-1.0.0.tgz$ cd mongodb-<OS>-1.0.0$ bin/mongod --dbpath ~/db
Connecting to the Database
use MongoDB;
my $connection = MongoDB::Connection->new;
my $db = $connection->get_database(‘test’);
my $collection = $db->get_collection(‘foo’);
Inserting
my $id = $collection->insert( { title => ‘My first blog post’, author => ‘Joe’, content => ‘Hello, world!’
comments => [] });
MongoDB::OIDan autogenerated primary key
my $id = $collection->insert({whatever});
print Dumper($id);--------------------------------------------
$VAR1 = bless( { 'value' => '4a9700dba5f9107c5cbc9a9c' }, 'MongoDB::OID' )
Updating
$collection->update({_id => $id}, {‘$push’ => {comments => {
‘author’ => ‘Fred’,‘comment’ => ‘Dumb post.”
}}});
…which gives us:
print Dumper($collection->find_one());--------------------------------------------{
title : ‘My first blog post’, author : ‘Joe’, content : ‘Hello, world!’ comments : [{ ‘author’ : ‘Fred’, ‘comment’ : ‘Dumb post’ }]}
Magic (not the Perl kind)
$gt, $gte, $lt, $lte, $eq, $neq, $exists, $set, $mod, $where, $in, $nin, $inc$push, $pull, $pop, $pushAll, $popAll
$c->query({ x => {‘$gt’ => 4}})
Querying
my $commented_by_fred = $collection->query({ “comments.author” : “Fred” });
my $commented_by_fred = $collection->query({ “comments.author” : qr/fred/i });
$where
$collection->find_one({‘$where’ : ‘this.y == (this.x + this.z)’});
Will work:{x => 1, y => 4, z => 3}{x => “hi”, y => “hibye”, z => “bye”}
Won’t work:{x => 1, y => 1}
Optimizing $where
$collection->find_one({ ‘name’ => ‘Sally’, ‘age’ => {‘$gt’ => 18}, ‘$where’ => ‘Array.sort(this.interests)[2] == “volleyball”’});
Speaking of indexing…
$collection->ensure_index({“age” => “ascending”});
$collection->ensure_index({ “name” => “descending”, “ts” => “descending”, “comments.author” => “ascending”});
Cursors
my $cursor = $coll->query({“foo” => “bar”});
my $obj;while (my $obj = $cursor->next) { ...}
my @all = $cursor->all;
Paging
my $cursor = $coll->query-> sort({ts => -1})-> skip($page_num * $results_per_page)-> limit($results_per_page);
my @page = $cursor->all;
Logging
• insert/update/remove is fast• Capped collections• Schemaless• $inc for counts