Top Banner
Scala with MongoDB Abdhesh Kumar Email: [email protected] Twitter:@abdheshkumar90
29

Scala with MongoDB

Sep 03, 2014

Download

Technology

abdheshkumar90

 
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: Scala with MongoDB

Scala with MongoDB

Abdhesh KumarEmail: [email protected]

Twitter:@abdheshkumar90

Page 2: Scala with MongoDB

1. What is MongoDB?

2. Why we use MongoDB?

3. What are the MongoDB Terminologies?

4. How to Install and Use MonogDB?

5. What are the Operators of MongoDB?

6. What is Casbah?

7. What is Salat?

Agenda

Page 3: Scala with MongoDB

What is MongoDB?

➢ MongoDB is a document-oriented database management system designed for performance, horizontal scalability, high availability,open source NoSQL database(Schemaless or Non-relational) ,And advanced queryability.

➢ MongoDB is a document-based database system, and as a result, all records, or data, in MongoDB are documents.

Page 4: Scala with MongoDB

Why we use MongoDB?Document Oriented➢ Documents (objects) map nicely to programming language data types.

➢ Dynamically-typed (schemaless) for easy schema evolution.➢ No joins and no transactions for high performance and easy scalability

High Performance

➢ No joins and no transactions makes reads and writes fast ➢ Indexes can include keys from embedded documents and arrays.

➢ Optional asynchronous writes

.

Page 5: Scala with MongoDB

High Availability➢Replicated servers with automatic master failover.

Easy Scalability➢ Automatic sharding distributes collection data across machines.➢Reads and writes are distributed over shards

Flexibility➢MongoDB stores data in JSON documents (which we serialize to BSON).

Page 6: Scala with MongoDB

Indexing➢ MongoDB supports generic secondary indexes, allowing a

variety of fast queries,and provides unique, compound, and geospatial indexing capabilities as well.

Fixed-size collections➢ Capped collections are fixed in size and are useful for certain

types of data, such as logs.➢ Real time aggregation➢ Rich query capabilities➢ Geospatial features➢ Flexible schema

Page 7: Scala with MongoDB

MongoDB Terminologies

Sql Terms/Concepts MongoDB Terms/Concepts

database database

table collection

index index

row Document or *BSON document

column Field or BSON field

join *Embedding and linking

Primary key _id

Group by aggregation

Page 8: Scala with MongoDB

MongoDB Terms/Concepts

Documents:➢ The concept of a document: an ordered set of keys with

associated values. ➢ documents are represented as objects:

{"greeting" : "Hello, world!"}

Collections:➢ A collection is a group of documents.

Page 9: Scala with MongoDB

Embedding:➢ “Embedding is the nesting of objects and arrays inside a

BSON document”

Linking:➢ “Links are references between documents”

Aggregation:➢ The MongoDB aggregation framework provides a means to

calculate aggregated values without having to use map-reduce.➢ If you’re familiar with SQL, the aggregation framework

provides similar functionality to GROUP BY and related SQL operators as well as simple forms of “self joins.” Additionally, the aggregation framework provides projection capabilities to reshape the returned data.

Page 10: Scala with MongoDB

What is JSON?➢ JSON (JavaScript Object Notation) is a lightweight data-

interchange format.➢ JSON is built on two structures:

1. A collection of name/value pairs. ex.object, record, struct, dictionary, hash table, keyed list, or associative array

2. An ordered list of values. ex. array, vector, list, or sequence

Page 11: Scala with MongoDB

{

"_id" : 1,

"name" : { "first" : "Rohan", "last" : "Singh" },

"contributes" : [ "TypeSafe", "Knoldus" ],

"awards" : [

{

"award" : "Play Framework Expert",

"year" : 2012,

"by" : "TypeSafe"

},

{ "award" : "Scala Expert",

"year" : 2013,

"by" : "Knoldus"

}

]

}

Page 12: Scala with MongoDB

What is BSON?

➢ BSON is a binary-encoded serialization of JSON-like document.

➢ BSON supports the embedding of documents and arrays within other documents and arrays.

➢ MongoDB uses BSON as the data storage and network transfer format for “documets”.

Page 13: Scala with MongoDB

Install MongoDB

$ 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 14: Scala with MongoDB

Starting MonDB and Mongo Shell

➢ $ ./bin/mongod

➢ $ mongo

MongoDB shell version: 2.4.1

connecting to: test

>

Page 15: Scala with MongoDB

MongoDB Operators

Query Selectors:➢ Comparison

$all, $gt, $gte, $in, $lt, $lte, $ne, $nin

➢ Logical

$and, $or, $nor, $not

➢ Element

$exists, $mod, $type

➢ JavaScript

$regix, $where

Page 16: Scala with MongoDB

➢ Array

$elemMatch,$size

Update➢ Fields

$inc, $set, $unset

➢ Array

$, $addToSet, $pop, $pullAll, $pull, $pushAll, $push

Projection$, $elemMatch, $slice

Page 17: Scala with MongoDB

What is the Casbah?

➢ Casbah is a Scala toolkit for MongoDB—We use the term “toolkit” rather than “driver”, as Casbah integrates a layer on top of the official mongo-java-driver for better integration with Scala.

Page 18: Scala with MongoDB

Installing & Setting up Casbah

*You should have MongoDB setup and running on your machine.

Add casbah dependencies to buid.sbt

resolvers += "Scala-tools" at "https://oss.sonatype.org/content/groups/scala-tools"

resolvers += "Sonatype Snapshot" at "https://oss.sonatype.org/content/repositories/releases"

libraryDependencies ++= Seq(

"org.mongodb" % "casbah_2.10" % "2.5.1"

)

Page 19: Scala with MongoDB

Use Casbah with Scala

➢ Import the Driverimport com.mongodb.casbah.Imports._

➢ Connecting to MongoDB Connect to default - localhost, 27017

val mongoConnection = MongoConnection()

connect to "mongodb01" host, default port

val mongoConnection = MongoConnection("mongodb01")

connect to "mongodb02" host, port 42017

val mongoConnection = MongoConnection("mongodb02", 42017)

Page 20: Scala with MongoDB

val mongoDB = mongoConnection("casbah_test")

Connect casbah_test database with test_data collecion by chaining

val mongoColl = mongoConnection("casbah_test")("test_data")

Working with Collections

Page 21: Scala with MongoDB

What is Salat?

➢ Salat is a bi directional Scala case class serialization library.➢ Salat provides fast, reliable bi directional serialization between

Scala case classes and MongoDB's DBObject format.➢ case classes can be used directly for storing document by

simply declaring a DAO and with the help of some annotations

Page 22: Scala with MongoDB

Salat DAOSalatDAOmakes it simple to start working with your case class objects.

Use it as is or as the basis for your own DAO implementation.

By extending SalatDAO, you can do the following out of box:➢ insert and get back an Option with the id

➢ findOne and get back an Option

➢ typed to your case class

➢ find and get back a Mongo cursor typed to your class

➢ iterate, limit, skip and sort

➢ update with a query and a case class

➢ save and remove case classes

➢ Projections

➢ built in support for child collections

Page 23: Scala with MongoDB

What Scala types can Salat handle?

➢ case classes

➢ embedded case classes

➢ embedded case classes typed to a trait or abstract superclass annotated with @Salat

➢ Scala enums

➢ Options

➢ Collections

Collections➢ Maps are represented as DBObject ; all other collections turn into DBList .

Page 24: Scala with MongoDB

Salat collection support

Salat 0.0.8-SNAPSHOT and above support the following mutable and

immutable collections:

➢ Map

➢ Lists and linked lists

➢ Seqs and indexed seqs

➢ Set

➢ Buffer

➢ Vector

Page 25: Scala with MongoDB

Salat Unsupported types

Salat can't support any of these types right now:

➢ Nested inner classes

➢ A class typed at the top-level to a trait or an abstract superclass

Salat can't support these types because the mongo-java-driver doesn't support

Them:

➢ Any type of Map whose key is not a String

➢ any type of map whose key is a String containing . or $

Page 26: Scala with MongoDB

Salat Supports Annotations

Salat offers the following annotations to customize serialization behavior:

➢ @Salat to support polymorphic instances of a trait or abstract superclass

➢ @Key to change the name of a field

➢ @Persist to serialize a value outside the case class constructor

➢ @Ignore to ignore a field in the case class constructor

➢ @EnumAs to customize the behavior of a particular enum

Page 27: Scala with MongoDB

Salat Grater

Grater Salat as the notion of Grater that is responsible to the de/serialization

of a case class.

This through two simple functions, let have a Grater for the type T:

➢ asDBObject(t:T) : returns a MongoDB representation of t

➢ asObject(dbo:DBObject)[T] : returns a T instance based on the provided

Mo ngoDB content

Page 28: Scala with MongoDB

Installing & Setting up Salat

Add salat dependencies to buid.sbt

resolvers += "Novus Release Repository" at "http://repo.novus.com/releases/"

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

libraryDependencies ++= Seq(

"com.novus" %% "salat" % "1.9.2-SNAPSHOT"

)

Page 29: Scala with MongoDB

References

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