Google App
Engine for Java
       Java




 shin1ogawa@
...shin1ogawa
Google, Java, Eclipse, AppEngine,
Wicket, Maven, Hudson,        ...
                       Java
•           ...
• Platform
    •
    •

•
•
• Datastore
    •
    •
•
Platform

•


    •    Disk

•
    •   AppEngine



• Google Apps       Apps
Platform

• Google


• Google
•
Platform

•
    •
                          JVM                            …

•                         Platform
    •   A...
Platform

•                          WebContainer
• static              Container
•
 • DatastoreService
 • MailService
 • ...
Platform

•



     /            , CPU
         , DISK
    , API         ,
                      ...
Platform
“                        ”
• 1
                                         1
    •                        ”release”
...
Platform
Web
• JVM           JDK1.6      Hotspot Client VM
• Jetty


    •   Jetty       JEE           App Engine


    • ...
Platform
Web                   /
• appengine-web.xml               sessions enabled=true


•                              ...
Platform

•
    • Disk
•
• API
• Java




     : http://code.google.com/intl/ja/appengine/docs/quotas.html
Platform

• Disk
    •                   3000
    • Datastore         1GB
•                 250
    • ...


              ...
Platform

•
        30
•                /           10MB
•                    2000
•        API          7000
• ...       ...
Platform
API
• Datastore                      1MB
• MemcacheService     Key
           1MB
• URLFetchService          10

...
Platform
Java
•
• Thread
• Socket
• JDK                  class
    •   white-list                class              -> htt...
1
•          : Google
•
    •   Best practices for writing scalable applications
• Google Group
    •   Google App Engine ...
2
•             1,   3               IRC                  Office Hour
    •   irc://irc.freenode.net/#appengine
    •   Go...
3




• https://sites.google.com/a/topgate.co.jp/


         : AppEngineSDK
                      Hackathon   Googler
• Google Plugins for Eclipse(SDK                               )
 •                                               Eclipse
...
• war/WEB-INF/
  • appengine-web.xml
   ...appengine

 • datastore-indexes.xml
   ...             index

 • cron.xml      ...
•
• SDK
                                     local_db.bin(
                )    SDK
    •                 local_db.bin    ...
•


•                     java.util.logging


    •   war/WEB-INF/appengine-web.xml   logging.properties



• RemoteApiSer...
appengine-web.xml
•              on/off
• https    on/off
•                       static


•


• logging.properties
•     ...
Datastore

• Kind...RDB
• Entity...RDB
• Property...RDB
• Index...RDB
• Key...RDB          Entity
• Filter...SQL     Where...
Datastore

• BigTable
 • ...

• RDB
 •    (     =         key                   )
     • [0]id=1, name=”Hoge”
     • [1]id...
Datastore

•
    •         Entity     80ms/1
    • RDB     Index
•               Entity


•
    • Query
                  ...
Datastore

•
    •         Entity     80ms/1
    • RDB     Index
•               Entity


•
    • Query
                  ...
Datastore
Entity Group
•                               Entity
    • RDB               Relation
    • Java
    •       )
  ...
Datastore
Key
• Entity                          Property
 • Long    (id)    String    (name)   Key    Id


• EntityGroup  ...
Datastore
Property
• Entity
• java.lang
 • String          500
• Text
 • 500            String
• Blob
 •
• List Property
 ...
Datastore
Index
• Property
• Kind Index
 • Kind Key                        Index
• Single Property Index
 •        Propert...
Datastore
Transaction
•                    ACID
    •
        • JDO
        •
•               Transaction   EntityGroup
  ...
Datastore
Query
•                   ’<‘, ‘<=’, ‘==’, ‘>=’, ‘>’
• Like                      (startsWith())
•             An...
Datastore
Query
• Index
                                 → Index


 • Kind Index, Single Property Index
            Datast...
Datastore
NG
•
    •
        ID
    •                            1,2,3,1001,1002...

•
    •                  1000
       ...
Datastore

•
    •
    •
•                             Kind
•           EntityGroup       (
    Transaction           )
  ...
Datastore
                     /
                                         =


•
    • ID,          ShardingNo
    •       ...
Datastore
                            /
                        1         2      ...


• offset/limit
•                   ...
Datastore

• JDO, JPA, Low-level API      3
 •           JPA
 • JDO                                  ORM


     •         ...
Datastore
JDO/Entity
@PersistenceCapable(identityType = IdentityType.APPLICATION
  , detachable = "true")
@Version(strateg...
Datastore
JDO/Owned Relationship
public class Parent {
  ...
  @Persistent(defaultFetchGroup = “true”)
  List<Child> child...
Datastore
JDO/Owned Relationship(Key                             )
public class Parent {
  ...
}

public class Child {
  ....
Datastore
JDO/Owned Relationship(                     )
public class Parent {
  ...
  @Persistent(defaultFetchGroup = “tru...
Datastore
JDO/Unowned Relationship
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Entity {
  @P...
Datastore
JDO/
PersistentManager pm = ... // PersistentManager        ;
Parent parent = new Parent();
parent.set......
Lis...
Datastore
JDO/
PersistentManager pm = ... // PersistentManager             ;
Query query = pm.newQuery(Entity.class);
quer...
Datastore
JDO/
PersistentManager pm = ... // PersistentManager             ;
Query query = pm.newQuery(Entity.class);
quer...
Datastore
JDO/
•
    • transient, hollow, persistent/detached
    • JDOHelper#getObjectState()
• PersistenceManager
    In...
Datastore
Low-level API/
Entity entity = new Entity(“Kind”); // Low-level API   Entity
entity.setProperty(“name”, “hoge”);...
Datastore
Low-level API/
Query query = new Query(“Entity”);
query
  .addFilter(“name”, FilterOperator.GREATER_THAN_OR_EQUA...
Datastore
Low-level API/
Query query = new Query(“Entity”);
query
  .addFilter(“name”, FilterOperator.GREATER_THAN_OR_EQUA...
MemcacheService
•
    •           (                  )
    •

• cache
•            JCache(javax.cache), Low-level API     ...
URLFetchService
•         URL
•                API   java.net.URLConnection   Low-level
    API   2
    •         commons-...
cron
•
    •                URL                     Filter/Servlet
•
•
    <?xml version="1.0" encoding="UTF-8"?>
    <cro...
• MailService
 •          JavaMail(javax.mail), Low-level API   2
 •

• ImageService
 •   low-level API
 •              .....
ApiProxy
• com.google.apphosting.api.ApiProxy
 •                                       Proxy
• com.google.apphosting.api.A...
Delegate#makeSyncCall()
• public byte[] makeSyncCall(
  Environment environment,
  String packageName, // service
  String...
UI


•        UI                             …
                                    …


•


        Google App Engine   ”  ...
shin1ogawa@
Upcoming SlideShare
Loading in …5
×

初心者向けGAE/Java説明資料

9,650 views

Published on

課金の部分をもう少しうまくまとめたい。

Published in: Technology
1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total views
9,650
On SlideShare
0
From Embeds
0
Number of Embeds
399
Actions
Shares
0
Downloads
124
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

初心者向けGAE/Java説明資料

  1. 1. Google App Engine for Java Java shin1ogawa@
  2. 2. ...shin1ogawa Google, Java, Eclipse, AppEngine, Wicket, Maven, Hudson, ... Java • OSS • [SF.jp]Jiemamy... • [SF.jp]gae-j-samples...GAE/J GAE/J/Maven • [SF.jp]asclipse...Amateras AIR GEAR AS3 • java-ja, wicket-ja, BOTTUKU... (http://www.topgate.co.jp) GoogleAppEngine GoogleApps
  3. 3. • Platform • • • • • Datastore • • •
  4. 4. Platform • • Disk • • AppEngine • Google Apps Apps
  5. 5. Platform • Google • Google •
  6. 6. Platform • • JVM … • Platform • AWS-EC2 ”Amazon Cloud Watch( )” ( ) ”Auto Scaling” ”Elastic Load Balancing” • Relational Database • KVS SimpleDB( )
  7. 7. Platform • WebContainer • static Container • • DatastoreService • MailService • URLFetchService • ImageService... • cron
  8. 8. Platform • / , CPU , DISK , API , ...
  9. 9. Platform “ ” • 1 1 • ”release” ”snapshot” … • http://myappid.appspot.com/ ... • http://snapthot.latest.myappid.appspot.com/ ... • • datastore • python java
  10. 10. Platform Web • JVM JDK1.6 Hotspot Client VM • Jetty • Jetty JEE App Engine • Jetty Comet (30 stream ) • = JVM • static • https off
  11. 11. Platform Web / • appengine-web.xml sessions enabled=true • Datastore • _ah_session Kind • Application SessionCleanupServlet • Servlet web.xml
  12. 12. Platform • • Disk • • API • Java : http://code.google.com/intl/ja/appengine/docs/quotas.html
  13. 13. Platform • Disk • 3000 • Datastore 1GB • 250 • ... appengine
  14. 14. Platform • 30 • / 10MB • 2000 • API 7000 • ... , /
  15. 15. Platform API • Datastore 1MB • MemcacheService Key 1MB • URLFetchService 10 • Datasotre 1000 • JDO 1000 ... • ...
  16. 16. Platform Java • • Thread • Socket • JDK class • white-list class -> http:// code.google.com/intl/ja/appengine/docs/java/jrewhitelist.html • GC • ...
  17. 17. 1 • : Google • • Best practices for writing scalable applications • Google Group • Google App Engine for Java • Google App Engine • Google-App-Engine-Japan • Google Code • googleappengine • datanucleus-appengine
  18. 18. 2 • 1, 3 IRC Office Hour • irc://irc.freenode.net/#appengine • Google Calendar: delevepoer-calendar@google.com • Google App Engine Blog • AppEngine Cookbook • Under the covers of the Google App Engine Datastore • tips • shin1ogawa • ( maven plugin ) • Google App Engine
  19. 19. 3 • https://sites.google.com/a/topgate.co.jp/ : AppEngineSDK Hackathon Googler
  20. 20. • Google Plugins for Eclipse(SDK ) • Eclipse • AppEngine • Ant(SDK ) • dev_appserver • app_cfg AppEngine • Python SDK(JavaSDK ) • index vacuum(Index Error ) • bulkloader(Datastore Export/Import) • NetBeans IntelliJ IDEA plugin …
  21. 21. • war/WEB-INF/ • appengine-web.xml ...appengine • datastore-indexes.xml ... index • cron.xml ...cron • appegnine-generated/ • datastore-indexes-auto.xml ... index • local_db.bin ...Datastore
  22. 22. • • SDK local_db.bin( ) SDK • local_db.bin AppID, versionId local_db.bin k • Maven • mvnsearch appengine • shin1ogawa Maven SDK plugin
  23. 23. • • java.util.logging • war/WEB-INF/appengine-web.xml logging.properties • RemoteApiServlet SDK
  24. 24. appengine-web.xml • on/off • https on/off • static • • logging.properties • web.xml
  25. 25. Datastore • Kind...RDB • Entity...RDB • Property...RDB • Index...RDB • Key...RDB Entity • Filter...SQL Where • equality filter... ”=” Filter • inequality filter...”>”, “>=”, “<=”, “<“ • Entity Group... Entity
  26. 26. Datastore • BigTable • ... • RDB • ( = key ) • [0]id=1, name=”Hoge” • [1]id=2, height=170.5, weight=65.5 • [2]id=3, tag=[“Apple”, “Java”] •
  27. 27. Datastore • • Entity 80ms/1 • RDB Index • Entity • • Query ( ) •
  28. 28. Datastore • • Entity 80ms/1 • RDB Index • Entity • • Query ( ) •
  29. 29. Datastore Entity Group • Entity • RDB Relation • Java • ) • Kind:Entity:id=1 • Kind:Entity:id=2 • Kind:Entity:id=3 • Kind:Entity:id=4 • Kind:Entity:id=5
  30. 30. Datastore Key • Entity Property • Long (id) String (name) Key Id • EntityGroup Entity Key Entity • Entity: Kind=Parent, id=1 • Entity: Kind=Child, name=“hoge@fuga.com” • Entity Key • Parent(1)/Child(“hoge@fuga.com”)
  31. 31. Datastore Property • Entity • java.lang • String 500 • Text • 500 String • Blob • • List Property • List Set
  32. 32. Datastore Index • Property • Kind Index • Kind Key Index • Single Property Index • Property Index • Composite Index • Property Index • Text Blob Index (index explosion) Composite Property Index
  33. 33. Datastore Transaction • ACID • • JDO • • Transaction EntityGroup Entity •
  34. 34. Datastore Query • ’<‘, ‘<=’, ‘==’, ‘>=’, ‘>’ • Like (startsWith()) • And • Java ‘IN’, ‘!=’ • Property inequality filter Property • max() min() • Join
  35. 35. Datastore Query • Index → Index • Kind Index, Single Property Index Datastore • Composite Index datastore-indexes.xml
  36. 36. Datastore NG • • ID • 1,2,3,1001,1002... • • 1000 • JDO 1000 Google tips ” ”
  37. 37. Datastore • • • • Kind • EntityGroup ( Transaction ) • Transaction •
  38. 38. Datastore / = • • ID, ShardingNo • “ID, ShardingNo)” •
  39. 39. Datastore / 1 2 ... • offset/limit • +1 Filter • Filter k • • property property
  40. 40. Datastore • JDO, JPA, Low-level API 3 • JPA • JDO ORM • Datastore • Low-level API • JDO Low-level API JEE ... Low-level API
  41. 41. Datastore JDO/Entity @PersistenceCapable(identityType = IdentityType.APPLICATION , detachable = "true") @Version(strategy = VersionStrategy.VERSION_NUMBER) public class Entity { @Persistent @PrimaryKey(valueStrategy = IdGeneratorStrategy.IDENTITY) Key id; @Persistent String value; @Persistent(defaultFetchGroup = “true”) Text text; } Fetch Fetch @Version JDO (OPT_VERSION property )
  42. 42. Datastore JDO/Owned Relationship public class Parent { ... @Persistent(defaultFetchGroup = “true”) List<Child> children; } public class Child { @Persistent @PrimaryKey(valueStrategy = IdGeneratorStrategy.IDENTITY) Key id; ... } Child Key ( encoded-pk String )
  43. 43. Datastore JDO/Owned Relationship(Key ) public class Parent { ... } public class Child { ... @Persistent @Extension(vendorName="datanucleus", key="gae.parent-pk", value="true") Key parentKey; ... } Entity Child parentKey Entity Key EntityGroup Ancestor Key
  44. 44. Datastore JDO/Owned Relationship( ) public class Parent { ... @Persistent(defaultFetchGroup = “true”) List<Child> children; } public class Child { ... @Persistent(mappedBy = “children”) Parent parent; ... } mappedBy property
  45. 45. Datastore JDO/Unowned Relationship @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Entity { @Persistent @PrimaryKey(valueStrategy = IdGeneratorStrategy.IDENTITY) Key id; @Persistent String value; @Persistent Sey<Key> otherEntityRefs; }
  46. 46. Datastore JDO/ PersistentManager pm = ... // PersistentManager ; Parent parent = new Parent(); parent.set...... List<Child> children = new ArrayList<Child>(); children.add(child1); parent.setChildren(children); try { Transaction transaction = pm.currentTransaction(); transaction.begin(); pm.makePersistent(parent); transaction.commit(); } finally { if (transaction.isActive()) transaction.rollback(); }
  47. 47. Datastore JDO/ PersistentManager pm = ... // PersistentManager ; Query query = pm.newQuery(Entity.class); query.setFilter(“name == param”); query.declareParameters(“java.lang.String param”); query.setOrdering(“name asc, key desc”); @SuppressWarnings("unchecked") List<Entity> list = (List<Entity>) query.execute(“hoge”); ... Key key = KeyFactory.createKey(“Entity”, “keyName”); Entity entity = pm.getObjectById(Entity.class, key); Key key = entity.getOtherEntityKey(); OtherEntity otherEntity = pm.getObjectById(OtherEntity.class, key);
  48. 48. Datastore JDO/ PersistentManager pm = ... // PersistentManager ; Query query = pm.newQuery(Entity.class); query.setFilter(“name == param”); query.declareParameters(“java.lang.String param”); query.setResult(“count(this)”); @SuppressWarnings("unchecked") Integer count = (Integer) query.execute(“hoge”); ... query.setResult(“key”); List<Entity> list = (List<Entity>) query.execute(“hoge”); ”count(this)” ”key” Key
  49. 49. Datastore JDO/ • • transient, hollow, persistent/detached • JDOHelper#getObjectState() • PersistenceManager InstanceLifecycleListener • PersistenceManager#execute() List serialize • PersistenceManager#retrieve[All]() Property fetch
  50. 50. Datastore Low-level API/ Entity entity = new Entity(“Kind”); // Low-level API Entity entity.setProperty(“name”, “hoge”); entity.setProperty(“height”, 170.5); DatastoreService service = DatastoreServiceFactory.getDatastoreService(); service.put(entity); Entity[] entities = .... service.put(entities); Entity
  51. 51. Datastore Low-level API/ Query query = new Query(“Entity”); query .addFilter(“name”, FilterOperator.GREATER_THAN_OR_EQUAL, “hoge”) .addSort("name", SortDirection.ASCENDING) .addSort("__key__", SortDirection.ASCENDING); DatastoreService service = DatastoreServiceFactory.getDatastoreService(); List<Entity> entities = service.prepare(query).asList( FetchOptions.Builder.withOffset(0)); Key key = KeyFactory.createKey(“Entity”, “keyName”); Entity entity = service.get(key); property ”__key__”
  52. 52. Datastore Low-level API/ Query query = new Query(“Entity”); query .addFilter(“name”, FilterOperator.GREATER_THAN_OR_EQUAL, “hoge”) .addSort("name", SortDirection.ASCENDING) .addSort("__key__", SortDirection.ASCENDING).setKeysOnly(); DatastoreService service = DatastoreServiceFactory.getDatastoreService(); int count = service.prepare(query).countEntities(); Entity entity = service. prepare(query).asSingleResult(); setKeysOnly() asSingleResult() 0 null 1 Entity 2 Exception
  53. 53. MemcacheService • • ( ) • • cache • JCache(javax.cache), Low-level API 2 MemcacheService service = MemcacheServiceFactory.getMemcacheService(); service.put("key", list); Object cachedObject = service.get("key"); service.delete("key");
  54. 54. URLFetchService • URL • API java.net.URLConnection Low-level API 2 • commons-httpclient URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(new HTTPRequest(new URL(“”))); if (response.getResponseCode() != 200) throw new RuntimeException(); byte[] bytes = response.getContent();
  55. 55. cron • • URL Filter/Servlet • • <?xml version="1.0" encoding="UTF-8"?> <cronentries> <cron> <url>/cron/fetch?name=jiemamy-sf</url> <description>fetch the jiemamy SF.jp timeline.</description> <schedule>every 1 minutes</schedule> </cron> </cronentries>
  56. 56. • MailService • JavaMail(javax.mail), Low-level API 2 • • ImageService • low-level API • ... • • UserService • Google Account • Google appengine-java ML
  57. 57. ApiProxy • com.google.apphosting.api.ApiProxy • Proxy • com.google.apphosting.api.ApiProxy.Delegate • • com.google.appengine.tools.development.ApiProxyL ocal • Delegate ApiProxyLocal
  58. 58. Delegate#makeSyncCall() • public byte[] makeSyncCall( Environment environment, String packageName, // service String methodName, // service byte[] requestBytes) throws ApiProxyException byte[] ProtocolBuffer byte SDK RemoteApiServlet http makeSyncCall() Servlet [ | YO]
  59. 59. UI • UI … … • Google App Engine ” ” ” ”
  60. 60. shin1ogawa@

×