san francisco java user group

38
Kristina Chodorow September 8 th , 2009 MongoDB

Upload: kchodorow

Post on 12-May-2015

3.405 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: San Francisco Java User Group

Kristina Chodorow

September 8th, 2009

MongoDB

Page 2: San Francisco Java User Group

Who am I?

• Software Engineer at 10gen

• Java, Perl, PHP, drivers

Page 3: San Francisco Java User Group
Page 4: San Francisco Java User Group

Technique #1:

literally scale

Page 5: San Francisco Java User Group

Technique #1:

literally scale

$3,200 $4,000,000x 1250 =

(Courtesy of Ask Bjorn Hansen)

Page 6: San Francisco Java User Group

Technique #2: shard

now-yesterday yesterday-

the day before

the day before-

the day before that

Page 7: San Francisco Java User Group

Technique #3: master-slave replication

R/W?W

W

W

R

R

Page 8: San Francisco Java User Group

Document-oriented DBs

• MongoDB

• CouchDBCassandra

Relational DBs

Fewer More

Key/Value Stores

• Project Voldemort

• Tokyo Cabinet

Graph DBs

Page 9: San Francisco Java User Group

MongoDB

Page 10: San Francisco Java User Group

MongoDB

• Ease of use

• Scalable

• Dynamic queries - similar “feel” to SQL

• Speed of key/value stores (almost)

• Power of RDBMSs (almost)

Page 11: San Francisco Java User Group
Page 12: San Francisco Java User Group

Downloading MongoDB

www.mongodb.org

Binaries available for Linux, Mac,

Windows, Solaris

Page 13: San Francisco Java User Group

The Venerable Java Driver

Available at Github: www.github.com/mongodb/mongo-java-driver/downloads

JavaScript: { x : y, z : w }

Java: BasicDBObjectBuilder.start().add("x", "y").add("z", "w").get()

Page 14: San Francisco Java User Group

Connecting to the Database

Mongo connection = new Mongo(“blog”);

DBCollection collection =

connection.getCollection(“posts”);

Page 15: San Francisco Java User Group

Inserting

collection.insert({

title : 'My first blog post',

author : 'Joe',

content : 'Hello, world!',

comments : []

});

Page 16: San Francisco Java User Group

Creating a User

DBObject post = BasicDBObjectBuilder.start()

.add("title", "My First Blog Post")

.add("username", "Joe")

.add("content", "Hello, world!")

.add("comments", new ArrayList())

.get();

collection.insert(post);

Page 17: San Francisco Java User Group

MongoDB::OIDan autogenerated primary key

collection.insert(post);

System.out.println(post.get("_id"));

--------------------------------------------

4a9700dba5f9107c5cbc9a9c

Page 18: San Francisco Java User Group

Updating

collection.update({_id : post._id},

{$push : {comments :

{

author => 'Fred',

comment => 'Dumb post.'

}

}});

{

title : 'My first blog post',

author : 'Joe',

content : 'Hello, world!',

comments :

[{author : 'Fred', comment : 'Dumb post.'}]

}

Page 19: San Francisco Java User Group

Updating

DBObject postId = BasicDBObjectBuilder.start()

.add("_id", post.get("_id")).get();

DBObject comment = BasicDBObjectBuilder.start()

.add("author", "Fred")

.add("comment", "Dumb post.").get();

DBObject addComment = new BasicDBObject(

"$push",

new BasicDBObject("comments", comment));

collection.update(joeId, addComment);

Page 20: San Francisco Java User Group

Magic $

$gt, $gte, $lt, $lte, $eq, $neq, $exists,

$set, $mod, $where, $in, $nin, $inc

$push, $pull, $pop, $pushAll, $popAll

collection.find({ x : {$gt : 4}})

Page 21: San Francisco Java User Group

Querying

commentsByFred = collection.find({

"comments.author" : "Fred"

});

commentsByFred = collection.find({

"comments.author" : /fred/i

});

Page 22: San Francisco Java User Group

Querying

Pattern fredPattern = Pattern

.compile("fred", CASE_INSENSITIVE);

DBObject fredQuery = BasicDBObject.start()

.add("comments.author", fredPattern)

.get();

Page 23: San Francisco Java User Group

$where

collection.find({$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}

Page 24: San Francisco Java User Group

Optimizing $where

collection.find({

name : Sally,

age : { $gt : 18},

$where : 'Array.sort(this.interests)[2]

== "volleyball"'})

Page 25: San Francisco Java User Group

Speaking of indexing…

collection.ensureIndex({age : 1});

collection.ensureIndex({

"name" : 1,

"ts" : -1,

"comments.author" : 1

});

Page 26: San Francisco Java User Group

Cursors

DBCursor cursor = collection.find(criteria);

while (cursor.hasNext()) {

DBObject obj = cursor.next();

...

}

Page 27: San Francisco Java User Group

Paging

DBObject sort = BasicDBObjectBuilder.start()

.add("ts", -1)

.get();

DBCursor cursor = coll.find()

.sort(sort)

.skip(pageNum * resultsPerPage)

.limit(resultsPerPage);

List<DBObject> page = cursor.toArray();

Page 28: San Francisco Java User Group

Twitter Schema

user = {

_id : ObjectId

username : String,

following : []

}

tweet = {

_id : ObjectId

userId : ObjectId,

msg : String,

ts: Date

}

Page 29: San Francisco Java User Group

Following

db.users.update({_id : joe._id}, {$push : {

following : fred._id

}});

{

_id : "a3845e28c36e22e578bb243f",

username : "joe",

following : ["2e893d7a458c271ba2e0349e"]

}

Page 30: San Francisco Java User Group

Finding Followers

db.users.find({$in : {following : joe._id}},

{username : 1});

Alice

Bob

Fred

...

Page 31: San Francisco Java User Group

Storing Files

4 megabytes

Page 32: San Francisco Java User Group

Storing Files

Page 33: San Francisco Java User Group

Storing Files

Page 34: San Francisco Java User Group

Storing Files

ObjectId fileId = new ObjectId();

fileObj = {

_id : fileId,

filename : "ggbridge.png",

user : "joe",

takenIn : "San Francisco"

}

chunkObj = {

fileId : fileId,

chunkNum : N

}

Page 35: San Francisco Java User Group

Storing Files

import com.mongodb.gridfs.*;

GridFS fs = new GridFS(connection);

GridFSFile file = fs.createFile(filename);

file.put("user", "Joe");

...

fs.save(file);

Page 36: San Francisco Java User Group

Retrieving Files

GridFSFile file = fs.findOne();

file.writeTo("myfile.png");

Page 37: San Francisco Java User Group

Logging

• insert/update/remove is fast

• Capped collections

• Upserts

• $inc for counts

Page 38: San Francisco Java User Group

Thank you!

[email protected]

@kchodorow

@mongodb

irc.freenode.net#mongodb

www.mongodb.org