-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java sample for doing online prediction in Cloud Machine Learning Engine. #868
Changes from 3 commits
11f70b0
41591d8
468255e
4614206
e98c820
1d1a129
970cc5b
50d004b
2b5c7e4
a936efa
fd619ff
a92e099
2b8d9a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should mention that this is a part of a tutorial and provide a link. |
||
Compile the sample code using Maven by running the following command: | ||
<pre>mvn compile</pre> | ||
Execute the sample code using Maven by running the following command: | ||
<pre>mvn -q exec:java</pre> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"instances": ["YOUR_INPUT_INSTANCE1", "YOUR_INPUT_INSTANCE2"]} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<project> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License needed here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>mlengine-online-prediction</artifactId> | ||
<version>1</version> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.4.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>OnlinePredictionSample</mainClass> | ||
<systemProperties> | ||
<systemProperty> | ||
<key>java.util.logging.config.file</key> | ||
<value>logging.properties</value> | ||
</systemProperty> | ||
</systemProperties> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.api-client</groupId> | ||
<artifactId>google-api-client</artifactId> | ||
<version>1.22.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-discovery</artifactId> | ||
<version>v1-rev58-1.22.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some kind of comment how you are inspecting the discovery document and getting the Prediction API. |
||
|
||
JsonSchema param = new JsonSchema(); | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something about your Model here would be good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment about this. Why aren't you actually generating an APIary client so don't need to do it this way? Seems simple enough, but there are some named things when you create it from the proto usually. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't particular reason for choosing discovery over direct API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, I'm happy with just using discovery. As you will need to generate a One Platform API for GA. (I'd rather see a 1P now, but can live w/ this) |
||
|
||
String response = request.execute().parseAsString(); | ||
System.out.println(response); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you look at our other README's and add content about auth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, PTAL