getting started with mongodb and scala - open source bridge 2012

Post on 18-Oct-2014

7.075 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Getting started with MongoDB and Scala Open Source Bridge June 27 2012 Portland Oregon

TRANSCRIPT

Getting started with MongoDB and Scala

Open Source BridgeJune 27, 2012Sean Sullivan

Gilt Groupe

Gilt Portland

http://twitter.com/tinyrobots/status/217675630962163713

Jobs @ Gilt Portland

• QA engineering

• Backend engineering

• Frontend engineering

#scala #java #html5 #javascript#ruby

open source @ Gilt

• MongoDB

• Using Mongo and Scala together

• MongoDB at Gilt

“MongoDB is a document-oriented database management system designed for performance, horizontal scalability, high availability, and advanced queryability”

source: http://docs.mongodb.org/manual/about/

http://www.mongodb.org

https://github.com/mongodb/mongo

Licensing

Database: GNU AGPL v3.0

Drivers: Apache License 2.0

source: http://www.mongodb.org/display/DOCS/Licensing

Getting started

http://www.mongodb.org/downloads

http://www.mongodb.org/downloads

MongoDB concepts

MySQL MongoDBdatabase database

table collectionindex indexrow BSON document

column BSON fieldjoin embedding and linking

primary key _id fieldgroup by aggregation

source: mongodb.org

http://www.mongodb.org/display/DOCS/BSON

Embedding vs Linking

“Embedding is the nesting of objects and arrays inside a BSON document”

“Links are references between documents”

source: http://www.mongodb.org/display/DOCS/Schema+Design

MongoDB on MacOS X

$ wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.0.6.tgz

$ tar -zxvf mongodb-osx-x86_64-2.0.6.tgz

$ sudo mkdir -p /data/db

$ sudo chown `id -u` /data/db

Starting MongoDB

$ mongod

Mongo shell

$ mongo

Mongo shell demo

Mongo shell

> use mydb

> obama = { name: “Obama” };

> db.presidents.save(obama);

> db.presidents.ensureIndex({ name: 1 });

> db.presidents.find({name: “Obama”});

> exit

Client libraries for the JVM

• mongo-java-driver

• Morphia• Casbah

• Hammersmith

• Rogue

MongoDB Java driver

https://github.com/mongodb/mongo-java-driver/

mongo-java-driver and Maven

<dependency> <groupId>org.mongodb<groupId> <artifactId>mongo-java-driver<artifactId> <version>2.8.0<version> <dependency>

mongo-java-driver example code

Morphia

http://code.google.com/p/morphia/

Morphia example code

Casbah

https://github.com/mongodb/casbah/

Casbah

• Scala toolkit for MongoDB

• built on top of the mongo-java-driver

Casbah

• Scala idioms

• Scala collections

• fluid query syntax

https://twitter.com/mongodb/status/217291079920254976

Casbah and Maven

<dependency> <groupId>org.mongodb<groupId> <artifactId>casbah_2.9.2<artifactId> <version>2.3.0<version> <dependency>

Casbah and SBT

"org.mongodb" %% "casbah" % "2.3.0"

Casbah DBObjectimport org.mongodb.casbah.Imports._

val mongoConn = MongoConnection(hostname, port)val mongoDB = mongoConn(“test_db”)val collection = mongoDB(“test_data”)

val newObj = MongoDBObject(“a” -> “apple”, “b” -> “banana”)newObj += “c” -> “chocolate”

val a = newObject.getAs[String](“a”)// a is an Option[String]

http://api.mongodb.org/scala/casbah/current/tutorial.html

Casbah MongoDBList// MongoDBList is a Mongo-friendly Scala List

import org.mongodb.casbah.Imports._

val builder = MongoDBList.newBuilder builder += "foo" builder += "bar" builder += "x" builder += "y" val newLst = builder.result /* newLst: com.mongodb.BasicDBList = [ "foo" , "bar" , "x" , "y"] */

http://api.mongodb.org/scala/casbah/current/tutorial.html

Querying with Casbahval mongoColl = MongoConnection()("test_db")("users")val user1 = MongoDBObject("user" -> "barack", "email" -> "barack@whitehouse.gov")val user2 = MongoDBObject("user" -> "someOtherUser")mongoColl += user1mongoColl += user2mongoColl.find()// com.mongodb.casbah.MongoCursor =// MongoCursor{Iterator[DBObject] with 2 objects.}

for { x <- mongoColl} yield x/* Iterable[com.mongodb.DBObject] = List( { "_id" : { "$oid" : "4c3e2bec521142c87cc10faa"} , "user" : "obama" , "email" : "barack@whitehouse.gov"}, { "_id" : { "$oid" : "4c3e2bec521142c87dc10fbb"} , "user" : "someOtherUser"} ) */

http://api.mongodb.org/scala/casbah/current/tutorial.html

Fluid querying with Casbah DSL

val q = "email" $exists true// q: (String, com.mongodb.DBObject) =// (email,{ "$exists" : true})val users = for (x <- mongoColl.find(q)) yield xassert(users.size == 1)

http://api.mongodb.org/scala/casbah/current/tutorial.html

Hammersmith

https://github.com/bwmcadams/hammersmith

Hammersmith

• new MongoDB driver for Scala

• pure Scala

• Asynchronous

• Not production ready at this time

MongoDB at Gilt

• feature configuration service

• global navigation service

• user service

Foursquare’s Fongo project

“Fongo is an in-memory java implementation of mongo.”

“[...] primary use is for lightweight unit testing where you don't want to spin up a mongo process”

Additional resources

• http://www.10gen.com/presentations

• http://www.mongodb.org

• http://www.slideshare.net/sullis

Questions?

THE END

top related