GAE Developer - Day4
Simon @ MiCloud
2014Q1
Last Class
● Memcache basic
● Task Queue concept
● Push Queue
● Pull Queue
● Create a Bucket
● Create a CloudSQL
Before Today
Create a Bucket
$ gcloud auth login
$ gcloud config set project [my-project-id]
$ gsutil mb gs://[my-bucket]
$ gsutil ls g...
Sample Database
http://www.mysqltutorial.org/mysql-sample-database.aspx
Create a CloudSQL
$ gcloud sql -h
$ gcloud sql instances create [inst-name]
$ gcloud sql instances list
$ gcloud sql insta...
Create a CloudSQL
$ gcloud sql instances set-root-password test-sql 
-p [password]
$ gcloud sql instances patch test-sql 
...
Connect Cloud SQL - MySQL Workbench
http://www.mysql.com/products/workbench/
Today
● Use CloudStorage - BlobStore
● Connect CloudSQL
Using Cloud Storage
Use CloudStorage - BlobStore
Create Upload Url
BlobstoreService blobstoreService =
BlobstoreServiceFactory.getBlobstoreService();
UploadOptions uploadO...
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
BlobstoreService blobstoreServic...
Serving Data
public void doGet(HttpServletRequest req, HttpServletResponse resp){
BlobstoreService blobstoreService =
Blob...
Other Storage API
● Image API
● Cloud Storage Client Library
Connect Cloud SQL
Before Connect
● CloudSQL instance ready
● Authorized
○ root password
○ instance ip address
○ access ip address
Configure Connectors
● Hostname is Cloud SQL instance IP address
● Connection URL includes instance IP and
database name
●...
Local Test Configure
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.g...
Connect from App Engine
● JDBC
● Java Persistence API (JPA)
● Java Data Objects (JDO)
● Other JPAs custom additions: Hiber...
Detect running environment
Connection conn = null;
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Va...
DriverManager.registerDriver(new AppEngineDriver());
conn = DriverManager.getConnection(connUrl);
String sql = "SELECT cou...
Other Frameworks
Popular frameworks for programming MySQL
● Hibernate (Java)
● JOOQ (Java)
● JPA ( Java )
● Django (Python)
Google App Engine Developer - Day4
Upcoming SlideShare
Loading in …5
×

Google App Engine Developer - Day4

980 views

Published on

GAE Java - Day4
About CloudStorage and CloudSQL

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
980
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
15
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Google App Engine Developer - Day4

  1. 1. GAE Developer - Day4 Simon @ MiCloud 2014Q1
  2. 2. Last Class ● Memcache basic ● Task Queue concept ● Push Queue ● Pull Queue
  3. 3. ● Create a Bucket ● Create a CloudSQL Before Today
  4. 4. Create a Bucket $ gcloud auth login $ gcloud config set project [my-project-id] $ gsutil mb gs://[my-bucket] $ gsutil ls gs://[my-bucket]
  5. 5. Sample Database http://www.mysqltutorial.org/mysql-sample-database.aspx
  6. 6. Create a CloudSQL $ gcloud sql -h $ gcloud sql instances create [inst-name] $ gcloud sql instances list $ gcloud sql instances import instance-name --uri gs://your-bucket/sql-dump-file.gz $ gcloud sql instances patch --assign-ip [inst-name] $ gcloud sql instances get [inst-name]
  7. 7. Create a CloudSQL $ gcloud sql instances set-root-password test-sql -p [password] $ gcloud sql instances patch test-sql --authorized-networks=[your-ip-address]
  8. 8. Connect Cloud SQL - MySQL Workbench http://www.mysql.com/products/workbench/
  9. 9. Today ● Use CloudStorage - BlobStore ● Connect CloudSQL
  10. 10. Using Cloud Storage
  11. 11. Use CloudStorage - BlobStore
  12. 12. Create Upload Url BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName( "BUCKET-NAME"); //Name without “gs://” String uploadUrl = blobstoreService.createUploadUrl( "/callback-url", uploadOptions); resp.getWriter().println("<form action=" + uploadUrl + " method="post" enctype="multipart/form-data">"); resp.getWriter().println("<input type="file" name="field-name"");
  13. 13. public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); Map<String, List<FileInfo>> blobs = blobstoreService.getFileInfos(req); List<FileInfo> keys = blobs.get("field-name"); //input file name on form if (keys != null && keys.size() > 0) { FileInfo fileKey = keys.get(0); String objectUploaded = fileKey.getGsObjectName(); …. (Save the objectUploaded mapping to datastore or database) Create Callback Handler
  14. 14. Serving Data public void doGet(HttpServletRequest req, HttpServletResponse resp){ BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/<bucket>/<object>"); try { blobstoreService.serve(blobKey, resp); } catch (IOException e){ e.printStackTrace(); } ….
  15. 15. Other Storage API ● Image API ● Cloud Storage Client Library
  16. 16. Connect Cloud SQL
  17. 17. Before Connect ● CloudSQL instance ready ● Authorized ○ root password ○ instance ip address ○ access ip address
  18. 18. Configure Connectors ● Hostname is Cloud SQL instance IP address ● Connection URL includes instance IP and database name ● Java connection is: jdbc:mysql://<instance-ip>:3306/
  19. 19. Local Test Configure appengine-web.xml <?xml version="1.0" encoding="utf-8"?> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>mitac-cp300</application> <version>eord</version> …. <use-google-connector-j>true</use-google-connector-j> …. </appengine-web-app>
  20. 20. Connect from App Engine ● JDBC ● Java Persistence API (JPA) ● Java Data Objects (JDO) ● Other JPAs custom additions: Hibernate, EclipseLink jdbc:google:mysql://... (instead of jdbc:mysql://...)
  21. 21. Detect running environment Connection conn = null; if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { // app is running in production Class.forName("com.mysql.jdbc.GoogleDriver"); String url= "jdbc:google:mysql://instance-name/"; conn = DriverManager.getConnection(url); ... } else { // app is running on localhost Class.forName("com.mysql.jdbc.Driver"); String url= "jdbc:mysql://db-ip-address/"; conn = DriverManager.getConnection(url); ... } conn.~
  22. 22. DriverManager.registerDriver(new AppEngineDriver()); conn = DriverManager.getConnection(connUrl); String sql = "SELECT count(*) as total FROM " + tableName + ""; PreparedStatement stmt = conn.prepareStatement(sql); ResultSet results = stmt.executeQuery(); while (results.next()) { int total = results.getInt("total"); … } Query Example - Standard JDBC
  23. 23. Other Frameworks Popular frameworks for programming MySQL ● Hibernate (Java) ● JOOQ (Java) ● JPA ( Java ) ● Django (Python)

×