NoSQL Concepts MongoDB Concepts MongoDB Demos Agenda.

31
WTT 2014 - WORKSHOP DE TENDÊNCIAS TECNOLÓGICAS 2014

Transcript of NoSQL Concepts MongoDB Concepts MongoDB Demos Agenda.

Page 1: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

WTT 2014 - WORKSHOP DE TENDÊNCIAS TECNOLÓGICAS 2014

Page 2: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

NoSQL ConceptsMongoDB ConceptsMongoDB Demos

Agenda

Page 3: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

“NoSQL” = “No SQL” = Not using traditional relational DBMS “No SQL” Don’t use SQL language

Alternative to traditional relational DBMS+ Flexible schema+ Quicker/cheaper to set up+ Massive scalability+ Relaxed consistency higher performance & availability

– No declarative query language more programming– Relaxed consistency fewer guarantees

NoSQL databases?

Page 4: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

NoSQL Systems

Map Reduce Framework Originally from Google, open source Hadoop

Key-Values-Stores Google BigTable, Amazon Dynamo, Cassandra, HBase, …

Document Stores• Data model: (key, document) pairs• Document: JSON, XML, other semistructured formats• CouchDB, MongoDB, SimpleDB, …

Graph Database Systems Neo4j, FlockDB, Pregel, …

Big-Table Implementation Teradata, Exadata, GreenPlum, MonetDB, …

Page 5: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

ACID versus BASE ACID Atomicity, Consistency, Isolation, Durability Traditional Databases

CAP Strong Consistency + High Availability + Partition-tolerance

The CAP-Theorem postulates that only two of the three different aspects of scaling out are can be achieved fully at the same time.

Many of the NOSQL BASE Basically Available, Soft-state, Eventually consistent

Page 6: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Quiz. NoSQL Applications ?

[ ] Web Log Analysis URL, timestamp, number of accesses

[ ] Social-network graphs user1, user2, Find friends of a friends

[ ] Wikipedia Pages Large collections, structured and unstructured data

[ ] Twitter messages unstructured data

[ ] Blog maintenance unstructured data

[ ] Account credits and debts

Page 7: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

MongoDB

MongoDB (from "humongous") is an open-source document database, and the leading NoSQL database. Written in C++, MongoDB features

• Document-Oriented Storage• Querying• Full Index Support• Replication & High Availability• Auto-Sharding• Map/Reduce• Geospatial support• Text Search

Page 8: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

DB-Engines Ranking

Page 9: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

SQL to MongoDB Mapping Chart

Page 10: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

JSON x SQL x BSONJSONJavaScript Object Notation. A human-readable, plain text format for expressing structured data with support in many programming languages.

BSONA serialization format used to store documents and make remote procedure calls in MongoDB. “BSON” is a portmanteau of the words “binary” and “JSON”.

SQL Schema Statements MongoDB Schema Statements

CREATE TABLE users ( Id INT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) )

db.users.insert( { user_id: "abc123", age: 55, status: "A" } )

Page 11: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

var p = {‘_id’: ‘3432’,‘author’: DBRef(‘User’, 2), ‘title’: ‘Introduction to MongoDB’,‘body’: ‘MongoDB is an open sources.. ‘,‘timestamp’: Date(’01-04-12’),‘tags’: [‘MongoDB’, ‘NoSQL’],‘comments’: [{‘author’: DBRef(‘User’, 4),

‘date’: Date(’02-04-12’),‘text’: ‘Did you see.. ‘,‘upvotes’: 7, … ]

}> db.posts.save(p);

JSON Document Model

Page 12: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Create Index on any field in the document// 1 means ascending, -1 means descending> db.posts.ensureIndex({‘author’: 1});

//Index Nested Documents> db.posts.ensureIndex(‘comments.author’: 1);

// Index on tags> db.posts.ensureIndex({‘tags’: 1});

// Geo-spatial Index> db.posts.ensureIndex({‘author.location’: ‘2d’});

Indexes

Page 13: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

// find posts which has ‘MongoDB’ tag.> db.posts.find({tags: ‘MongoDB’});

// find posts by author’s comments.> db.posts.find({‘comments.author’: DBRef(‘User’,2)}).count();

// find posts written after 31st March. > db.posts.find({‘timestamp’: {‘gte’: Date(’31-03-12’)}});

// find posts written by authors around [22, 42]> db.posts.find({‘author.location’: {‘near’:[22, 42]});

Queries?

$gt, $lt, $gte, $lte, $ne, $all, $in, $nin, count, limit, skip, group, etc…

Page 14: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

db.posts.update({_id: ‘3432’}, {‘title’: ‘Introduction to MongoDB (updated)’, ‘text’: ‘Updated text’, ${addToSet: {‘tags’: ‘webinar’}});

Updates? Atomic Operations

$set, $unset

MongoDB does not support TRANSACTIONS !

$push, $pull, $pop, $addToSet

$inc, $decr, many more…

Page 15: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Some Cool features, but not in this lab

• Geo-spatial Indexes for Geo-spatial queries.$near, $within_distance, Bound queries (circle, box)

• Map/ReduceGROUP BY in SQL, map/reduce in MongoDB.

• GridFSStores Large Binary Files.

Page 16: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: my bond girls database

Page 17: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: mongodb installhttp://www.mongodb.org/downloadshttp://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data

Page 18: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: mongodb as a Service https://mongolab.com/welcome/

Page 19: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: pymongo

Page 20: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: mongolab create a account

Page 21: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: after a Collection, create a document

Page 22: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: find( ) with Python

Page 23: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: find( ) with mongo shell

Page 24: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: load a .CSV collection with mongo shell

Page 25: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: results of load a .CSV collection

Page 26: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: find( ) a document with mongo shell

Page 27: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: find( ) a document with Python

Page 28: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Demo: find( ) select a field

Page 29: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

References The little Mongodb Book by Karl Seguinhttp://openmymind.net/mongodb.pdf

Mongodbhttp://docs.mongodb.org/manual/reference/sql-comparison/http://www.mongodb.org/downloadshttp://docs.mongodb.org/manual/installation/http://docs.mongodb.org/manual/tutorial/getting-started-with-the-mongo-shell/http://docs.mongodb.org/manual/https://www.mongodb.com/reference https://university.mongodb.com

Mongodb-as-a-Servicehttps://mongolab.com/welcome/https://www.mongohq.com/

Pymongo, mongodb drive for Pythonhttp://api.mongodb.org/python/current/installation.htmlhttp://api.mongodb.org/python/current/tutorial.html

Easy Install, to install pymongo for Windows https://pypi.python.org/pypi/setuptools

Page 30: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

Schema-lessWrites

db.createCollection(‘logs’, {capped: true, size: 1048576})

NO TRANSACTIONSData Processing ~ MapReduceGeospatial Full Text SearchNoSQL + tools and maturity

Conclusion: When to use MongoDB

Page 31: NoSQL Concepts  MongoDB Concepts  MongoDB Demos Agenda.

WTT 2014 - WORKSHOP DE TENDÊNCIAS TECNOLÓGICAS 2014Prof. Dr. Rogério de Oliveira [email protected]://meusite.mackenzie.br/rogerio