From 11f70b0752273981b6612a8bfbdb459bb6bd1b07 Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Tue, 26 Sep 2017 21:06:36 +0000 Subject: [PATCH 1/6] Java sample for online prediction in CMLE. --- mlengine/online-prediction/README.md | 7 +++ mlengine/online-prediction/input.txt | 1 + mlengine/online-prediction/pom.xml | 49 +++++++++++++++++ .../src/main/java/OnlinePredictionSample.java | 55 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 mlengine/online-prediction/README.md create mode 100644 mlengine/online-prediction/input.txt create mode 100644 mlengine/online-prediction/pom.xml create mode 100644 mlengine/online-prediction/src/main/java/OnlinePredictionSample.java diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md new file mode 100644 index 00000000000..aae2dc3bcfc --- /dev/null +++ b/mlengine/online-prediction/README.md @@ -0,0 +1,7 @@ +# Cloud Machine Learning Engine - Online Prediction with Java + +Compile and run the sample: + 1. Compile the sample code using Maven by running the following command: +
mvn compile
+ 1. Execute the sample code using Maven by running the following command: +
mvn -q exec:java
diff --git a/mlengine/online-prediction/input.txt b/mlengine/online-prediction/input.txt new file mode 100644 index 00000000000..19eb80fdd15 --- /dev/null +++ b/mlengine/online-prediction/input.txt @@ -0,0 +1 @@ +{"instances": ["YOUR_INPUT_INSTANCE1", "YOUR_INPUT_INSTANCE2"]} diff --git a/mlengine/online-prediction/pom.xml b/mlengine/online-prediction/pom.xml new file mode 100644 index 00000000000..816cdae1bfa --- /dev/null +++ b/mlengine/online-prediction/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + com.google.cloud.samples + mlengine-online-prediction + 1 + + + + + org.codehaus.mojo + exec-maven-plugin + 1.4.0 + + + + java + + + + + OnlinePredictionSample + + + java.util.logging.config.file + logging.properties + + + + + + + + + joda-time + joda-time + 2.2 + + + com.google.api-client + google-api-client + 1.22.0 + + + com.google.apis + google-api-services-discovery + v1-rev58-1.22.0 + + + diff --git a/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java new file mode 100644 index 00000000000..e536b44960d --- /dev/null +++ b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java @@ -0,0 +1,55 @@ +/* + * Sample code for doing Cloud Machine Learning Engine online prediction in Java. + */ + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.FileContent; +import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpContent; +import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpRequestFactory; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.UriTemplate; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.discovery.Discovery; +import com.google.api.services.discovery.model.JsonSchema; +import com.google.api.services.discovery.model.RestDescription; +import com.google.api.services.discovery.model.RestMethod; +import java.io.File; + +public class OnlinePredictionSample { + public static void main(String[] args) throws Exception { + HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); + Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build(); + + RestDescription api = discovery.apis().getRest("ml", "v1").execute(); + RestMethod method = api.getResources().get("projects").getMethods().get("predict"); + + JsonSchema param = new JsonSchema(); + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String versionId = "YOUR_VERSION_ID"; + param.set( + "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId)); + + GenericUrl url = + new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true)); + System.out.println(url); + + String contentType = "application/json"; + File requestBodyFile = new File("input.txt"); + HttpContent content = new FileContent(contentType, requestBodyFile); + System.out.println(content.getLength()); + + GoogleCredential credential = GoogleCredential.getApplicationDefault(); + HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content); + + String response = request.execute().parseAsString(); + System.out.println(response); + } +} + From 41591d86df1d132b7338bbeab69258d7b9874279 Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Tue, 26 Sep 2017 21:09:04 +0000 Subject: [PATCH 2/6] update README --- mlengine/online-prediction/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md index aae2dc3bcfc..b57c0ef4c24 100644 --- a/mlengine/online-prediction/README.md +++ b/mlengine/online-prediction/README.md @@ -1,7 +1,9 @@ # Cloud Machine Learning Engine - Online Prediction with Java +Modify the OnlinePredictionSample.java with your project/model/version information. + Compile and run the sample: - 1. Compile the sample code using Maven by running the following command: + Compile the sample code using Maven by running the following command:
mvn compile
- 1. Execute the sample code using Maven by running the following command: + Execute the sample code using Maven by running the following command:
mvn -q exec:java
From e98c820ca1cfaa249976a185a82d19779ee81ddb Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Tue, 3 Oct 2017 20:20:08 +0000 Subject: [PATCH 3/6] add licence and some comments --- .../src/main/java/OnlinePredictionSample.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java index e536b44960d..6f70dad7009 100644 --- a/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java +++ b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java @@ -1,5 +1,17 @@ /* - * Sample code for doing Cloud Machine Learning Engine online prediction in Java. + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; @@ -19,6 +31,10 @@ import com.google.api.services.discovery.model.RestMethod; import java.io.File; +/* + * Sample code for doing Cloud Machine Learning Engine online prediction in Java. + */ + public class OnlinePredictionSample { public static void main(String[] args) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); @@ -30,6 +46,8 @@ public static void main(String[] args) throws Exception { JsonSchema param = new JsonSchema(); String projectId = "YOUR_PROJECT_ID"; + // You should have already deployed a model and a version. + // For reference, see https://cloud.google.com/ml-engine/docs/how-tos/deploying-models. String modelId = "YOUR_MODEL_ID"; String versionId = "YOUR_VERSION_ID"; param.set( From 2b5c7e4e7904c67363909bac56750c88139ff1d5 Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Wed, 4 Oct 2017 21:25:24 +0000 Subject: [PATCH 4/6] Update README, add license to pom.xml --- mlengine/online-prediction/README.md | 10 ++++++++++ mlengine/online-prediction/pom.xml | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md index b57c0ef4c24..01429899571 100644 --- a/mlengine/online-prediction/README.md +++ b/mlengine/online-prediction/README.md @@ -1,5 +1,15 @@ # Cloud Machine Learning Engine - Online Prediction with Java +## Setup +This sample demonstrates how to send online prediction requests to your deployed +model on CMLE. +Follow the [tutorial]{https://cloud.google.com/ml-engine/docs/how-tos/deploying-models} +to deploy your model first. + +This sample is using the [Application Default Credential]{https://developers.google.com/identity/protocols/application-default-credentials}. You can install the Google Cloud SDK and run: +
gcloud auth application-default login
+ +## Run Modify the OnlinePredictionSample.java with your project/model/version information. Compile and run the sample: diff --git a/mlengine/online-prediction/pom.xml b/mlengine/online-prediction/pom.xml index 816cdae1bfa..3d913d857b4 100644 --- a/mlengine/online-prediction/pom.xml +++ b/mlengine/online-prediction/pom.xml @@ -1,3 +1,16 @@ + + 4.0.0 com.google.cloud.samples From fd619ff5d85f2b40498e502e741a0cb31abe3df7 Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Wed, 4 Oct 2017 21:26:55 +0000 Subject: [PATCH 5/6] fix README --- mlengine/online-prediction/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md index 01429899571..e4a52d8adaa 100644 --- a/mlengine/online-prediction/README.md +++ b/mlengine/online-prediction/README.md @@ -3,10 +3,10 @@ ## Setup This sample demonstrates how to send online prediction requests to your deployed model on CMLE. -Follow the [tutorial]{https://cloud.google.com/ml-engine/docs/how-tos/deploying-models} +Follow the [tutorial](https://cloud.google.com/ml-engine/docs/how-tos/deploying-models) to deploy your model first. -This sample is using the [Application Default Credential]{https://developers.google.com/identity/protocols/application-default-credentials}. You can install the Google Cloud SDK and run: +This sample is using the [Application Default Credential](https://developers.google.com/identity/protocols/application-default-credentials). You can install the Google Cloud SDK and run:
gcloud auth application-default login
## Run From a92e09996f408bb1a520a0385380842f77439b16 Mon Sep 17 00:00:00 2001 From: Lun-Kai Hsu Date: Wed, 4 Oct 2017 21:28:43 +0000 Subject: [PATCH 6/6] small fix --- mlengine/online-prediction/README.md | 9 ++++----- mlengine/online-prediction/pom.xml | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md index e4a52d8adaa..2723b8ceb8c 100644 --- a/mlengine/online-prediction/README.md +++ b/mlengine/online-prediction/README.md @@ -12,8 +12,7 @@ This sample is using the [Application Default Credential](https://developers.goo ## Run Modify the OnlinePredictionSample.java with your project/model/version information. -Compile and run the sample: - Compile the sample code using Maven by running the following command: -
mvn compile
- Execute the sample code using Maven by running the following command: -
mvn -q exec:java
+Compile the sample code using Maven by running the following command: +
mvn compile
+Execute the sample code using Maven by running the following command: +
mvn -q exec:java
diff --git a/mlengine/online-prediction/pom.xml b/mlengine/online-prediction/pom.xml index 3d913d857b4..c7e87f0c740 100644 --- a/mlengine/online-prediction/pom.xml +++ b/mlengine/online-prediction/pom.xml @@ -1,5 +1,5 @@