4. NoSQL databases
NOSQL sometimes stands for Not Only SQL
NOSQL is mechanism for storage and retrieval
of data other than tabular data
Motivations are simplicity of design, horizontal
scaling, control over availability
5. Problems of RD solved by NoSQL
★ RD will not scale to your traffic at an
acceptable cost
★ NoSQL provides a tool to develop new
features easily.
★ NoSQL have local data transactions which
do not have to be very durable. e.g. “liking”
items on websites.
6. Avoid NoSQL
➢If application requires run-time flexibility.
➢If application requires ACID
➢if application requires complicated queries
➢if application requires query language
➢If consistency is mandatory and there will
be no drastic changes in data volume,
relational databases would be a better option
7.
8. What is MongoDB?
★ MongoDB is cross-platform
★ MongoDB is document-oriented database
★ MongoDB is a NoSQL database
★ MongoDB stores data in JSON-like
documents
9. MongoDB philosophy
Keep functionality when we can
Non-relational makes scaling horizontally
practical
Document models are good
Database technology should run anywhere
VMs, cloud, metal, etc
10. Use cases for MongoDB
Need of horizontal scaling:
storing in many regular servers
Iterative development:
regular changes of database’s structure
Document-oriented logic:
web page is important than data
12. Goal: create Web-application for e-commerce
Products: there is no specific products, different
type of products may be sold on webapp
Problem: design database schema
E-commerce: sample problem
13. Product_Book{
id, name, shipping_info,
price, description,
……….
author,
title,
publisher,
edition,
ISBN
}
Product_Media{
id, name, shipping_info,
price, description,
……….
artist,
title,
track_listing,
label,
format
}
Simple solution: each time create table for specific product type
Problem: very complex code, creating table, rewriting app takes time and
causes errors, items cannot be considered as one item
RELATIONALDATABASE
approach
14. Product{
id, name, shipping_info,
price, description,
field1_value,
field1_name,
field2_value,
field2_name,
}
Product{
id, name, shipping_info,
price, description,
type,
author,
artist,
track_listing,
ISBN,
}
Set of fields with value and name
Problems: what if there are many
fields, how to find all books
All types of attributes in one table
Problems: new items causes
changes in code and table
RELATIONALDATABASE
approach
17. {title: “Matrix”, price: 3500,
details:{actors:[‘Keanu Reaves’,’’K. Zeta Jones’]}}
{title: “Sherlock Holmes”, price: 2100,
details:{ISBN:33002A,author:”Conan Doyle”}}
★ MongoDB stores data in document form
○ Don’t need schema
○ Store in JSON form
○ Datas are edited in application
★ No JOINS
○ Data is loaded in LINEAR time
18. CRUD operations
Retrieving, creating, updating, deleting operations are done
on application side
There is no SQL queries
if software has many apps (on different platforms) it is BAD.
Because you have to write logic each time
if software has one app it is GOOD.
Because you don’t have to mess with SQL code
22. What is Lucene? (small explanation)
Lucene is information retrieval library, which
takes documents and makes them easily
searchable, through:
● indexing
● advanced analysis
● tokenization (indexing mice as mouse)
23. Lucene creates inverted
index, so that searching in
documents is performed in
linear time
Indexing
Searching terms in
documents without
indexing is
doc.size x no.documents
Don’t do this
24. Elasticsearch: use case
Elasticsearch is used as:
★ search engines
★ as a search mechanism for web-apps
among main database
○ e.g. E-commerce storing data in MongoDB, while
search data is stored in elasticsearch
★ as a document database
25. Elasticsearch: REST
Elasticsearch can be
accessed through
REST protocol
#Inserting data
PUT http://localhost:9200/movies/movie/1
{"title": "The Godfather","director":
"Francis Ford Coppola"}
#Getting data
GET http://localhost:9200/movies/movie/1
#Delete data
DELETE http://localhost:90/movies/movie/1
#Searching data
POST http://localhost:9200/_search
"query": {
"query_string": {
"query": "kill"
}
}
27. Couchbase history
Couchbase was created by combining two
NoSQL databases
Membase + CouchOne (principal players
behind CouchDB) = Couchbase
28. CouchBase
● Written in: Erlang & C
● Main point: Memcache compatible, but with
persistence and clustering
● Protocol: memcached + extensions
● Very fast (200k+/sec) access of data by key
● Provides memcached-style in-memory
caching buckets
29. Couchbase
Best used: Any application where low-latency
data access, high concurrency support and
high availability is a requirement.
For example: Low-latency use-cases like ad
targeting or highly-concurrent web apps like
online gaming (e.g. Zynga).
30. Couchbase
Couchbase store data in key-value or in document form
Couchbase is a key-value store: every Document has a
Key and a Value
Key can be a up to 250 characters
Keys are unique, within a bucket there can be only one key
Values can be JSON, string, numbers, binary blobs, special
positive number
31. Key-value NOSQL databases
Performance: high
Scalability: high
Flexibility: high
Complexity: none
Advantage:
High speed of response
Disadvantage:
All logic is located in app
32. Couchbase java example
CouchbaseClient c = new CouchbaseClient(baseURI, "default", "");
long userCounter = c.incr(“user_counter”,1,1);
c.set(“user:”+userCounter,json.toJson(user));
c.set("key", 0, "Hello World");
System.out.println(c.get("key"));