Lecture 9 - NoSQL - George Mason University

Transcription

NoSQLSWE 432, Fall 2017Design and Implementation of Software for the Web

Today LaTozaWhat is NoSQL?How can you create a NoSQL app with Firebase?GMU SWE 432 Fall 20172

NoSQL non SQL, non-relational, "not only" SQL databasesEmphasizes simplicity & scalability over support for relationalqueriesImportant characteristics Schema-less: each row in dataset can have different fields(just like JSON!)Non-relational: no structure linking tables together or queriesto "join" tables (Often) weaker consistency: after a field is updated, all clientseventually see the update but may see older data in themeantimeAdvantages: greater scalability, faster, simplicity, easierintegration with codeSeveral types. We'll look only at key-value. LaTozaGMU SWE 432 Fall 20173

Key-Value sql-databases-overviewLaTozaGMU SWE 432 Fall 20174

Firebase Realtime Database Example of a NoSQL datastore Google web service se/Realtime database Data stored to remote web service Data synchronized to clients in real timeSimple API Offers library wrapping HTTP requests & responses Handles synchronization of dataCan also be used on frontend to build web apps with persistencewithout backendGMU SWE 432 Fall 20165

Setting up Firebase Detailed instructions to create project, get API key https://firebase.google.com/docs/web/setup script src .js" /script script // Initialize Firebase// TODO: Replace with your project's customized code snippetvar config {apiKey: " API KEY ",authDomain: " PROJECT ID .firebaseapp.com",databaseURL: "https:// DATABASE NAME .firebaseio.com",storageBucket: " BUCKET .appspot.com",};firebase.initializeApp(config); /script LaToza/BellGMU SWE 432 Fall 20166

Setting up Firebase Realtime Database Go to https://console.firebase.google.com/, createa new projectInstall firebase modulenpm install firebase Include Firebase in your web appconst firebase require("firebase");// Initialize Firebase// TODO: Replace with your project's customized code snippetconst config {apiKey: " API KEY ",authDomain: " PROJECT ID .firebaseapp.com",databaseURL: "https:// DATABASE NAME .firebaseio.com",storageBucket: " BUCKET ozaGMU SWE 432 Fall 20177

Get Config Object for your Project LaTozaGo to Authentication, Click "Web Setup" in theupper right cornerGMU SWE 432 Fall 20178

Permissions LaToza/BellBy default, Firebase requires authentication All unauthenticated requests will be refused Do not want anyone with your URL to steal, destroy your productiondata Will look at authentication in later lecture For development, ok to allow anonymous accessGMU SWE 432 Fall 20169

Firebase Console See data values, updated in realtime Can edit data valueshttps://console.firebase.google.comLaTozaGMU SWE 432 Fall 201710

Simple test program After successfully completing previous steps,should be able to replace config and run thisscript. Can test by viewing data on console.const firebase require("firebase");// Initialize Firebaseconst config {. [copied from your config object on Firebase]};firebase.initializeApp(config);let database firebase.database();database.ref('users/' 'Josh').set({ username: 'Josh', email: 'josh@gmail.com'});LaTozaGMU SWE 432 Fall 201711

Activity: Set up Firebase RealtimeDatabaseLaTozaGMU SWE 432 Fall 201712

Firebase data model: JSON LaToza/BellJSON format data Hierarchic tree ofkey/value pairs Can view as onebig object Or describe pathto descendent andview descendentas objectGMU SWE 432 Fall 201613

Structuring dataLaToza How do you organize lots of data? One idea: just have a hierarchic structure Level 1: most general elements Level 2: elements that belong to Level 1 elements Level n: elements that belong to Level n-1elements What are the pros and cons of this approach?GMU SWE 432 Fall 201714

Structuring data LaTozaShould be consideringwhat types of recordsclients will berequesting. Do not want to forceclient to downloaddata that do notneed.Better to think ofstructure as lists of datathat clients will retrieveCan duplicate deeplynested structures withseparate indexesGMU SWE 432 Fall 201715

Activity: Design a data modelLaToza Imagine you are designing a data model for arestaurant review site. Each user has a profile, may submit reviews(linked to the user) for a restaurant, and mayupvote, downvote, and comment on reviews(linked to the reviewer and restaurant). How would you structure this data?GMU SWE 432 Fall 201716

Storing Data: Setfunction writeUserData(userId, name, email, imageUrl) {firebase.database().ref('users/' userId).set({username: name,email: email,profile picture : imageUrl});}“On the activefirebase database”Must be initialized first (comingsoon .).“Set value”Sets the value tospecified JSON.“Get the users/[userID] node”Arbitrary nodes in the tree can beaddressed by their path.LaToza/BellGMU SWE 432 Fall 201617

Storing Data: Pushvar key firebase.database().ref().child('posts').push({ author: username, uid: uid, body: body, title: title }); What about storing collections? Use push to create key automatically All data MUST have a key so it can be uniquelyreferenced LaToza/BellArrays given index keysShould never have multiple clients synchronizing an array Local indexes could get of sync with remote keys Instead, use JSON object with number as keyGMU SWE 432 Fall 201618

Storing Data: .remove();Removes the ‘posts’ subtree. Can delete a subtree by setting value to null or bycalling remove on ref LaToza/BellIf you want to store null, first need to convert valueto something else (e.g., 0, '')GMU SWE 432 Fall 201619

Listening to data changesvar starCountRef firebase.database().ref('posts/' postId '/starCount');starCountRef.on('value', function(snapshot) {updateStarCount(postElement, snapshot.val());});“When values changes, invoke function”Specify a subtree by creating a reference to a path. Listen to one ormore events by using on(eventName, eventHandlerFunction(snapshot))LaToza/Bell Read data by listening to changes to specificsubtrees Events will be generated for initial values and thenfor each subsequent updateGMU SWE 432 Fall 201620

Data Update Events LaToza/BellTypes of events value: entire contents of a path child added child changed child removedCan listen to events on any part of subtree Could have subtrees that correspond to different collections ofdata Should always listen to lowest subtree of interest to minimizeextraneous communicationCan read data exactly one time (and not get updates) using onceGMU SWE 432 Fall 201621

Ordering data Data is by, default, ordered by key in ascendingorder e.g., numeric index keys are ordered from 0 n e.g., alphanumeric keys are ordered inalphanumeric orderCan get only first (or last) n elements e.g., get n most recent news itemsvar recentPostsRef recentPostsRef.once('value', function(snapshot) {displayPost(snapshot.val());});LaToza/BellGMU SWE 432 Fall 201622

Activity: Persist restaurant data inFirebase LaTozaAssume you have some endpoints to gathersubmitted data from users for previous scenario.Using data submitted through these endpoints,store to Firebase.Offer endpoints for retrieving this data back fromFirebase.GMU SWE 432 Fall 201723

Reading for next time LaTozaIntro to CORS: for-REST-APIs/GMU SWE 432 Fall 201724

NoSQL non SQL, non-relational, "not only" SQL databases Emphasizes simplicity & scalability over support for relational queries Important characteristics Schema-less: each row in dataset can have different fields (just like JSON!) Non-relational: no structure linking tables together or queries to "join" tables