Top Banner
Getting started with MongoDB and Scala Open Source Bridge June 27, 2012 Sean Sullivan
58

Getting started with MongoDB and Scala - Open Source Bridge 2012

Oct 18, 2014

Download

Technology

Getting started with MongoDB and Scala
Open Source Bridge
June 27 2012
Portland Oregon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Getting started with MongoDB and Scala - Open Source Bridge 2012

Getting started with MongoDB and Scala

Open Source BridgeJune 27, 2012Sean Sullivan

Page 2: Getting started with MongoDB and Scala - Open Source Bridge 2012
Page 3: Getting started with MongoDB and Scala - Open Source Bridge 2012

Gilt Groupe

Page 4: Getting started with MongoDB and Scala - Open Source Bridge 2012
Page 5: Getting started with MongoDB and Scala - Open Source Bridge 2012

Gilt Portland

Page 6: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 7: Getting started with MongoDB and Scala - Open Source Bridge 2012

Jobs @ Gilt Portland

• QA engineering

• Backend engineering

• Frontend engineering

#scala #java #html5 #javascript#ruby

Page 8: Getting started with MongoDB and Scala - Open Source Bridge 2012

open source @ Gilt

Page 9: Getting started with MongoDB and Scala - Open Source Bridge 2012
Page 10: Getting started with MongoDB and Scala - Open Source Bridge 2012
Page 11: Getting started with MongoDB and Scala - Open Source Bridge 2012

• MongoDB

• Using Mongo and Scala together

• MongoDB at Gilt

Page 12: Getting started with MongoDB and Scala - Open Source Bridge 2012

“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/

Page 13: Getting started with MongoDB and Scala - Open Source Bridge 2012

http://www.mongodb.org

Page 14: Getting started with MongoDB and Scala - Open Source Bridge 2012

https://github.com/mongodb/mongo

Page 15: Getting started with MongoDB and Scala - Open Source Bridge 2012

Licensing

Database: GNU AGPL v3.0

Drivers: Apache License 2.0

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

Page 16: Getting started with MongoDB and Scala - Open Source Bridge 2012

Getting started

Page 17: Getting started with MongoDB and Scala - Open Source Bridge 2012

http://www.mongodb.org/downloads

Page 18: Getting started with MongoDB and Scala - Open Source Bridge 2012

http://www.mongodb.org/downloads

Page 19: Getting started with MongoDB and Scala - Open Source Bridge 2012

MongoDB concepts

Page 20: Getting started with MongoDB and Scala - Open Source Bridge 2012

MySQL MongoDBdatabase database

table collectionindex indexrow BSON document

column BSON fieldjoin embedding and linking

primary key _id fieldgroup by aggregation

source: mongodb.org

Page 21: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 22: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 23: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 24: Getting started with MongoDB and Scala - Open Source Bridge 2012

Starting MongoDB

$ mongod

Page 25: Getting started with MongoDB and Scala - Open Source Bridge 2012

Mongo shell

$ mongo

Page 26: Getting started with MongoDB and Scala - Open Source Bridge 2012

Mongo shell demo

Page 27: Getting started with MongoDB and Scala - Open Source Bridge 2012

Mongo shell

> use mydb

> obama = { name: “Obama” };

> db.presidents.save(obama);

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

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

> exit

Page 28: Getting started with MongoDB and Scala - Open Source Bridge 2012

Client libraries for the JVM

Page 29: Getting started with MongoDB and Scala - Open Source Bridge 2012

• mongo-java-driver

• Morphia• Casbah

• Hammersmith

• Rogue

Page 30: Getting started with MongoDB and Scala - Open Source Bridge 2012

MongoDB Java driver

Page 31: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 32: Getting started with MongoDB and Scala - Open Source Bridge 2012

mongo-java-driver and Maven

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

Page 33: Getting started with MongoDB and Scala - Open Source Bridge 2012

mongo-java-driver example code

Page 34: Getting started with MongoDB and Scala - Open Source Bridge 2012

Morphia

Page 35: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 36: Getting started with MongoDB and Scala - Open Source Bridge 2012

Morphia example code

Page 37: Getting started with MongoDB and Scala - Open Source Bridge 2012

Casbah

Page 38: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 39: Getting started with MongoDB and Scala - Open Source Bridge 2012

Casbah

• Scala toolkit for MongoDB

• built on top of the mongo-java-driver

Page 40: Getting started with MongoDB and Scala - Open Source Bridge 2012

Casbah

• Scala idioms

• Scala collections

• fluid query syntax

Page 41: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 42: Getting started with MongoDB and Scala - Open Source Bridge 2012

Casbah and Maven

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

Page 43: Getting started with MongoDB and Scala - Open Source Bridge 2012

Casbah and SBT

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

Page 44: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 45: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 46: Getting started with MongoDB and Scala - Open Source Bridge 2012

Querying with Casbahval mongoColl = MongoConnection()("test_db")("users")val user1 = MongoDBObject("user" -> "barack", "email" -> "[email protected]")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" : "[email protected]"}, { "_id" : { "$oid" : "4c3e2bec521142c87dc10fbb"} , "user" : "someOtherUser"} ) */

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

Page 47: Getting started with MongoDB and Scala - Open Source Bridge 2012

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

Page 48: Getting started with MongoDB and Scala - Open Source Bridge 2012

Hammersmith

Page 49: Getting started with MongoDB and Scala - Open Source Bridge 2012

https://github.com/bwmcadams/hammersmith

Page 50: Getting started with MongoDB and Scala - Open Source Bridge 2012

Hammersmith

• new MongoDB driver for Scala

• pure Scala

• Asynchronous

• Not production ready at this time

Page 51: Getting started with MongoDB and Scala - Open Source Bridge 2012

MongoDB at Gilt

Page 52: Getting started with MongoDB and Scala - Open Source Bridge 2012

• feature configuration service

• global navigation service

• user service

Page 53: Getting started with MongoDB and Scala - Open Source Bridge 2012

Foursquare’s Fongo project

Page 54: Getting started with MongoDB and Scala - Open Source Bridge 2012

“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”

Page 56: Getting started with MongoDB and Scala - Open Source Bridge 2012

Additional resources

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

• http://www.mongodb.org

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

Page 57: Getting started with MongoDB and Scala - Open Source Bridge 2012

Questions?

Page 58: Getting started with MongoDB and Scala - Open Source Bridge 2012

THE END