+ All Categories
Home > Documents > The Softer Side Of Schemas

The Softer Side Of Schemas

Date post: 15-Nov-2014
Category:
Upload: best-tech-videos
View: 112 times
Download: 1 times
Share this document with a friend
Description:
Google App Engine is a powerful cloud-computing platform designed to help developers more easily create and manage scalable web applications. With the recent addition of Java support, developers can now interact with the Google App Engine datastore using JDO and JPA, both standard Java persistence APIs. In this talk we will discuss how these standards allow you to create a "soft schema" on top of a schema-less datastore, and the advantages this soft schema provides. We will also explore how the datastore's "pre-sharded" transactional model is exposed in these standard interfaces. Finally, we will discuss how a soft schema maps to a traditional, relational schema, and examine some techniques for porting persistence code on and off App Engine.Watch a video at http://www.bestechvideos.com/2009/06/29/google-i-o-2009-mapping-java-persistence-standards-to-the-google-app-engine-datastore
Popular Tags:
41
Monday, June 1, 2009
Transcript
Page 1: The Softer Side Of Schemas

Monday, June 1, 2009

Page 2: The Softer Side Of Schemas

Monday, June 1, 2009

Page 3: The Softer Side Of Schemas

The Softer Side Of SchemasMax RossMay 28, 2009

Monday, June 1, 2009

Page 4: The Softer Side Of Schemas

Overview

• The App Engine Datastore• Soft Schemas• Migrating to App Engine• Migrating from App Engine• Questions

3

Monday, June 1, 2009

Page 5: The Softer Side Of Schemas

Monday, June 1, 2009

Page 6: The Softer Side Of Schemas

The App Engine Datastore

Monday, June 1, 2009

Page 7: The Softer Side Of Schemas

5

The Datastore Is...

• Transactional• Natively Partitioned• Hierarchical• Schema-less• Based on Bigtable• Not a relational database• Not a SQL engine

Monday, June 1, 2009

Page 8: The Softer Side Of Schemas

“I don’t want an RDBMS for my application, I just want persistence.”

Luiz-Otavio Zorzella, Software Engineer and fellow GBus patron

Monday, June 1, 2009

Page 9: The Softer Side Of Schemas

7

Simplifying Storage

• Simplify development of apps• Simplify management of apps• App Engine services build on Google’s strengths• Scale always matters

– Request volume– Data volume

Monday, June 1, 2009

Page 10: The Softer Side Of Schemas

8

Datastore Storage Model

• Basic unit of storage is an Entity consisting of– Kind (table)– Key (primary key)– Entity Group (partition)– 0..N typed Properties (columns)

Kind PersonEntity Group /Person:EthelKey /Person:EthelAge Int64: 30Best Friend Key:/Person:Sally

Monday, June 1, 2009

Page 11: The Softer Side Of Schemas

9

Noteworthy Datastore Features

• Ancestor• Heterogenous property types• Multi-value properties• Variable properties

Kind PersonEntity Group /Person:EthelKey /Person:Ethel/Person:JaneAge Double: 8.5Best Friend Key:/Person:Eloise Key:/Person:PattyGrade Int64: 3

Kind Person

Entity Group /Person:Ethel

Key /Person:Ethel

Age Int64: 30

Best Friend Key:/Person:Sally

Monday, June 1, 2009

Page 12: The Softer Side Of Schemas

10

Datastore Transactions

• Transactions apply to a single Entity Group– Watch out for contention!– Global transactions are feasible

• get(), put(), delete() are transactional• Queries cannot participate in transactions (yet)

/Person:Ethel/Person:Jane

/Person:Ethel

/Person:Max

Transaction

Monday, June 1, 2009

Page 13: The Softer Side Of Schemas

Monday, June 1, 2009

Page 14: The Softer Side Of Schemas

Soft Schemas

Monday, June 1, 2009

Page 15: The Softer Side Of Schemas

“A soft schema is a schema whose constraints are enforced purely in the application layer.”

Monday, June 1, 2009

Page 16: The Softer Side Of Schemas

13

Soft Schema Pluses

• Simpler development process– Rapid typesafe prototyping

• One less language in your SDLC– Online evolution works!

Monday, June 1, 2009

Page 17: The Softer Side Of Schemas

14

Implementing A Soft Schema On The Datastore

• JDO or JPA meta-data defines the soft schema• Established apis• Existing tooling• Easier porting• Specs are (mostly) mappable to datastore features• Datastore features are (mostly) mappable to specs

Monday, June 1, 2009

Page 18: The Softer Side Of Schemas

15

Filtering By Ancestor

• Expose ‘parent’ on your model object• Filter on it (equality only)• Decent substitute for a composite pk

@Entitypublic class Address { // ... @Extension(vendorName = ”datanucleus”, key = ”gae.parent-pk”) private Key personKey;}

select from com.example.Address where personKey = :personKey

Monday, June 1, 2009

Page 19: The Softer Side Of Schemas

16

Filtering By Multi-value Properties

@PersistenceCapablepublic class Person { // ... @Persistent private List<String> hobbies;}

select from com.example.Person where hobbies.contains(“yoga”)

Monday, June 1, 2009

Page 20: The Softer Side Of Schemas

17

Transactions

• API is a good fit

• Implementation is tougher

• Global vs Entity Group transactions– Similar to sharding

• Two phase commit

Monday, June 1, 2009

Page 21: The Softer Side Of Schemas

18

Relationship Management

• JDO and JPA are not just about object relationships– Transparent persistence– Object view of your data– Centralized mapping– Big maintainability win

• Letting a framework manage relationships can simplify code– True for RDBMS– Especially true for App Engine Datastore

Monday, June 1, 2009

Page 22: The Softer Side Of Schemas

19

Transparent Entity Group Management

• Entity Group layout is important– Write throughput– Atomicity of updates

• Object relationships can be described as “owned” or “unowned”

• We let ownership imply co-location within an Entity Group

Monday, June 1, 2009

Page 23: The Softer Side Of Schemas

20

Owned One To Many (Today)

@Entityclass Person { // ... @OneToMany List<Pet> petList;}

Kind PersonEntity Group /Person:13Key /Person:13

Kind PetEntity Group /Person:13Key /Person:13/Pet:18

Monday, June 1, 2009

Page 24: The Softer Side Of Schemas

21

Owned One To Many (Future)

@Entityclass Person { // ... @OneToMany List<Pet> petList;}

Kind PersonEntity Group /Person:13Key /Person:13Pets /Person:13/Pet:18

Kind PetEntity Group /Person:13Key /Person:13/Pet:18

Monday, June 1, 2009

Page 25: The Softer Side Of Schemas

22

Future JDO/JPA Work

• Provide more control over physical layout– Requires getNextId() to avoid multiple updates to same entity

• Create parent to get parent key• Create child with parent key to get child key• Update parent with child key

• Support unowned relationships– Tricky transaction issues here

• Loosen our query restrictions– Parity with Python

Monday, June 1, 2009

Page 26: The Softer Side Of Schemas

Monday, June 1, 2009

Page 27: The Softer Side Of Schemas

Migrating To App Engine

Monday, June 1, 2009

Page 28: The Softer Side Of Schemas

24

Bringing Existing Code To The App Engine Party

• The Datastore is not a drop-in replacement for an RDBMS

• Analyze your use of– Primary keys– Transactions– Queries– Views– Triggers

• Don’t forget about data migration!

Monday, June 1, 2009

Page 29: The Softer Side Of Schemas

25

Porting On: Primary Keys

• Single-column numeric and string primary keys fit nicely• Composite keys can map to an ancestor chain

• Mapping tables can be represented using multi-value properties

PET_ID (pk) PERSON_ID (pk)(fk)8 44

PET

P_ID1 (pk)(fk) P_ID2 (pk)(fk)8 328 34

FRIENDSHIP

Key /Person:8Friends /Person:32 /Person:34

Key /Person:44/Pet:8

Monday, June 1, 2009

Page 30: The Softer Side Of Schemas

26

Porting On: Transactions

• Identify “roots” in your data model– User is often a good choice for online services

• Identify operations that transact on multiple roots

• Analyze the impact of partial success and then either– refactor– disable the transaction– disable the transaction and write compensating logic

Monday, June 1, 2009

Page 31: The Softer Side Of Schemas

27

Porting On: Queries

• Shift processing from reads to writes • Identify joins

– Denormalize or rewrite as multiple queries

• Identify unsupported filter operations (distinct, toUpper)– Rewrite as multiple queries– Filter in-memory

select * from PERSON p, ADDRESS a where a.person_id = p.id and p.age > 25 and a.country = “US”

select from com.example.Person where age > 25 and country = “US”

Monday, June 1, 2009

Page 32: The Softer Side Of Schemas

Monday, June 1, 2009

Page 33: The Softer Side Of Schemas

Migrating From App Engine

Monday, June 1, 2009

Page 34: The Softer Side Of Schemas

29

Taking Your Code To Someone Else’s Party

• App Engine persistence code is generally more restrictive– Queries– Transactions– Multiple updates

• Decide what portability means and how important it is– To Key or not to Key?– Multi-value properties

• Congratulations, you’ve already sharded your data model!

Monday, June 1, 2009

Page 35: The Softer Side Of Schemas

30

Portable Root Object

@Entityclass Book { @Id String id; String title; // ...}

Kind BookEntity Group /Book:2Key /Book:2Title Vineland

ID (pk) TITLE2 Vineland

BOOK

Monday, June 1, 2009

Page 36: The Softer Side Of Schemas

31

Portable Child Object

@Entityclass Chapter { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension(vendorName = "datanucleus", key = “gae.encoded-pk”) String id;

@Extension(vendorName = “datanucleus”, key = “gae.parent-pk”) Long bookId;

String pages; // ...}

Kind ChapterEntity Group /Book:2Key /Book:2/Chapter:8Pages 23

ID (pk) BOOK_ID (pk)(fk) PAGES8 2 23

CHAPTER

Monday, June 1, 2009

Page 37: The Softer Side Of Schemas

32

Key Takeaways

• The App Engine Datastore simplifies persistence

• You can use JDO/JPA to implement a soft schema

• Denormalization is not a dirty word

• Plan for portability

Monday, June 1, 2009

Page 38: The Softer Side Of Schemas

Monday, June 1, 2009

Page 39: The Softer Side Of Schemas

Questions

Monday, June 1, 2009

Page 40: The Softer Side Of Schemas

34

For More Information

• http://code.google.com/appengine• http://code.google.com/p/datanucleus-appengine• http://groups.google.com/group/google-appengine-java

• App Engine Chat Time– irc.freenode.net#appengine– First and third Wednesday of each month

[email protected]

• To give feedback on this talk: http://haveasec.com/io

Monday, June 1, 2009

Page 41: The Softer Side Of Schemas

Monday, June 1, 2009


Recommended