talk about reactivemongo at msug may

21
Reactive Mongo Asynchronous driver for MongoDB

Upload: andrey-neverov

Post on 15-Jan-2015

272 views

Category:

Software


2 download

DESCRIPTION

My talk about ReactiveMongo driver at Moscow Scala user group May meetup

TRANSCRIPT

Page 1: Talk about ReactiveMongo at MSUG May

Reactive MongoAsynchronous driver for MongoDB

Page 2: Talk about ReactiveMongo at MSUG May

About me

• My name is Andrey Neverov • “General rhetoric” • scala since 2011 • now: scala, akka, spray, mongodb • contribute to reactive mongo

Page 3: Talk about ReactiveMongo at MSUG May

Plan

1. What is reactive mongo 2. Connecting 3. Queries 4. Working with data 5. Streaming 6. Reactive Mongo + Play

Page 4: Talk about ReactiveMongo at MSUG May

Reactive Mongo

• Asynchronous driver • Non-blocking I/O • Scalability • Streaming • GridFS

Page 5: Talk about ReactiveMongo at MSUG May

val driver = new MongoDriverval connection = driver. connection(List(“localhost"))val db = connection.db("db_name")val collection = db.collection(“coll_name")

Connecting

Page 6: Talk about ReactiveMongo at MSUG May

Queries

val query = BSONDocument( “age” -> BSONDocument( “$gt” -> 5 ))

Page 7: Talk about ReactiveMongo at MSUG May

Queries

val animalsOlderThanFive = collection. find(query). cursor[BSONDocument]. collect[List]()!

// Future[List[BSONDocument]]

Page 8: Talk about ReactiveMongo at MSUG May

Queries

animalsOlderThanFive.map { case animals: List[Animal] => …} recover { case t: Throwable => // fail!}

Page 9: Talk about ReactiveMongo at MSUG May

Readerscase class Animal(_id: BSONObjectID, age: Int)object Animal { implicit val reader = new BSONDocumentReader[Animal] { def read(doc: BSONDocument) = { Animal(…) }}}

Page 10: Talk about ReactiveMongo at MSUG May

Readers

object Animal { import reactivemongo.bson.Macros!

val handler = Macros.handler[Animal]}

Page 11: Talk about ReactiveMongo at MSUG May

Readers

val animalsFuture = collection. find(query). cursor[Animal]. collect[List]()!

Page 12: Talk about ReactiveMongo at MSUG May

Streaming

// Enumerator[BSONDocument]val enumeratorOfAnimals = collection. find(query). cursor[BSONDocument]. enumerate()

Page 13: Talk about ReactiveMongo at MSUG May

Streaming

// Iteratee[BSONDocument, Unit]val process: = Iteratee.foreach { a => val name = a[String]("name") val pretty = BSONDocument.pretty(a) println(s"got $name of: $pretty") }

Page 14: Talk about ReactiveMongo at MSUG May

Streaming

enumeratorOfAnimals.apply(process)!

or:!

enumeratorOfAnimals |>>> process

Page 15: Talk about ReactiveMongo at MSUG May

Play

• Play json <-> Reactive mongo bson

• Async actions

• Iteratees

• GridFS

Page 16: Talk about ReactiveMongo at MSUG May

Play

object Application extends Controller with MongoController {!

def collection: JSONCollection = db.collection(“animals")}

Page 17: Talk about ReactiveMongo at MSUG May

Playobject JsonFormats { import play.api.libs.json.Json import play.api.data._ import play.api.data.Forms._!

implicit val format = Json.format[Animal]}

Page 18: Talk about ReactiveMongo at MSUG May

Playdef findByName(name: String) = Action.async { collection. find(Json.obj("name" -> name)) cursor[JsObject]. collect[List]() map { case animals => Ok(Json.arr(animals)) }}

Page 19: Talk about ReactiveMongo at MSUG May

Play

def fromJson = Action.async(parse.json) { r => r.body.validate[Animal].map { a => collection.insert(a).map { … } }.getOrElse { Future(BadRequest(“bad json")) }}

Page 20: Talk about ReactiveMongo at MSUG May

Playdef upload =Action(gridFSBodyParser(gfs)) { r => // Future[ReadFile[BSONValue]] val futureFile = r.body.files.head.ref futureFile.map { file => // do something Ok }}

Page 21: Talk about ReactiveMongo at MSUG May

Thanks!

• http://www.mongodb.org/

• http://reactivemongo.org/

• https://github.com/ReactiveMongo/ReactiveMongo/

• https://github.com/neverov