Skip to content

Commit 6637e6b

Browse files
lluunnlesv
authored andcommitted
Java sample for doing online prediction in Cloud Machine Learning Engine. (#868)
* Java sample for online prediction in CMLE. * update README * add licence and some comments * Update README, add license to pom.xml * fix README * small fix
1 parent 8548a77 commit 6637e6b

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

mlengine/online-prediction/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Cloud Machine Learning Engine - Online Prediction with Java
2+
3+
## Setup
4+
This sample demonstrates how to send online prediction requests to your deployed
5+
model on CMLE.
6+
Follow the [tutorial](https://cloud.google.com/ml-engine/docs/how-tos/deploying-models)
7+
to deploy your model first.
8+
9+
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:
10+
<pre>gcloud auth application-default login</pre>
11+
12+
## Run
13+
Modify the OnlinePredictionSample.java with your project/model/version information.
14+
15+
Compile the sample code using Maven by running the following command:
16+
<pre>mvn compile</pre>
17+
Execute the sample code using Maven by running the following command:
18+
<pre>mvn -q exec:java</pre>

mlengine/online-prediction/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"instances": ["YOUR_INPUT_INSTANCE1", "YOUR_INPUT_INSTANCE2"]}

mlengine/online-prediction/pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!--
2+
Copyright 2017 Google Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
14+
<project>
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.google.cloud.samples</groupId>
17+
<artifactId>mlengine-online-prediction</artifactId>
18+
<version>1</version>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.codehaus.mojo</groupId>
24+
<artifactId>exec-maven-plugin</artifactId>
25+
<version>1.4.0</version>
26+
<executions>
27+
<execution>
28+
<goals>
29+
<goal>java</goal>
30+
</goals>
31+
</execution>
32+
</executions>
33+
<configuration>
34+
<mainClass>OnlinePredictionSample</mainClass>
35+
<systemProperties>
36+
<systemProperty>
37+
<key>java.util.logging.config.file</key>
38+
<value>logging.properties</value>
39+
</systemProperty>
40+
</systemProperties>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
<dependencies>
46+
<dependency>
47+
<groupId>joda-time</groupId>
48+
<artifactId>joda-time</artifactId>
49+
<version>2.2</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.google.api-client</groupId>
53+
<artifactId>google-api-client</artifactId>
54+
<version>1.22.0</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.apis</groupId>
58+
<artifactId>google-api-services-discovery</artifactId>
59+
<version>v1-rev58-1.22.0</version>
60+
</dependency>
61+
</dependencies>
62+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
18+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
19+
import com.google.api.client.http.FileContent;
20+
import com.google.api.client.http.GenericUrl;
21+
import com.google.api.client.http.HttpContent;
22+
import com.google.api.client.http.HttpRequest;
23+
import com.google.api.client.http.HttpRequestFactory;
24+
import com.google.api.client.http.HttpTransport;
25+
import com.google.api.client.http.UriTemplate;
26+
import com.google.api.client.json.JsonFactory;
27+
import com.google.api.client.json.jackson2.JacksonFactory;
28+
import com.google.api.services.discovery.Discovery;
29+
import com.google.api.services.discovery.model.JsonSchema;
30+
import com.google.api.services.discovery.model.RestDescription;
31+
import com.google.api.services.discovery.model.RestMethod;
32+
import java.io.File;
33+
34+
/*
35+
* Sample code for doing Cloud Machine Learning Engine online prediction in Java.
36+
*/
37+
38+
public class OnlinePredictionSample {
39+
public static void main(String[] args) throws Exception {
40+
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
41+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
42+
Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();
43+
44+
RestDescription api = discovery.apis().getRest("ml", "v1").execute();
45+
RestMethod method = api.getResources().get("projects").getMethods().get("predict");
46+
47+
JsonSchema param = new JsonSchema();
48+
String projectId = "YOUR_PROJECT_ID";
49+
// You should have already deployed a model and a version.
50+
// For reference, see https://cloud.google.com/ml-engine/docs/how-tos/deploying-models.
51+
String modelId = "YOUR_MODEL_ID";
52+
String versionId = "YOUR_VERSION_ID";
53+
param.set(
54+
"name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));
55+
56+
GenericUrl url =
57+
new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
58+
System.out.println(url);
59+
60+
String contentType = "application/json";
61+
File requestBodyFile = new File("input.txt");
62+
HttpContent content = new FileContent(contentType, requestBodyFile);
63+
System.out.println(content.getLength());
64+
65+
GoogleCredential credential = GoogleCredential.getApplicationDefault();
66+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
67+
HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);
68+
69+
String response = request.execute().parseAsString();
70+
System.out.println(response);
71+
}
72+
}
73+

0 commit comments

Comments
 (0)