Datastore – Easy storage for AppEngine



Datastore – Easy storage for AppEngine

0 0


datastore-slides


On Github google-cssi / datastore-slides

Datastore

Easy storage for AppEngine

Created by Luis Ibanez / @luisibanez

Datastore

Native database

in AppEngine

Love

databases

databases

Everywhere!

Collection

of Records

Remember JSON?

"students":[
  {"firstName":"John", "lastName":"Doe",   "email":"jd@gmail.com"},
  {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"},
  {"firstName":"Peter","lastName":"Jones", "email":"pj@gmail.com"}
]
                    

A Field

"firstName":"John"
                    

A Record

{
  "firstName":"John",
  "lastName":"Doe",
  "email":"jd@gmail.com"
}
                    

List of Records

"students":[
  {"firstName":"John", "lastName":"Doe",   "email":"jd@gmail.com"},
  {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"},
  {"firstName":"Peter","lastName":"Jones", "email":"pj@gmail.com"}
]
                    

Several Lists of Records

"students":[
  {"firstName":"John", "lastName":"Doe",   "email":"jd@gmail.com"},
  {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"},
  ...
]
                    
"classes":[
  {"name":"math 101",    "grade":"5th", "room":"B11"}
  {"name":"zombies 201", "grade":"7th", "room":"Basement"}
  ...
  ]
                    

Exercise !

Which one of these

is a Database?

  • Phone Contacts
  • A Picture
  • Lunch Menu
  • Bus Schedule
  • Student list

Which one of these

is a Database?

  • A Cat !
  • Veterinarian Cat Records
  • A Picture
  • Vacations Pictures Album
  • A Phone call
  • Phone calls history

What fields are

in a veterinarian

cat record?

Identify the Database Elements!

Datastore

Native database

in AppEngine

What Datastore does for you:

  • Store your records
  • Retrieve your records
  • Replicate your records
  • Index your records

A Record

=

Datastore Model

Student

Model

Student Model

  • Name
  • University
  • Age

Field Types

  • Name: String
  • University: String
  • Age: Integer

Model

Field: | Name | University | Age Type: | String | String | Integer Entity 1 | "John Doe" | "UCLA" | 21 Entity 2 | "Jane Doe" | "MIT" | 22 Entity 3 | "Mary Smith" | "UNC" | 19 ... | ... | ... | ...

Exercise !

Model

For a veterinarian

cat record?

Model

  • Field names
  • Field types

Model

For student records

Tell us about Python Classes!

Python

Classes

Python Class

  • Attributes
  • Methods

Python Class

class Student:
  age = 25
  name = "John Doe"
  university = "UNC"
                    
one_student = Student()
                    
print one_student.age
print one_student.name
print one_student.university
                    

Attributes

one_student = Student()
                    
print one_student.age
                    
one_student.age = 22
                    
print one_student.age
                    

Constructor

class Student:

  def __init__(self, name, age, university):
    self.name = name
    self.age = age
    self.university = university
                    
one_student = Student(name="Mary", age=23, university="MIT")
                    
print one_student.age
print one_student.name
print one_student.university
                    

Methods

class Student:
  age = 25
  name = "John Doe"
  university = "UNC"
                    
  def printMe(self):
    print 'name: ', self.name
    print 'age: ', self.age
    print 'university: ', self.university
                    
one_student = Student(name="Leroy", age=20, university="UCLA")
one_student.printMe()
                    

Exercise !

Write a Python Class

Representing a Song

  • Title
  • Singer
  • Author
  • Genre
  • Year

Write Methods

  • __init__
  • printMe

Datastore

Client

Library

Datastore Client Library

from google.appengine.ext import ndb
                    
class Student(ndb.Model):
                    
    name = ndb.StringProperty(required=True)
                    
    university = ndb.StringProperty(required=True)
                    
    age = ndb.IntegerProperty(required=False)
                    

Creating a Student Entity

student_instance = Student(name="Jon Doe", university="MIT")
                    
student_instance.put()
                    

Exercise !

Write an AppEngine app

that will

  • Take from URL Query line
    • Student Name
    • Student University
  • Create a Student instance
  • Write the instance in Datastore

Manually

Inspecting

Datastore

Launching AppEngine App

dev_appserver.py --port 9080 --admin_port 9000 .
                    
Checking for updates to the SDK.
Starting API server at: http://localhost:59242
Starting module "default" running at: http://localhost:9080
Starting admin server at: http://localhost:9000
                    

AppEngine Admin

http://localhost:9000

AppEngine Admin Interface

AppEngine Datastore Interface

AppEngine Datastore Interface

AppEngine Datastore Interface

Datastore Consistency

dev_appserver.py . --datastore_consistency_policy=consistent
                    

Eventual Consistency...

Strong Consistency...

Trade-Offs

  • High Scalability
  • Low Latency
  • High Availability
  • High Consistency

Exercise !

Pick Trade-Off

  • Presidential Elections Results
  • Facebook Likes
  • Medical Records
  • Twitter Favorites
  • FBI Criminal Records
  • Soccer World Cup Results

Consistency Options

--datastore_consistency_policy=
consistent
                    
time
                    
random
                    

Exercise !

Write an AppEngine app

that will

  • Import ndb and define a student model
  • Make a GET handler for /add_student
    • Create a form with a single button here.
    • Make it post to /add_student.
  • Make a POST handler for /add_student
    • Create a Student entity.
    • Put it in the Datastore.
    • Write a confirmation message in the response.

Testing the App

  • Browse to /add_student
  • Add three students
  • Inspect Datastore admin
  • Stop App
  • Restart App
  • Inspect Datastore admin again

Datastore

Keys

Get with Key

key = student_instance.put()
                    
key.get()
                    
key.get().name
                    
self.response.write(key.get().name)
                    

AppEngine Datastore Interface

Record Key and Id

Key to Id

key = student_instance.put()
                    
id = key.id()
                    
student = Student.get_by_id(id)
                    

Id to Key

key = ndb.Key('Student', id)
                    
key.get()
                    
key.get().name
                    

Querying

student_query = Student.query()
                    
student_data = student_query.fetch()
                    
template_values = {
    'students' : student_data
}
                    
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
                    

Keys in URLs

Get id and navigate to that url

self.redirect('/view_student?key=%s' % key.urlsafe())
                    

Get an entity from an ID

key = ndb.Key(urlsafe=self.request.get("key"))
student = key.get()
                    

Query

https://cloud.google.com/appengine/docs/python/ndb/queryclass

Model

https://cloud.google.com/appengine/docs/python/ndb/modelclass

Datastore Easy storage for AppEngine Created by Luis Ibanez / @luisibanez