Storage Tier. Mendel Rosenblum. CS142 Lecture Notes - Database.js

Similar documents
MongoDB Web Architecture

Topics. History. Architecture. MongoDB, Mongoose - RDBMS - SQL. - NoSQL

Front End Programming

About 1. Chapter 1: Getting started with mongoose 2. Remarks 2. Versions 2. Examples 4. Installation 4. Connecting to MongoDB database: 4

Relational databases and SQL

Sessions. Mendel Rosenblum. CS142 Lecture Notes - Sessions

Overview. * Some History. * What is NoSQL? * Why NoSQL? * RDBMS vs NoSQL. * NoSQL Taxonomy. *TowardsNewSQL

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

The NoSQL movement. CouchDB as an example

Tuesday, January 13, Backend III: Node.js with Databases

Databases/JQuery AUGUST 1, 2018

Input and Validation. Mendel Rosenblum. CS142 Lecture Notes - Input

Extra Notes - Data Stores & APIs - using MongoDB and native driver

Mongoose for Application Development

relational Key-value Graph Object Document

! An organized collection of data. ! Can easily be accessed, managed, and updated. ! Data are organized as a set of tables.

MySQL. A practical introduction to database design

10/18/2017. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Cassandra, MongoDB, and HBase. Cassandra, MongoDB, and HBase. I have chosen these three due to their recent

MongoDB Schema Design

COMP 430 Intro. to Database Systems. Encapsulating SQL code

Express.JS. Prof. Cesare Pautasso Modularity

Introduction to pysqlite

Find All Tables Containing Column With Specified Name Oracle

JSON Home Improvement. Christophe Pettus PostgreSQL Experts, Inc. SCALE 14x, January 2016

SQL Data Definition Language: Create and Change the Database Ray Lockwood

MongoDB. History. mongodb = Humongous DB. Open-source Document-based High performance, high availability Automatic scaling C-P on CAP.

MongoDB w/ Some Node.JS Sprinkles

Non-Relational Databases. Pelle Jakovits

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

Online Multimedia Winter semester 2015/16

A NoSQL Introduction for Relational Database Developers. Andrew Karcher Las Vegas SQL Saturday September 12th, 2015

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

5/1/17. Announcements. NoSQL Motivation. NoSQL. Serverless Architecture. What is the Problem? Database Systems CSE 414

Future Web App Technologies

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

MongoDB - a No SQL Database What you need to know as an Oracle DBA

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

PostgreSQL/Jsonb. A First Look

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Topic 12: Connecting Express and Mongo

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Distributed Non-Relational Databases. Pelle Jakovits

Stored procedures - what is it?

MySQL 101. Designing effective schema for InnoDB. Yves Trudeau April 2015

Create a simple database with MySQL

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

Databases (MariaDB/MySQL) CS401, Fall 2015

SQL Commands & Mongo DB New Syllabus

PART I SQLAlchemy Core

OKC MySQL Users Group

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

SQL, NoSQL, MongoDB. CSE-291 (Cloud Computing) Fall 2016 Gregory Kesden

How to Use JSON in MySQL Wrong

Administration Naive DBMS CMPT 454 Topics. John Edgar 2

A practical introduction to database design

Final Exam Review 2. Kathleen Durant CS 3200 Northeastern University Lecture 23

CMPE 131 Software Engineering. Database Introduction

<Insert Picture Here> MySQL Cluster What are we working on

CSE 344 JULY 9 TH NOSQL

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Final Exam Logistics. CS 133: Databases. Goals for Today. Some References Used. Final exam take-home. Same resources as midterm

Practical MySQL indexing guidelines

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Fiz: A Component Framework for Web Applications. John Ousterhout Stanford University

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Working with Databases and Java

What is Grails4Notes(TM)?

The Relational Model. Suan Lee

COMP 430 Intro. to Database Systems. SQL from application code

NoSQL Databases MongoDB vs Cassandra. Kenny Huynh, Andre Chik, Kevin Vu

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Database Application Architectures

Informix NoSQL-SQL-Crossover

Database Programming with PL/SQL

Oracle Database 10g Express

Introduction to MySQL. Database Systems

4.6.5 Data Sync User Manual.

Documenting APIs with Swagger. TC Camp. Peter Gruenbaum

ColdFusion Summit 2016

NoSQL & Firebase. SWE 432, Fall Web Application Development

5/2/16. Announcements. NoSQL Motivation. The New Hipster: NoSQL. Serverless. What is the Problem? Database Systems CSE 414

L6 Application Programming. Thibault Sellam Fall 2018

Database Systems CSE 414

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

NOSQL EGCO321 DATABASE SYSTEMS KANAT POOLSAWASD DEPARTMENT OF COMPUTER ENGINEERING MAHIDOL UNIVERSITY

Databases IIB: DBMS-Implementation Exercise Sheet 13

The NoPlsql and Thick Database Paradigms

Chapter 5: Physical Database Design. Designing Physical Files

sqoop Easy, parallel database import/export Aaron Kimball Cloudera Inc. June 8, 2010

Learning Objectives. Description. Your AU Expert(s) Trent Earley Behlen Mfg. Co. Shane Wemhoff Behlen Mfg. Co.

Using ElasticSearch to Enable Stronger Query Support in Cassandra

Chapter 24 NOSQL Databases and Big Data Storage Systems

Bottom line: A database is the data stored and a database system is the software that manages the data. COSC Dr.

SQL DATA DEFINITION LANGUAGE

CS143: Relational Model

Modern App Architecture

Transcription:

Storage Tier Mendel Rosenblum.js

Web Application Architecture Web Browser Web Server Storage System HTTP Internet LAN 2

Web App Storage System Properties Always available - Fetch correct app data, store updates Even if many request come in concurrently - Scalable From all over the world Even if pieces fail - Reliable / fault tolerant Provide a good organization of storing an application data Quickly generate the model data of a view Handle app evolving over time Good software engineering: Easy to use and reason about

Relational Database System Early on many different structures file system, objects, networks, etc. The database community decided the answer was the relational model Many in the community still think it is. Data is organized as a series of tables (also called called relations) A table is made of up of rows (also called tuples or records) A row is made of a fixed (per table) set of typed columns String: VARCHAR(20) Integer: INTEGER Floating-point: FLOAT, DOUBLE Date/time: DATE, TIME, DATETIME Others

Database Schema Schema: The structure of the database The table names (e.g. User, Photo, Comments) The names and types of table columns Various optional additional information (constraints, etc.)

Example: User Table ID first_name last_name location Column types ID - INTEGER first_name - VARCHAR(20) last_name - VARCHAR(20) location - VARCHAR(20) 1 Ian Malcolm Austin, TX 2 Ellen Ripley Nostromo 3 Peregrin Took Gondor 4 Rey Kenobi D'Qar 5 April Ludgate Awnee, IN 6 John Ousterhout Stanford,CA

Structured Query Language (SQL) Standard for accessing relational data Sweet theory behind it: relational algebra Queries: the strength of relational databases Lots of ways to extract information You specify what you want The database system figures out how to get it efficiently Refer to data by contents, not just name

SQL Example Commands CREATE TABLE Users ( id INT AUTO_INCREMENT, first_name VARCHAR(20), last_name VARCHAR(20), location VARCHAR(20)); INSERT INTO Users ( first_name, last_name, location) VALUES ('Ian', 'Malcolm', 'Austin, TX'); DELETE FROM Users WHERE last_name='malcolm'; UPDATE Users SET location = 'New York, NY WHERE id = 2; SELECT * FROM Users; SELECT * from Users WHERE id = 2;

Keys and Indexes Consider a model fetch: SELECT * FROM Users WHERE id = 2 Database could implement this by: 1. Scan the Users table and return all rows with id=2 2. Have built and index that maps id numbers to table rows. Lookup result from index. Uses keys to tell database that building an index would be a good idea Primary key: Organize data around accesses PRIMARY KEY(id) on a CREATE table command Secondary key: Other indexes (UNIQUE)

Object Relational Mapping (ORM) Relational model and SQL was a bad match for Web Applications Object vesus tables Need to evolve quickly 2 nd generation web frameworks (Rails) handled mapping objects to SQL DB Rail's Active Record Objects map to database records One class for each table in the database (called Models in Rails) Objects of the class correspond to rows in the table Attributes of an object correspond to columns from the row Handled all the schema creation and SQL commands behind object interface

NoSQL - MongoDB Using SQL databases provided reliable storage for early web applications Led to new databases that matched web application object model Known collectively as NoSQL databases MongoDB - Most prominent NoSQL database Data model: Stores collections containing documents (JSON objects) Has expressive query language Can use indexes for fast lookups Tries to handle scalability, reliability, etc.

Schema enforcement JSON blobs provide super flexibility but not what is always wanted Consider: <h1>hello {{person.informalname}}</h1> Good: typeof person.informalname == 'string' and length < something Bad: Type is 1GB object, or undefined, or null, or Would like to enforce a schema on the data Can be implemented as validators on mutating operations Mongoose - Object Definition Language (ODL) Take familiar usage from ORMs and map it onto Mongoose Effectively masks the lower level interface to MongoDB with something that is friendlier

Using: var mongoose = require('mongoose'); 1. Connect to the MongoDB instance mongoose.connect('mongodb://localhost/cs142'); 2. Wait for connection to complete: Mongoose exports an EventEmitter mongoose.connection.on('open', function () { // Can start processing model fetch requests }); mongoose.connection.on('error', function (err) { }); Can also listen for connecting, connected, disconnecting, disconnected, etc.

Mongoose: Schema define collections Schema assign property names and their types to collections String, Number, Date, Buffer, Boolean Array - e.g. comments: [ObjectId] ObjectId - Reference to another object Mixed - Anything var userschema = new mongoose.schema({ first_name: String, last_name: String, emailaddresses: [String], location: String });

Schema allows secondary indexes and defaults Simple index first_name: {type: 'String', index: true} Index with unique enforcement user_name: {type: 'String', index:{unique: true} } Defaults date: {type: Date, default: Date.now }

Secondary indexes Performance and space trade-off Faster queries: Eliminate scans - database just returns the matches from the index Slower mutating operations: Add, delete, update must update indexes Uses more space: Need to store indexes and indexes can get bigger than the data itself When to use Common queries spending a lot of time scanning Need to enforce uniqueness

Mongoose: Make Model from Schema A Model in Mongoose is a constructor of objects a collection May or may not correspond to a model of the MVC var User = mongoose.model('user', userschema); Create objects from Model User.create({ first_name: 'Ian', last_name: 'Malcolm'}, donecallback); function donecallback(err, newuser) { assert (!err); console.log('created object with ID', newuser._id); }

Model used for querying collection Returning the entire User collection User.find(function (err, users) {/*users is an array of objects*/ }); Returning a single user object for user_id User.findOne({_id: user_id}, function (err, user) { /* */ }); Updating a user object for user_id User.findOne({_id: user_id}, function (err, user) { // Update user object user.save(); });

Other Mongoose query operations - query builder var query = User.find({}); Projections query.select("first_name last_name").exec(donecallback); Sorting query.sort("first_name").exec(donecallback); Limits query.limit(50).exec(donecallback); query.sort("-location").select("first_name").exec(donecallback);

Deleting objects from collection Deleting a single user with id user_id User.remove({_id: user_id}, function (err) { } ); Deleting all the User objects User.remove({}, function (err) { } );