Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)
-
Upload
percival-baldwin -
Category
Documents
-
view
225 -
download
1
Transcript of Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)
![Page 1: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/1.jpg)
Introduction to Google AppEngine
Development in Java
Philippe Beaudoin
(Track Sponsor)
![Page 2: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/2.jpg)
Overview
What is Google AppEngineHit the ground running: Your first GAE appThe datastoreTask QueuesOverview of other services
?
![Page 3: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/3.jpg)
What is Google AppEngine
Fly me to the cloud, baby!Fly me to the cloud, baby!
PaaSPaaS(Platform as a Service)(Platform as a Service)
?
?
![Page 4: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/4.jpg)
• Platform features– Not Infrastructure as a Service (i.e. Amazon AWS)– Sandboxed– NoSQL distributed datastore + memcache– Many services (task queues, image manipulation…)
• APIs for…– Java (And other JVM languages)– Python– Go (beta)
?
![Page 5: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/5.jpg)
• Drawbacks– Not all of Java is available (sandbox)– Only communicate via HTTP/HTTPS + email– Read-only file system– Must finish processing within 30 seconds• Except for new backends
• Advantages– It will scale!– Pay only for what you need– Super-easy and fast deployment
?
![Page 6: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/6.jpg)
Billing model• Free!– 500Mb storage– 5M page views/month
• Billing enabled
?
![Page 7: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/7.jpg)
Admin console
?
![Page 8: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/8.jpg)
Hit the ground running:Your first GAE app
?
![Page 9: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/9.jpg)
?
![Page 10: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/10.jpg)
?
![Page 11: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/11.jpg)
?
![Page 12: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/12.jpg)
?
![Page 13: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/13.jpg)
The datastore
• NoSQL (Roughly: a fancy hashmap)• Optimistic concurrency– No locking: try first, fail later if things go wrong
• Multitenancy• Memcache (Use it!)• Default Java API: JDO or JPA– Get cover!
• Much better: Objectify (OSS project) ?
![Page 14: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/14.jpg)
?
![Page 15: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/15.jpg)
?
![Page 16: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/16.jpg)
Transactions
• Each entity has an entity group• By default, each entity is in a group of its own• Transactions are restricted to one entity group
By default, transactions are restricted to a
single entity!
?
![Page 17: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/17.jpg)
Everything in one Entity Group?
• Bad idea!– AppEngine limits the number of requests per
second on a given entity group.– Contention: every request wants the same group
• So design your entity groups carefully– The default (each entity in its group) often works
?
![Page 18: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/18.jpg)
Entity groups design
• Decide them when designing your data model– Address and phone have coupled transactions?
Same group!
• Grouping is a bit weird: choose one entity as the parent of the other.– Not necessarily a “real” parent-child relationship– No “cascading deletes”
?
![Page 19: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/19.jpg)
The components
?
![Page 20: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/20.jpg)
Persistable Entities
• Basic Java types (String, int, etc.)• Date• Blob (binary)• Email, GeoPt, PhoneNumber, etc.• Key<AnotherEntity>• List<>, Set<>, or arrays of the above• @Embedded AnyPersistableEntity• @Serialized AnySerializableEntity
?
![Page 21: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/21.jpg)
?
![Page 22: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/22.jpg)
Accessing entitiesput(), get(), query(), delete()
?
![Page 23: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/23.jpg)
Optimization
• Get/put/delete multiple keys at once• Asynchronous fetches for concurrent queries• Fetch only keys when needed
?
![Page 24: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/24.jpg)
Using the memcache
• Simply annotate the entity with @Cached!
• Optimized for read-intensive apps– Write-through
• Caches negative results to• Caches get(), put() and delete(), not query().• Can go out-of-sync in some situations– Not for entities requiring rigorous data integrity
?
![Page 25: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/25.jpg)
Useful trick: counters
• Remember: AppEngine limits the number of requests per second on a given entity group.
• A counter many “read-increment-write”!• The trick: a sharded counter– Create many counters (not @Cached!)– Pick one randomly, increment it– At the end, collect them all and sum them up
• Why it works? AppEngine has very fast read!• Same idea: sharded lists
?
![Page 26: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/26.jpg)
Queries
• NoSQL: only allows efficient queries– No table scans– No in-memory sorts– No joins (almost)
• Queries only allowed on indexed properties• Allow “merge-join” queries:
Horn
4 legsSpotted
Cows!
?
![Page 27: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/27.jpg)
Indexes
• List of entities sorted by a given field– Allows fast dichotomic search
• By default all fields are indexed– @Indexed, @Unindexed control that– Use on fields or classes
• Manual indexes– Specify more than one field to sort• Ex: Sort by gender then by age
– Needed for some complex queries
?
![Page 28: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/28.jpg)
Complex queries
• income > 100000 and age < 30– Not allowed! Inequalities on two fields
• gender = f and age > 18 and age < 25– Allowed! Two inequalities on the same field– Needs a manual index (equality + inequality)• Sort by gender, then by age
• all, sorted by decreasing age– Allowed, needs a manual index (reverse sort)
?
![Page 29: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/29.jpg)
Optimization
• Index only the fields you need• Index only the values you need!– @Unindexed(IfFalse.class) boolean admin;
• Try to limit manual indexes• Avoid manual indexes on many List<>
fields!• Exploding indexes!
?
![Page 30: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/30.jpg)
Useful trick: full text search
• Full text search not available out-of-the-box• Build a KeywordIndex table– KeywordIndex entity is a [keyword, entityId] pair– Normalize keywords (uppercase, no diacritics…)
• When searching for a prefix:– ofy().query(Customer.class).filter("keyword >=",
prefix).filter("keyword <=", prefix + "\ufffd").list();
?
![Page 31: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/31.jpg)
Task queues
• Work outside of a user request– Still need to be initiated by a user request
• Organize works in small discrete tasks– Again: split it up to make it scalable
• 10 minutes deadline (instead of 30s)• Examples:– email notifications– schema migration
?
![Page 32: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/32.jpg)
Push queue vs Pull queue
• Push queue– Meant to be consumed by your AppEngine app– Dispatching managed by AppEngine
• Pull queue new!
– Meant to be consumed externally (REST)• or on an AppEngine backend
– Consume the tasks when you’re ready– Your task consumer must scale!
?
![Page 33: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/33.jpg)
Configuring a Pull QueueIn queue.xml
<bucket-size>
<queue>
<max-concurrent-requests>
<rate>
?
![Page 34: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/34.jpg)
Enqueing a task
Or: getQueue(name)
?
![Page 35: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/35.jpg)
Mapper API
• The “map” part of “mapreduce”• Run a task on all the entities of a given type in
the datastore• Just configure the MapReduceServlet
?
![Page 36: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/36.jpg)
Configure a new mapper
?
![Page 37: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/37.jpg)
The mapper class
?
![Page 38: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/38.jpg)
Launching the mapper
?
![Page 39: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/39.jpg)
Overview of other services
• URL Fetch• Mail / XMPP (Google Talk)• OAuth, OpenID, Google Accounts• Image manipulation• Channel API• Blobstore
?
![Page 40: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/40.jpg)
URL Fetch
• Access other web resources (REST Apis, etc.)– Only http/https– Maximum deadline of 10 seconds– Synchronous or asynchronous
• Use standard java.net.URLConnection• Low-level AppEngine API has more features
?
![Page 41: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/41.jpg)
Channel API
• Enables COMET on AppEngine– Only for javascript clients
?
![Page 42: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/42.jpg)
Channel API
?
![Page 43: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/43.jpg)
Blobstore
• Objects up to 2 Gb• Useful for videos or large images• User use a form to upload to the blobstore• Can serve back the blob later
?
![Page 44: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/44.jpg)
Conclusion
• Platform-as-a-Service– Learn the platform– Constraints are often opportunities– Work with it, not against
• Still in active development– No https on custom domain
• A great way to learn what is needed to build a scalable app.– Try your hand at the free version!
![Page 45: Introduction to Google AppEngine Development in Java Philippe Beaudoin (Track Sponsor)](https://reader038.fdocuments.net/reader038/viewer/2022103005/56649db45503460f94aa4709/html5/thumbnails/45.jpg)
Hands on?
Tomorrow, 11am, CamstasiaGoogle Web Toolkit and the Model View Google Web Toolkit and the Model View
Presenter Architecture Presenter Architecture
@philbeaudoinhttp://arcbees.com