NOSQL
NOSQL
Basically Available
• The system guarantees availability.
• It will always respond to a request (success or failure).
• Even if some nodes fail, the system still works.
Soft State
• The system state may change over time, even without new input.
• Because of data replication, different nodes may temporarily have different values.
• The system does not require immediate consistency.
Eventual Consistency
• If no new updates are made, the system will eventually become consistent.
• After some time, all replicas will contain the same data.
• Unlike RDBMSs that focus on consistency, BASE systems focus on
availability.
• They relax the rules and allow reports to run even if not all portions of
the database are synchronized.
• BASE systems are considered optimistic in that they assume that
eventually all systems will catch up and become consistent.
• BASE systems tend to be simpler and faster because they don’t have
to write code that deals with locking and unlocking resources. Their
mission is to keep the process moving and deal with broken parts at a
later time.
• BASE systems are ideal for web storefronts, where filling a shopping
cart and placing an order is the main priority.
ACID Vs BASE
•Additional operations:
– variations on the above, e.g., reverse lookup
(REVERSE INDEX)
– iterators
When to use a Key_Value
Database
► When your application needs to handle lots of small continuous reads and
writes, that may be volatile. Key-value databases offer fast in-memory access.
► When storing basic information, such as customer details; storing webpages
with the URL as the key and the webpage as the value; storing shopping-cart
contents, product categories, e-commerce product details
► For applications that don’t require frequent updates or need to support
complex queries.
Use cases for Key_Value
Databases
► Session management on a large scale.
► Using cache to accelerate application responses.
► Storing personal data on specific users.
► Product recommendations, storing personalized lists of items for
individual customers.
► Managing each player’s session in massive multiplayer online games.
► Redis, Dynamo, Riak are some NoSQL examples of key-value store
DataBases.
REmote DIctionary Server-Redis
• Their simplicity and generality save you time and money by mov ing
your focus from architectural design to reducing your data services
costs through
• Precision service levels
• Precision service monitoring and notification
• Scalability and reliability
• Portability and lower operational costs
Key Valu Stores
Instead of using a query language, application developers access and
manipulate a key-value store with the put, get, and delete functions,
• put($key as xs:string, $value as item()) adds a new key-value pair to
the table and will update a value if this key is already present.
• get($key as xs:string) as item() returns the value for any given key, or
it may return an error message if there’s no key in the key-value store.
• delete($key as xs:string) removes a key and its value from the table,
or it many return an error message if there’s no key in the key-value
store.
Simple Application in Redis
• CREATE
HSET student:101 name "Ann" age 21 branch "CSE" marks 90
• READ – Retrieve Student Details
HGETALL student:101
HGET student:101 name
• UPDATE – Modify Student Marks
HSET student:101 marks 95
• DELETE – Remove Student Record
DEL student:101
COLUMN-ORIENTED DATABASE
► While a relational database stores data in rows and reads data row by row, a
column store is organized as a set of columns.
► When you want to run analytics on a small number of columns, you can
read those columns directly without consuming memory with the unwanted
data.
► Columns are often of the same type and benefit from more efficient
compression, making reads even faster.
► Columnar databases can quickly aggregate the value of a given column
(adding up the total sales for the year, for example). Use cases include
analytics.
COLUMN-ORIENTED DATABASE
COLUMN-ORIENTED DATABASE
► Column databases use the concept of keyspace, which is sort of like a
schema in relational models.
► This keyspace contains all the column families, which then contain rows, which
then contain columns.
COLUMN-ORIENTED DATABASE
► If we take a specific row as an example:
► The Row Key is exactly that: the specific identifier of that row and is always
unique.
► The column contains the name, value, and timestamp, so that’s straightforward.
The name/value pair is also straight forward, and the timestamp is the date and
time the data was entered into the database.
► Some examples of column-store databases include Casandra, CosmoDB,
Bigtable, and HBase.
COLUMN-ORIENTED DATABASE
Cassandra- Column Database
Cluster
CQL
Use cases for Column-Oriented
Databases
► Developers mainly use column databases in:
► Content management systems
► Blogging platforms
► Systems that maintain counters
► Services that have expiring usage
► Systems that require heavy write requests (like log aggregators)
Benefits of Column-Oriented Databases
► There are several benefits that go along with columnar databases:
► Column stores are excellent at compression and therefore are efficient in
terms of
storage.
► You can reduce disk resources while holding massive amounts of
information in a single column
► Since a majority of the information is stored in a column, aggregation
queries are quite fast, which is important for projects that require large
amounts of queries in a small amount of time.
► Scalability is excellent with column-store databases.
► They can be expanded nearly infinitely, and are often spread across large clusters
of
machines, even numbering in thousands.
► That also means that they are great for Massive Parallel Processing
Benefits of Column-Oriented Databases
► Load times are similarly excellent, as you can easily load a billion-
row table in a few seconds.
► You can load and query nearly instantly.
► Large amounts of flexibility as columns do not necessarily have to
look like each other.
► You can add new and different columns without disrupting the whole
database.
RDBMS Cassandra
RDBMS deals with structured data. Cassandra deals with unstructured data.
In RDBMS, a table is an array of arrays. (ROW x In Cassandra, a table is a list of “nested key-
COLUMN) value pairs”. (ROW x COLUMN key x COLUMN
value)
Database is the outermost container that Keyspace is the outermost container that
contains data corresponding to an application. contains data corresponding to an application.
Tables are the entities of a database. Tables or column families are the entity of a
keyspace.
●Linking
Embedding & Linking
JSON
🠶 “JavaScript Object Notation”
🠶 Easy for humans to write/read, easy for computers
to parse/generate
🠶 Objects can be nested
🠶 Built on
🠶 name/value pairs
🠶 Ordered list of values
[Link]
BSON
• “Binary JSON”
• Binary-encoded serialization of JSON-like docs
• Also allows “referencing”
• Embedded structure reduces need for joins
• Goals
– Lightweight
– Traversable
– Efficient (decoding and encoding)
[Link]
{ BSON Example
"_id" : "37010"
"city" : "ADAMS",
"pop" : 2660,
"state" : "TN",
“councilman” : {
name: “John Smith”
address: “13 Scenic Way”
}
}
CRUD Query Language
Create, Read, Update, Delete
CRUD: Using the Shell
To insert documents into a collection/make a new collection:
db.<collection>.insert(<document>)
<=>
INSERT INTO <table>
VALUES(<attributevalues>);
CRUD: Inserting Data
Insert one document
db.<collection>.insert({<field>:<value>})
SELECT field1
FROM <table>;
UPDATE <table>
SET <field2> = <value2>
WHERE <field1> = <value1>;
CRUD: Updating
To remove a field
db.<collection>.update({<field>:<value>},
{ $unset: { <field>: 1}})
db.<collection>.remove({<field>:<value>}, true)
DOCUMENT-ORIENTED
DATABASES
● Goals
○ Declarativity
○ Change
● Design Choice : Have unique instance identifiers vs. having
foreign keys
○ Close in Spirit to OO
○ Will allow us to cope easier with Change
○ Declarativity is an issue in OO, but not for GDB as we will show
Database Representation
● Sailors(sid:integer, sname:char(10), rating: integer, age:real)
● Boats(bid:integer, bname:char(10), color:char(10))
● Reserve(sid:integer, bid:integer, day:date)
sid sname rating age sid bid day bid bname color
name dustin
IOF ID1
rating 7
sid 31
IOF
IOF lubber
name
IOF ID2
Boats
rating 8
Reserves
IOF IOF age 55.5
IOF ID8
ID6 ID3
…
ID4 ID5 : ID7 :
: :
Foreign Keys
sid 22
name dustin
ID1
rating 7
age 45.0
Sailor
sid 22
day 10/10/96
ID4
bid 101
Boat
bid 101
ID6
bname Interlake
color red
Data Representation in the GDB DDL
Name1 Val1
Name2 Val2
ID
……
NameN ValN
● ID:(Name1=Val1,…,NameN=ValN)
Examples:
ID1:(sid=22, name=“Dustin”, rating=7, age=45.0)
ID4:(sailor=ID1, day=“10/10/96”, boat=ID6)
ID6:(bid=101, bname=“Interlake”, color=“red”)
Defining New Concepts in GDB DDL– Grandson
P P
er er
Person so
so
nI nI
IOF O O
F F
_I S _I S _I
D GrSon _ID2 :- _ID1 o D o D
1 n 3 n 2
GrSon
_ID1:(GrSon=_ID2) :- _ID1:
(IOF=“Person”,Son=_ID3),
_ID3:(IOF=“Person”, Son=_ID2),
_ID2:(IOF=“Person”).
[DML-QL] Writing simple queries:
•The names of all sailors who have reserved a red boat
Sailor
_X _X Boats
s
_ID:(Name = _X) :-
_ID:(IOF = Sailors, Boat = _ID1, Name = _X),
_ID1:(IOF = Boats, Color = Red).
Informal Semantics
Query match
Facts
Extended Graph
RDBMS => GDB
● Sailors(sid:integer, sname:char(10), rating: integer, age:real)
● Boats(bid:integer, bname:char(10), color:char(10))
● Reserve(sid:integer, bid:integer, day:date)
sid sname rating age sid bid day bid bname color
Boo
k
IOF
Titl Database
e s
_ID
Aut Ramakris
hor han
Aut
Gehrke
hor
G
en
Change
e_
ex
I
O
F
va
_I 0. I _I va
lu x
D 7 O D lu
e 1
E FI 1_I eva
x x
O D lu
p 2:
FI 2
_I e
va
x
O D lu
N
F n e
Aggregate
Operation
High Order Queries
Find all the fields from Tables that contain the name John.
_ID:(Name=_X) :-
_ID1:(IOF=Tables),
_ID2:(IOF=_ID1, _X=“John”).
GDB vs. OO, XML, OR, CG, …
● GDB are close in spirit to OO but not the same (GDB : no
encapsulation + more IDs).
● Close To Datalog but with IDs(links) vs foreign keys
● The same for ORDBMs and somewhat XML
● Close to Conceptual Graphs But CG do not have IDs
● We can also use foreign keys: _ID:[ _ID(IOF =
Sailors, sname = lubber)].
OO vs. GDB
OO GDB
P
er
ID
so
1 nI
Class: Person O ag 4 C
age: 42 F
I e 2 ar
name: john na Jo
D I
car m h
ID 1 O
e n FI
2 ca
Class: Car r
D
color: red 2
co
lo
r
re
d
Translating NoSQL Knowledge to Graph
► With the advent of the NoSQL movement, businesses of all sizes
have a variety of modern options from which to build solutions
relevant to their use cases.
► Calculating average income? Ask a relational database.
► Building a shopping cart? Use a key-value Store.
► Storing structured product information? Store as a document.
► Describing how a user got from point A to point B? Follow a graph.
► Examples of Graph Databases
► Neo4j, ArangoDB
GRAPH_BASED NoSQL
ArangoDB[graph data model]
ArangoDB's, - requires two kinds of collections
— the first is the document collections (known as vertices collections in
group-theoretic language),
— the second is the edge collections.
—-- Edge collections also store documents, but they are characterized by
including two unique attributes, _from and _to for creating relations
between documents.
In practice, a document (read edge) links two documents (read vertices),
both stored in their respective collections.
This architecture is derived from the graph-theoretic concept of a
labeled, directed graph, excluding edges that can have not only labels,
but can be a complete JSON like document in itself.
Key-value vs. Graph: Data Model
Differences
Key-Value
Key-Value as
Model
Graph
Document vs. Graph: Data Model
Differences
Document as
Document Model Graph
References
► [Link]
► [Link]
► [Link]
► [Link]
atabase- [Link]
► [Link]
etailed- overview
• Create Nodes
CREATE (s1:Student {name:"Anu", age:21})
CREATE (s2:Student {name:"Rahul", age:22})
CREATE (c1:Course {name:"DBMS"})
• Create Relationship
MATCH (s:Student {name:"Anu"}), (c:Course {name:"DBMS"}) CREATE (s)-[:ENROLLED_IN]->(c)
• Read (Find Courses of Anu)
MATCH (s:Student {name:"Anu"})-[:ENROLLED_IN]->(c) RETURN c
• Update
MATCH (s:Student {name:"Anu"}) SET [Link] = 23
Delete
MATCH (s:Student {name:"Rahul"}) DELETE s
What is a Column family store ?
• Column family systems are important NoSQL data architecture
patterns because they can scale to manage large volumes of data
Columns store databases use a concept called
a keyspace. A keyspace is kind of like a schema
in the relational model. The keyspace contains
all the column families (kind of like tables in the
relational model), which contain rows, which
contain columns.
•A column family consists of multiple rows.
db = client["college"]
collection = db["students"]
collection.insert_one(student1)
collection.delete_one({"roll": 101})