easy orm-ness with objectify-appengine - indicthreads cloud computing conference 2011

33
1 Easy ORM-ness with Objectify-Appengine Meetu Maltiar Inphina Technologies

Upload: indicthreads

Post on 25-May-2015

1.015 views

Category:

Technology


0 download

DESCRIPTION

Session presented at the 2nd IndicThreads.com Conference on Cloud Computing held in Pune, India on 3-4 June 2011. http://CloudComputing.IndicThreads.com Abstract: There are many persistence frameworks on Google App Engine. Google has its low-level datastore API, then there are standards based frameworks like JPA and there are more frameworks like Objectify-Appengine specifically for Google App Engine. Managing persistence is still a challenge. With so many options, we face the dilemma of choosing right framework for the application we are building. Objectify-Appengine extends the convenience of NoSql by providing a Hibernate-style mapping layer between your application and GAE datastore. In this session I will cover handling persistence on Google App Engine with Objectify-Appengine. At one hand Google datastore API is low level and at the other there are standard based frameworks like JPA/JDO. Objectify-Appengine tries to hit the sweet spot between the two. Takeaways for the Audience Audience will see the capabilities of Objectify-Appengine and how can they leverage it to manage persistence on Google App Engine. The best practises to be followed when using Objectify, and demo of an application built on Objectify-Appengine. Speaker:Meetu Maltiar is a Senior Consultant at Inphina. In his six year experience he has worked as Software developer, Knowledge Manager and as a Mentor. He is passionate about technology and has experience in product development as well as services. He has experience on Google App Engine and has migrated existing applications to cloud and he has also worked on Green-field projects. He likes to invest his time in exploring upcoming languages like Scala and technology frameworks like GridGain, Esper and Hadoop and extensively blogs about them.

TRANSCRIPT

Page 1: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

1

Easy ORM-ness with Objectify-Appengine

Meetu Maltiar

Inphina Technologies

Page 2: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

2

Overall Presentation Goal

Google Datastore BasicsOptions available for managing persistenceObjectify-AppengineDemo of an application using Objectify

Page 3: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

3

Enough About MeSenior Software Engineer at InphinaTechnologies that interest me: Cloud Computing

Scala

Hadoop

Page 4: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

4

Datastore BasicsEntitiesOperationsKeysTransactions

Page 5: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

5

EntitiesAn Entity is an object’s worth of data in the

datastore

In datastore an Entity is like HashMap-like object of type Entity

Datastore is conceptually a HashMap of keys to entities, and an Entity is conceptually a HashMap of name/value pairs

Page 6: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

6

OperationsGet() an entity as a whole from datastore

Put() an entity as whole in datastore

Delete() an entity from datastore

Query() for entities matching criteria you define

Page 7: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

7

KeysIn the datastore, entities are identified by the id

(or name) and a kind, which corresponds to the type of Object you are storing.

So, to get Employee #111 from datastore, we need to call something like get_from_datastore (“Employee”, 111)

Page 8: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

8

Keys ContinuedThere is actually a third parameter required to

identify an entity and it is called parentParent places the child in the same entity group

as the parent

Parent (which can be null for the un-parented, root entity) is also required to uniquely identify an Entity.

Page 9: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

9

Keys ContinuedSo, to get Employee #111 from datastore we

need to call something equivalent to:

get_from_datastore (“Employee”, 111, null)

or,get_from_datastore (“Employee”, 111, the_parent).

Instead of passing three parameters datastore wraps it in a single Object called Key.

Page 10: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

10

TransactionsData gets stored in gigantic form of thousands of

machines

In order to perform an atomic transaction datastore requires that entities lie on same servers.

To give us more control over where our data is stored, the datastore has a concept of an entity group

To give us more control over where our data is stored, the datastore has a concept of an entity group

Page 11: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

11

Transactions ContinuedWithin a Transaction we can access data from a

single entity group

Choose entity groups carefully

Why not have everything in a single entity group?

Google restricts number of requests per second per entity group

Page 12: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

12

Executing TransactionsWhen we execute get(), put(), delete() or query()

in transaction

We must operate it on single entity group

All operations will either fail or succeed completely

If another process modifies the data before commit datastore operation will fail

Page 13: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

13

Tools

Page 14: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

14

Persistence OptionsJPA/JDOGoogle DatastorePersistence Frameworks on GAE Objectify-Appengine Twig Simple Datastore Slim3

Page 15: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

15

Google Datastore ChallengesSupports just four operationsIt persists GAE-Specific entity classes rather

than POJO’sDatastore Keys are untyped

Page 16: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

16

JPA/JDO ChallengesExtra Cruft Fetch Groups

Detaching

Owned/Unowned relationships

Leaky Abstraction Keys

Entity Groups

Indexes

Page 17: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

17

Developers Dilemma

Page 18: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

18

ObjectifyIt lets you persist, retrieve, delete and

query typed objectsAll native datastore features are supportedIt provides type safe key and query classes

Page 19: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

19

Objectify Design ConsiderationsMinimally impacts cold start time. It is light

weightNo external dependencies apart from GAE

SDK

Page 20: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

20

Working With Datastore

Entity ent = new Entity(“car”);

ent.setProperty(“color”, “red”);

ent.setProperty(“doors”, 2);

service.put(ent);

Page 21: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

21

Objectify ORMNess Objects!

Employee emp = new Employee();

emp.setFirstName(“John”);

emp.setLastName(“Smith”);

service.put(emp);

Page 22: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

22

An Objectify Entity

public class Employee {

@Id Long id;

private String firstName;

private String lastName;

}

Page 23: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

23

get() Operation

Objectify ofy = Objectifyservice.begin();

Employee emp = ofy.get(Employee.class, 123);

Map<Long, Employee> employees =

ofy.get(Employee.class, 123, 124, 125);

Page 24: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

24

put() Operation

Objectify ofy = Objectifyservice.begin();

Employee emp = new Employee(“John”, “adams”);

ofy.put(emp);

System.out.println(“Id Generated is ” + emp.getId());

List<Employee> employees = createEmployees();

ofy.put(employees);

Page 25: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

25

delete() Operation

Objectify ofy = Objectifyservice.begin();

ofy.delete(Employee.class, 123);

Employee emp = // get Employee from some where

ofy.delete(emp);

Page 26: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

26

query() Operation

Objectify ofy = Objectifyservice.begin();

List<Employee> employees =

ofy.query(Employee.class).filter(“firstName =”, “John”)

Page 27: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

27

Demo get()

put()

delete()

query()

Page 28: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

28

Objectify Best PracticesUse a DAO to register entitiesAutomatic Scanning not advised adds to initialization time

will require dependency jars apart from GAE

will require changes in web.xml

GAE spins down the instance when not in use. When it comes up the request is slow because of added initialization time. This is called cold start.

Page 29: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

29

Objectify Best Practices …Use Batch Gets Instead of QueriesUse Indexes sparinglyBy default every field of object will be indexed. It comes with

heavy computational price.

Use @Unindexed on entity level and @Indexed at fields required for query

Avoid @ParentIn JPA “owned” entity relationship provides referential integrity

checking and cascading deletes and saves. Not so here.

Page 30: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

30

Happy Developer

Page 31: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

31

ConclusionJDO/JPA learning curve is steep due to App Engine’s non-

relational nature and unique concepts such as entity groups, owned and un-owned relationships.

Google Datastore is low level. It makes no pretense of being relational but also does not allow working with objects. It just stores the objects of type com.google.appengine.api.datastore.Entity

Objectify is light weight and it preserves the simplicity and transparency of low level API and does all work converting to and from POJOS to Entity Objects.

Page 33: Easy ORM-ness with Objectify-Appengine - Indicthreads cloud computing conference 2011

33

ReferencesObjectify-Appengine

http://code.google.com/p/objectify-appengine/

Google IO 2011 Session on highly productive gwt rapid development with app-engine objectify-requestfactory and gwt-platform

Twitter mining with Objectify-Appengine http://www.ibm.com/developerworks/java/library/j-javadev2-14/?ca=drs-