Data 101 - Data Engineering
Guest Lecture - Luke Liu��NoSQL + Semi-structured Data
Announcements
Goal of Today’s Lecture
Next Lecture (and Project 4): you will dive into querying semi-structured data using one of the most popular NoSQL databases, MongoDB
Contrasting Data Models
6
Assumptions on Structured Data
7
Semi-Structured Data
8
NoSQL Motivation
NoSQL databases emerged in late 2000s
NoSQL: reduced functionality
Scaling the Database
Two approaches:
Consistency is much harder to enforce
Consistency: The guarantee that any transactions started in the future necessarily see the effects of other transactions committed in the past (e.g. in other words, the database always reflects the most current changes)
Scaling Through Partitioning
Scaling Through Replication
Relational Model: Scalability
NoSQL: Scalability
SQL vs NoSQL: Question 1
The goal is to build a service for tens of millions of Amazon shoppers, which will store each shopper’s past 100 viewed products. The stored data is shopper specific, and should be used for targeted product advertisements towards that shopper. It’s fine if this data is a little out-of-date. What kind of database should be used?
Hint: This database should be able to look up the cached result by the SQL query (string).
Hint 2: The stored data is non-relational.
Solution: NoSQL
SQL vs NoSQL: Question 2
The goal is to build a service at PayPal that can allow users to apply for loans through the app. The service needs a database to store loan applications. In addition to the loan amount, the application also needs information regarding the user’s current balance and prior transaction history. What kind of database should be used?
Hint: This is a financial application where data consistency is very important.
Hint 2: Data about the loan, user’s balance and transaction history all need to be stored, and there’s relationships between these data.
Solution: SQL
Data Models for Semi-structured Data
Key-Value Stores
Key-Value Stores Scaling
Example
How would you represent the Flights data as key, value pairs?
Flights(fid, date, carrier_id, flight_num, origin, dest, ...) Carriers(cid, name)
Document Stores Motivation
JSON
22
JSON Syntax
23
JSON Types
JSON Semantics: a Tree !
person
Mary
name
address
name
address
street
no
city
Maple
345
SF
John
Thai
phone
23456
0
1
{“person”:
[ {“name”: “Mary”,
“address”: � {“street”:“Maple”,� “no”:345,� “city”: “SF”}},� {“name”: “John”,� “address”: “Thailand”,� “phone”:2345678}}�]
}
Recall: arrays are ordered in JSON!
Transforming Rectangular Data to JSON
name
name
name
phone
phone
phone
“John”
3634
“Sue”
“Dirk”
6343
6363
Person
person
name | phone |
John | 3634 |
Sue | 6343 |
Dirk | 6363 |
{“person”: [�{“name”: “John”, “phone”:3634},�{“name”: “Sue”, “phone”:6343},
{“name”: “Dirk”, “phone”:6383}�]
}
Transforming Rectangular Data to JSON
Person
name | phone |
John | 3634 |
Sue | 6343 |
May inline multiple relations based on foreign keys
Orders
personName | date | product |
John | 2002 | Gizmo |
John | 2004 | Gadget |
Sue | 2002 | Gadget |
{“Person”:
[{"name": "John",
"phone":3646,�"Orders":[� {"date":2002,"product":"Gizmo"},� {“date”:2004,"product":"Gadget"}�]
},
{"name": "Sue",�"phone":6343,�"Orders":[
{"date":2002,"product":"Gadget"}�]
}
]
}
Transforming Rectangular Data to JSON
Person
name | phone |
John | 3634 |
Sue | 6343 |
Many-many relationships are more difficult to represent
Orders
personName | date | product |
John | 2002 | Gizmo |
John | 2004 | Gadget |
Sue | 2002 | Gadget |
prodName | price |
Gizmo | 19.99 |
Phone | 29.99 |
Gadget | 9.99 |
Product
Options for the JSON file:
Q: How many times will the product information be repeated in Option 2?
Q: What is the size of the eventual JSON file?
Transforming JSON to Rectangular Data
name | phone |
John | 1234 |
Joe | NULL |
{“person”:� [{“name”:“John”, “phone”:1234},� {“name”:“Joe”}]�}
no phone !
Transforming JSON to Rectangular Data
name | phone | |
Mary | 2345 | 3456 |
| | |
???
{“person”:� [{“name”:”John”, “phone”:1234},� {“name”:”Mary”, “phone”:[1234,5678]}]�}
Two phones !
name | phone1 | phone2 |
Mary | 2345 | 3456 |
| | |
name | phone |
Mary | 2345 |
Mary | 3456 |
Transforming JSON to Rectangular Data
{“person”:� [{“name”:“Sue”, “phone”:3456},� {“name”:{“first”:“John”, “last”:“Smith”},“phone”:2345}� ]�}
Structured�name !
Concrete JSON Example
{
"ID": "22222",
"name": {
“firstname": "Albert",
“lastname": "Einstein"
},
"deptname": "Physics",
"children": [
{"firstname": "Hans", "lastname": "Einstein" },
{"firstname": "Eduard", "lastname": "Einstein" }
]
}
32
How might I represent this in the relational model?
Why JSON?
33
Cons for JSON
34
XML
<course>
<course id> CS194/INFO290 </course id>
<title> Data Engineering </title>
<depts>
<dept name> Data Science </dept name>
<dept name> School of Information </dept name>
</depts>
<credits> 4 </credits>
</course>
35
XML Query Languages
36
Discussion: Why Semi-Structured Data?
Document Stores: JSON-Native Data Systems
38
Is Semi-Structured Data New?
39
So what do we do?
40
One Excellent Use Case:
Data Sharing, Exchange, and Transfer
41
Summary
42