Your SlideShare is downloading. ×
0
Scala with MongoDBAbdhesh KumarEmail: abdhesh@knoldus.comTwitter:@abdheshkumar90
1. What is MongoDB?2. Why we use MongoDB?3. What are the MongoDB Terminologies?4. How to Install and Use MonogDB?5. What a...
What is MongoDB?➢ MongoDB is a document-oriented database managementsystem designed for performance, horizontal scalabilit...
Why we use MongoDB?Document Oriented➢ Documents (objects) map nicely to programming language datatypes.➢ Dynamically-typed...
High Availability➢Replicated servers with automatic master failover.Easy Scalability➢ Automatic sharding distributes colle...
Indexing➢ MongoDB supports generic secondary indexes, allowing avariety of fast queries,and provides unique, compound, and...
MongoDB TerminologiesSql Terms/Concepts MongoDB Terms/Conceptsdatabase databasetable collectionindex indexrow Document or ...
MongoDB Terms/ConceptsDocuments:➢ The concept of a document: an ordered set of keys withassociated values.➢ documents are ...
Embedding:➢ “Embedding is the nesting of objects and arrays inside aBSON document”Linking:➢ “Links are references between ...
What is JSON?➢ JSON (JavaScript Object Notation) is a lightweight data-interchange format.➢ JSON is built on two structure...
{"_id" : 1,"name" : { "first" : "Rohan", "last" : "Singh" },"contributes" : [ "TypeSafe", "Knoldus" ],"awards" : [{"award"...
What is BSON?➢ BSON is a binary-encoded serialization of JSON-likedocument.➢ BSON supports the embedding of documents and ...
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.tg...
Starting MonDB and Mongo Shell➢ $ ./bin/mongod➢ $ mongoMongoDB shell version: 2.4.1connecting to: test>
MongoDB OperatorsQuery Selectors:➢ Comparison$all, $gt, $gte, $in, $lt, $lte, $ne, $nin➢ Logical$and, $or, $nor, $not➢ Ele...
➢ Array$elemMatch,$sizeUpdate➢ Fields$inc, $set, $unset➢ Array$, $addToSet, $pop, $pullAll, $pull, $pushAll, $pushProjecti...
What is the Casbah?➢ Casbah is a Scala toolkit for MongoDB—We use the term “toolkit” ratherthan “driver”, as Casbah integr...
Installing & Setting up Casbah*You should have MongoDB setup and running on your machine.Add casbah dependencies to buid.s...
Use Casbah with Scala➢ Import the Driverimport com.mongodb.casbah.Imports._➢ Connecting to MongoDBConnect to default - loc...
val mongoDB = mongoConnection("casbah_test")Connect casbah_test database with test_data collecion by chainingval mongoColl...
What is Salat?➢ Salat is a bi-directional Scala case class serialization library.➢ Salat provides fast, reliable bi-direct...
Salat DAOSalatDAOmakes it simple to start working with your case class objects.Use it as is or as the basis for your own D...
What Scala types can Salat handle?➢ case classes➢ embedded case classes➢ embedded case classes typed to a trait or abstrac...
Salat collection supportSalat 0.0.8-SNAPSHOT and above support the following mutable andimmutable collections:➢ Map➢ Lists...
Salat Unsupported typesSalat cant support any of these types right now:➢ Nested inner classes➢ A class typed at the top-le...
Salat Supports AnnotationsSalat offers the following annotations to customize serializationbehavior:➢ @Salat to support po...
Salat GraterGraterSalat as the notion of Grater that is responsible to the de/serializationof a case class.This through tw...
Installing & Setting up SalatAdd salat dependencies to buid.sbtresolvers += "Novus Release Repository" at "http://repo.nov...
Referenceshttp://docs.mongodb.org/manual/
Upcoming SlideShare
Loading in...5
×

Scala with MongoDB

2,225

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,225
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Scala with MongoDB"

  1. 1. Scala with MongoDBAbdhesh KumarEmail: abdhesh@knoldus.comTwitter:@abdheshkumar90
  2. 2. 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
  3. 3. What is MongoDB?➢ MongoDB is a document-oriented database managementsystem designed for performance, horizontal scalability, highavailability,open source NoSQL database(Schemaless or Non-relational) ,And advanced queryability.➢ MongoDB is a document-based database system, and as aresult, all records, or data, in MongoDB are documents.
  4. 4. Why we use MongoDB?Document Oriented➢ Documents (objects) map nicely to programming language datatypes.➢ Dynamically-typed (schemaless) for easy schema evolution.➢ No joins and no transactions for high performance and easyscalabilityHigh Performance➢ No joins and no transactions makes reads and writes fast➢ Indexes can include keys from embedded documents andarrays.➢ Optional asynchronous writes.
  5. 5. High Availability➢Replicated servers with automatic master failover.Easy Scalability➢ Automatic sharding distributes collection data across machines.➢Reads and writes are distributed over shardsFlexibility➢MongoDB stores data in JSON documents (which we serializeto BSON).
  6. 6. Indexing➢ MongoDB supports generic secondary indexes, allowing avariety of fast queries,and provides unique, compound, andgeospatial indexing capabilities as well.Fixed-size collections➢ Capped collections are fixed in size and are useful for certaintypes of data, such as logs.➢ Real time aggregation➢ Rich query capabilities➢ Geospatial features➢ Flexible schema
  7. 7. MongoDB TerminologiesSql Terms/Concepts MongoDB Terms/Conceptsdatabase databasetable collectionindex indexrow Document or *BSON documentcolumn Field or BSON fieldjoin *Embedding and linkingPrimary key _idGroup by aggregation
  8. 8. MongoDB Terms/ConceptsDocuments:➢ The concept of a document: an ordered set of keys withassociated values.➢ documents are represented as objects:{"greeting" : "Hello, world!"}Collections:➢ A collection is a group of documents.
  9. 9. Embedding:➢ “Embedding is the nesting of objects and arrays inside aBSON document”Linking:➢ “Links are references between documents”Aggregation:➢ The MongoDB aggregation framework provides a means tocalculate aggregated values without having to use map-reduce.➢ If you’re familiar with SQL, the aggregation frameworkprovides similar functionality to GROUP BY and related SQLoperators as well as simple forms of “self joins.” Additionally,the aggregation framework provides projection capabilities toreshape the returned data.
  10. 10. 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 array2. An ordered list of values. ex. array, vector, list, or sequence
  11. 11. {"_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"}]}
  12. 12. What is BSON?➢ BSON is a binary-encoded serialization of JSON-likedocument.➢ BSON supports the embedding of documents and arrayswithin other documents and arrays.➢ MongoDB uses BSON as the data storage and networktransfer format for “documets”.
  13. 13. 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
  14. 14. Starting MonDB and Mongo Shell➢ $ ./bin/mongod➢ $ mongoMongoDB shell version: 2.4.1connecting to: test>
  15. 15. MongoDB OperatorsQuery Selectors:➢ Comparison$all, $gt, $gte, $in, $lt, $lte, $ne, $nin➢ Logical$and, $or, $nor, $not➢ Element$exists, $mod, $type➢ JavaScript$regix, $where
  16. 16. ➢ Array$elemMatch,$sizeUpdate➢ Fields$inc, $set, $unset➢ Array$, $addToSet, $pop, $pullAll, $pull, $pushAll, $pushProjection$, $elemMatch, $slice
  17. 17. What is the Casbah?➢ Casbah is a Scala toolkit for MongoDB—We use the term “toolkit” ratherthan “driver”, as Casbah integrates a layer on top of the official mongo-java-driver for better integration with Scala.
  18. 18. Installing & Setting up Casbah*You should have MongoDB setup and running on your machine.Add casbah dependencies to buid.sbtresolvers += "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")
  19. 19. Use Casbah with Scala➢ Import the Driverimport com.mongodb.casbah.Imports._➢ Connecting to MongoDBConnect to default - localhost, 27017val mongoConnection = MongoConnection()connect to "mongodb01" host, default portval mongoConnection = MongoConnection("mongodb01")connect to "mongodb02" host, port 42017val mongoConnection = MongoConnection("mongodb02", 42017)
  20. 20. val mongoDB = mongoConnection("casbah_test")Connect casbah_test database with test_data collecion by chainingval mongoColl = mongoConnection("casbah_test")("test_data")Working with Collections
  21. 21. What is Salat?➢ Salat is a bi-directional Scala case class serialization library.➢ Salat provides fast, reliable bi-directional serialization betweenScala case classes and MongoDBs DBObject format.➢ case classes can be used directly for storing document bysimply declaring a DAO and with the help of some annotations
  22. 22. 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
  23. 23. What Scala types can Salat handle?➢ case classes➢ embedded case classes➢ embedded case classes typed to a trait or abstract superclass annotatedwith @Salat➢ Scala enums➢ Options➢ CollectionsCollections➢ Maps are represented as DBObject ; all other collections turn into DBList .
  24. 24. Salat collection supportSalat 0.0.8-SNAPSHOT and above support the following mutable andimmutable collections:➢ Map➢ Lists and linked lists➢ Seqs and indexed seqs➢ Set➢ Buffer➢ Vector
  25. 25. Salat Unsupported typesSalat cant support any of these types right now:➢ Nested inner classes➢ A class typed at the top-level to a trait or an abstract superclassSalat cant support these types because the mongo-java-driver doesnt supportThem:➢ Any type of Map whose key is not a String➢ any type of map whose key is a String containing . or $
  26. 26. Salat Supports AnnotationsSalat offers the following annotations to customize serializationbehavior:➢ @Salat to support polymorphic instances of a trait or abstractsuperclass➢ @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
  27. 27. Salat GraterGraterSalat as the notion of Grater that is responsible to the de/serializationof 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 theprovidedMo ngoDB content
  28. 28. Installing & Setting up SalatAdd salat dependencies to buid.sbtresolvers += "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")
  29. 29. Referenceshttp://docs.mongodb.org/manual/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×