Databases SQL, NoSQL, ORMs, REST/GraphQL, JSON/gRPC SQL QL (1974) - - PowerPoint PPT Presentation

databases
SMART_READER_LITE
LIVE PREVIEW

Databases SQL, NoSQL, ORMs, REST/GraphQL, JSON/gRPC SQL QL (1974) - - PowerPoint PPT Presentation

Databases SQL, NoSQL, ORMs, REST/GraphQL, JSON/gRPC SQL QL (1974) 4) Initially, relational databases with query languages based on mathematical logic SQL (Chamberlin, Boyce @ IBM Research) Query language accessible to those without


slide-1
SLIDE 1

SQL, NoSQL, ORMs, REST/GraphQL, JSON/gRPC

Databases

slide-2
SLIDE 2

SQL QL (1974) 4)

 Initially, relational databases with query languages based on

mathematical logic

 SQL (Chamberlin, Boyce @ IBM Research)

 Query language accessible to those without formal training in

mathematics or computer programming

 Hugely successful

 DB2, Oracle, SQL Server, PostgreSQL

 Databases support ACID properties

 Atomic (transactions fully completed or not begun at all)  Consistent (system must remain in valid state, errors rolled back)  Isolated (each transaction executes as if it is the only one)  Durable (all changes permanent)

 Then, the Internet and WWW happened….

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-3
SLIDE 3

WWW WWW

 High volume, high velocity data  Non-uniform, "dirty" data  Can not be handled by traditional relational database  ACID properties not required (and difficult to scale-up to large

amounts of data)

 Motivates move to non-relational databases

 Bigtable, Dynamo, MongoDB, Cassandra, etc.  Eschew the use of SQL to store, query, and retrieve data “NoSQL”

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-4
SLIDE 4

NoSQL QL

 Large-scale datastores with no standard query language  Often schemaless (e.g. key-value storage of arbitrary types)

 Like a Python dictionary  Akin to static typing (SQL) vs. dynamic typing (NoSQL)

 Limited querying support (i.e. no JOINs)  Often with weak consistency (BASE)

 Basically Available (requests may return failure or a transient inconsistent

state)

 Soft state (system always moving towards consistent state)  Eventual consistency (when input stops, system will reach a consistent

state)

 But, get performance and scale!  Huge variety of implementations tailored to specific application

 http://nosql-database.org/  How to choose? (Take 486/586 + Cloud and Cluster course)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-5
SLIDE 5

SQL QL vs.

  • s. NoSQL

QL

Portland State University CS 430P/530 Internet, Web & Cloud Systems

SQL NoSQL (initially) Relational (structured) data Non-relational (unstructured) data Complex querying (JOINs) Simple key-value lookup Schema No schema ACID (Atomic, Consistent, Isolated, Durable) consistency BASE (Basic Availability Soft-state Eventual) consistency

slide-6
SLIDE 6

NewS wSQL: QL: Reven enge ge of SQL QL?

 Bolt on SQL features to NoSQL

 Transaction processing (OLTP)  SQL interfaces on top of Hadoop and Spark

 Bolt on NoSQL features to SQL

 Horizontally scalable SQL systems (H-Store, Spanner)

 Get the best of both worlds

 SQL querying and consistency  NoSQL scaling

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-7
SLIDE 7

SQL QL as s th the un e unifyi ifying ng lang nguage? uage?

 Timescale blog

 https://blog.timescale.com/why-sql-beating-nosql-what-this-means-

for-future-of-data-time-series-database-348b777b847a

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-8
SLIDE 8

http://www.w3schools.com/sql

SQL

slide-9
SLIDE 9

SQL QL: : Str truct ucture ured d Qu Quer ery y Lang ngua uage ge

 Standard language for accessing and manipulating databases

 MySQL, Postgres SQL, SQLite, SQL Server, Access, Oracle, Sybase,

DB2, etc.

 Major commands are same between them

 Includes two things

 Data Definition Language (DDL)  Data Manipulation Language (DML)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-10
SLIDE 10

SQL Data Definition Language

slide-11
SLIDE 11

SQL QL DD DDL (Da Data ta De Defini inition) tion)

 Permits databases and database tables to be created or deleted

(among other operations).

 CREATE creates a new database, table, index  DROP : deletes a database, table, index

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-12
SLIDE 12

CREA EATE TE

 CREATE DATABASE

 Used to create a database

 CREATE TABLE

 Used to create a table within database

Portland State University CS 430P/530 Internet, Web & Cloud Systems

CREATE DATABASE database_name; CREATE DATABASE my_db;

CREATE TABLE table_name( column_name1 data_type, column_name2 data_type, . . . ); CREATE TABLE persons ( P_Id INT, LastName VARCHAR(25), FirstName VARCHAR(25), Address VARCHAR(25), City VARCHAR(15), PRIMARY KEY (P_Id) );

slide-13
SLIDE 13

Ba Basi sic c SQL QL Da Dataty tatypes pes

 Note: Data must be clean! (Not easy to guarantee)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

Data type Description Storage Byte Allows whole numbers from 0 to 255 1 byte Integer Allows whole numbers between -32,768 and 32,767 2 bytes Long Allows whole numbers between -2,147,483,648 and 2,147,483,647 4 bytes Single Single precision floating-point. Will handle most decimals 4 bytes Double Double precision floating-point. Will handle most decimals 8 bytes

slide-14
SLIDE 14

SQL Data Manipulation Language

slide-15
SLIDE 15

Ba Basi sic c fun unctions ctions us used ed in web eb app pps

 CRUD

 Create = INSERT INTO  Read = SELECT  Update = UPDATE  Delete = DELETE

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-16
SLIDE 16

SEL ELEC ECT

 Used to read data from a database

 Result stored in a table, called the result-set

Portland State University CS 430P/530 Internet, Web & Cloud Systems

P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger

Persons Table SELECT FirstName, LastName FROM Persons; SELECT * FROM Persons; SELECT column_name(s) FROM table_name;

slide-17
SLIDE 17

WHER ERE E clause ause

 Add a predicate to limit what is returned

 Text values should be quoted by single quotes or double quotes  Numeric values do not need to be enclosed in quotes

Portland State University CS 430P/530 Internet, Web & Cloud Systems

SELECT column_name(s) FROM table_name WHERE column_name operator value; SELECT * FROM persons WHERE city='Sandnes';

P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger

Persons Table

slide-18
SLIDE 18

INSER ERT T INTO

 Used to create new records in a table  Example

Portland State University CS 430P/530 Internet, Web & Cloud Systems

P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger

Persons Table

CREATE DATABASE test; CREATE TABLE persons ( P_Id INT, LastName VARCHAR(25), FirstName VARCHAR(25), Address VARCHAR(25), City VARCHAR(15), PRIMARY KEY (P_Id) ); INSERT INTO persons VALUES (1, 'Hansen', 'Ola', 'Timoteivn10', 'Sandnes'); INSERT INTO persons VALUES (2, 'Svendson', 'Tove', 'Borgvn23','Sandnes'); INSERT INTO persons VALUES (3, 'Pettersen', 'Kari', 'Storgt20', 'Stavanger');

slide-19
SLIDE 19

UPDATE TE

 Used to update records in a table

Portland State University CS 430P/530 Internet, Web & Cloud Systems

UPDATE table_name SET column=value, column2=value2,… WHERE some_column=some_value; UPDATE Persons SET Address='Nissestien 67', city='Sandnes' WHERE lastname='Tjessem' AND firstname='Jakob';

slide-20
SLIDE 20

DE DELET ETE E st statement ement

 Used to delete records in a table

Portland State University CS 430P/530 Internet, Web & Cloud Systems

DELETE FROM table_name WHERE some_column=some_value; DELETE FROM persons WHERE lastname='Tjessem' AND firstname='Jakob';

slide-21
SLIDE 21

Ot Other er comm mmon

  • n SQL

QL pr primi mitiv tives es

 Pattern matching

 % wildcard for matching 0 or more characters  _ wildcard for matching exactly 1 character

 Result processing

 LIMIT  ORDER BY / DESC / ASC  DISTINCT

 Table combining

 JOIN (INNER, LEFT, RIGHT, FULL)

 Constraints

 NOT NULL  UNIQUE  PRIMARY KEY  FOREIGN KEY  CHECK (constraint)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-22
SLIDE 22

Gu Gues estbo tbook

  • k app

pp

 https://bitbucket.org/wuchangfeng/cs430-src  WebDev_Guestbook_v3_nginx_uwsgi

 Simple MVC app in Python/Flask  Models

 sqlite3 model in model_sqlite3.py based on previously

defined interface in Model.py

 sqlite3 uses a file in the file system to store database (see iPhone)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

. ├── Model.py ├── model_pylist.py ├── model_sqlite3.py ├── app.py ├── requirements.txt ├── templates │ ├── layout.html │ └── index.html └── static └── style.css

slide-23
SLIDE 23

model_sqlite3.py

 Derived class that implements Model using

sqlite3

Portland State University CS 430P/530 Internet, Web & Cloud Systems

DB_FILE = 'entries.db' # file for our Database class model(Model): def __init__(self): """ Create db on startup if it does not exist """ connection = sqlite3.connect(DB_FILE) cursor = connection.cursor() try: cursor.execute("select count(rowid) from guestbook") except sqlite3.OperationalError: cursor.execute("create table guestbook (name text, email text, signed_on date, message)") cursor.close() def select(self): """ Gets all rows from the database Each row contains: name, email, date, message """ connection = sqlite3.connect(DB_FILE) cursor = connection.cursor() cursor.execute("SELECT * FROM guestbook") return cursor.fetchall() . ├── Model.py ├── model_pylist.py ├── model_sqlite3.py ├── app.py ├── requirements.txt ├── templates │ ├── layout.html │ └── index.html └── static └── style.css

slide-24
SLIDE 24

. ├── Model.py ├── model_pylist.py ├── model_sqlite3.py ├── app.py ├── requirements.txt ├── templates │ ├── layout.html │ └── index.html └── static └── style.css

model_sqlite3.py

Portland State University CS 430P/530 Internet, Web & Cloud Systems

def insert(self, name, email, message): """ Inserts entry into database (name, email, date, message) """ params = {'name':name, 'email':email, 'date':date.today(), 'message':message} connection = sqlite3.connect(DB_FILE) cursor = connection.cursor() cursor.execute("insert into guestbook (name, email, signed_on, message) VALUES (:name, :email, :date, :message)", params) connection.commit() cursor.close() return True

Note use of parameterized queries to avoid SQL injection vulnerability

slide-25
SLIDE 25

SQL Functions

slide-26
SLIDE 26

SQL QL agg ggregat egate e func unctions tions

 SQL aggregate functions: return a single value calculated from values

in a column

 AVG(), COUNT(), MAX(), MIN(), SUM()

Portland State University CS 430P/530 Internet, Web & Cloud Systems

SELECT AVG(column_name) FROM table_name SELECT AVG(UnitPrice) AS UnitAverage FROM Products SELECT ProductName FROM products WHERE UnitPrice>( SELECT AVG(UnitPrice) FROM Products) P_Id ProductName UnitPrice UnitsInStock UnitsOnOrder 1 Jarlsberg 10.45 16 15 2 Mascarpone 32.56 23 3 Gorgonzola 15.67 9 20

slide-27
SLIDE 27

Iss ssue ues

 Computation (SQL functions) and data selection (WHERE) on

large-scale, high-velocity data

 Can a database scale to store a non-stop firehose of data?  Can a database handle queries and functions on top of the firehose?  The issues Google faced when building their initial search engine

 Motivates

 Non-standard data storage

 NoSQL, Distributed key-value stores

 Non-standard querying, parallel data stream processing approaches

 Map-Reduce, Hadoop, Spark and other data processing pipelines

 "Data Science"

 Will revisit both when we get to cloud topics

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-28
SLIDE 28

Object-Relational Mapping (Write Python instead of SQL)

slide-29
SLIDE 29

OR ORMs

 Take objects and map them directly to relational database

 SQL taken care of by underlying library

 Sacrifice a little bit of performance and SQL purity for faster

development

 Developers do not have to learn a second language!  Potentially cleaner code  Minimal SQL injection risks  Unify insertion, querying, updating and deleting to/from SQL, NoSQL,

and NewSQL databases

 Will see examples in GCP cloud database APIs

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-30
SLIDE 30

SQL QL CREA EATE TE TABLE BLE (sq sqlit lite3) e3)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-31
SLIDE 31

SQL QLAlchem Alchemy OR ORM eq equiv uivale alent nt

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-32
SLIDE 32

SQL QL vs.

  • s. SQL

QLAlchem Alchemy: : Inser sertio tion

 Insert into DB with SQL (sqlite3)  Insert into DB via ORM

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-33
SLIDE 33

SQL QLAlchem Alchemy y exa xample: ple: Qu Quer erying ying

 SQL (sqlite3)  SQLAlchemy

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-34
SLIDE 34

Database queries via REST/JSON (Write URLs instead of SQL)

Web APIs, Web EndPoints

slide-35
SLIDE 35

Progra

  • grammatic

mmatically ally call all into databas atabase e via Web eb APIs Is (En EndP dPoints

  • ints)

 Key for modern client-side web frameworks

 Directly interacting with "model"

 Explosive growth

 Protecting an estimated $2.2 billion in assets

 https://www-03.ibm.com/press/us/en/pressrelease/48026.wss

 Each API with 5-6 versions per year (PeachTech)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-36
SLIDE 36

Gr Growth wth in Web eb API se security urity iss ssue ues

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-37
SLIDE 37

Web eb APIs/EndP s/EndPoints

  • ints

 2 parts

 API (REST, GraphQL)  Data-exchange format (JSON, gRPC)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-38
SLIDE 38

RES EST APIs Is

 Representational State Transfer  Style of web software architecture that simplifies application  Not a standard, but a design pattern

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-39
SLIDE 39

RES EST APIs Is

 Take all resources for web application (data, files, functions)

 Identify each resource and action on resource via an HTTP method

and URL.

 Method selects action  Send arguments via the HTTP request (e.g. in URL, URL parameters,

  • r payload)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

Where (URL) What (App- defined) How (HTTP method) Resources (Data, files, functions)

slide-40
SLIDE 40

RES EST toy exa xample ple

 http://foo.com/bar/users  Server foo.com  Database bar  Table users  URL returns table users in database bar in a particular format

(XML, JSON)

 Effectively turn URLs into pre-canned SQL  Common examples

 Twitter, Flickr, Amazon

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-41
SLIDE 41

RES EST and nd HTTP TP met methods ds

 HTTP request methods indicate the desired action  GET

 Requests a representation of the specified resource.  Use for operations that have NO side-effects (safe operations)  Works with robots and crawlers.

 POST

 Submits data to be processed (e.g., from an HTML form) to the

identified resource. Data is included in the body of the request.

 PUT

 Uploads a representation of the specified resource.

 DELETE

 Deletes the specified resource.

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-42
SLIDE 42

Iss ssue ues s wi with th RES EST

 URLs into SQL  Problem

 Must provide a fixed mapping between what app wants and what the API

actually implements

 Overfetching

 API returns much more data than user asks for

 Underfetching

 API must be queried multiple times to get all the data user needs  Example: queries to different tables in a backend require multiple API requests

 Slows down DevOps

 Front-end developers must coordinate with backend engineers to get additional API

endpoints defined when needed

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-43
SLIDE 43

Gr Graph aphQL QL

 Facebook (2015)  REST mapping to SQL query too coarse

 Rigid queries based on URL and a JSON result that must then be parsed  Queries on multiple tables require multiple requests  Results returned as text that must be parsed

 GraphQL

 Language that allows client to specify details of what it wants

 Give front-end programmable control over query

 Specify the structure of how the result data should be returned

 Helps strongly typed client code ingest data

 Akin to giving clients direct SQL-like interface to backend database

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-44
SLIDE 44

 Example

 Return only the balance, email, and subscriptions columns for a

particular user

 Similar to a custom "SELECT" issued from the client  Reduces payload size of returned results (making it good for mobile)

 Batch multiple queries together in a single request

Portland State University CS 430P/530 Internet, Web & Cloud Systems

getUser(id: "user_123") { balance, email, subscriptions } userA: getUser(id: "user_123") { email } userB: getUser(id: "user_456") { email }

slide-45
SLIDE 45

Da Data ta-exchang change e formats mats

 JavaScript Object Notation (JSON)

 De-facto web object data format (versus XML)

 Subset of JavaScript  Minimal, lightweight, text-based syntax  Easy to parse and generate

 Prevalent in most web sites and web APIs, often as part of a REST

architecture

 Designed to enable stateful, real-time communication between browser

and web application

 Often used via AJAX to allow web server to directly modify elements of a page

without refresh

 AJAX = Asynchronous Javascript And XML

 But now it's all AJAJ

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-46
SLIDE 46

JS JSON ON objects ects

 Objects are unordered containers of key/value pairs similar to

Python dictionaries

 Keys are strings  Values are JSON values  Wrapped in { }

 , separates key/value pairs  : separates keys and values

 Parsed into native language data structures as struct, record,

hashtable, or dictionary

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-47
SLIDE 47

JS JSON ON Values lues

 Strings

 Sequence of 0 or more Unicode characters wrapped in double quotes

 Numbers

 Integer, Real, Scientific  No octal or hex  No NaN or Infinity

 Booleans

 true, false

 null

 A value that isn't anything

 Objects  Arrays

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-48
SLIDE 48

JS JSON ON exa xample ple

Portland State University CS 430P/530 Internet, Web & Cloud Systems

{ "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ "212 555-1234", "646 555-4567" ] } Name/Value Pairs Number data type String Array Child properties

slide-49
SLIDE 49

RES EST wi with th JS JSON ON

 Common pattern

Portland State University CS 430P/530 Internet, Web & Cloud Systems

import requests response = requests.get('https://api.adviceslip.com/advice') advice_dict = response.json() print(advice_dict["slip"]["advice"])

slide-50
SLIDE 50

JS JSON ON iss ssues ues

 Text format

 Binary data must be encoded (Base64)  Space overhead in using text

 Flat format

 Object treated as a string  Must be parsed to obtain original object (e.g. JSON.parse())  Processing overhead in going to/from string and into programming-

language specific data structure

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-51
SLIDE 51

gR gRPC

 Initially developed by Google as ProtoBufs (protocol buffers)  Support serialization of structured data in an arbitrary (potentially

binary) format

 Derived from FlatBuffer's zero-copy deserialization idea  Minimal processing to go from wire-line format to language-specific

representation

 ProtoBuf generators for C++, Java, C#, Python, etc.  Each protobuf contains an interface description language specifying

format of the stream of bytes transmitted

 Uses HTTP/2 as a transport

 Used throughout Google's infrastructure, but now open-sourced

 Mandatory for certain cloud services that are data-intensive (e.g.

BigQuery)

 When to use…

 https://codeclimate.com/blog/choose-protocol-buffers/

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-52
SLIDE 52

Cloud SQL AWS RDS (Relational Database Service) Azure SQL Database

SQL databases in the cloud

slide-53
SLIDE 53

"Lif ift t and nd Shift" t"

 Move application running on-premises directly into the cloud  Simplest way to take advantage of cloud services  Keeps application largely the same  Can be done in entirety (e.g. application and backend servers moved

to the cloud)

 Can be done as a "hybrid" deployment (e.g. only backend runs in the

cloud and application connects from on-premises)

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-54
SLIDE 54

Go Google gle Cloud ud SQL QL

 Drop-in replacement for MySQL (or Postgres) relational databases

 No need to purchase hardware or install software  No need reconfigure code  Automatic backups and software updates  Data encrypted in flight and at rest  Accessible only to project (unless external access allowed)  Hosted by a Compute Engine instance you configure

 Security

 No access to database from outside project unless specified

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-55
SLIDE 55

AWS Rel elational ational Da Database tabase Ser ervice ice

 Similar to Cloud SQL, but with more

  • ptions

 Amazon Aurora, MySQL, PostgreSQL,

Oracle, Microsoft SQL Server, Maria DB

 As with Google Cloud SQL, can

customize hardware used for server

Portland State University CS 430P/530 Internet, Web & Cloud Systems

slide-56
SLIDE 56

Database Labs #1-3