diff --git a/mlengine/online-prediction/README.md b/mlengine/online-prediction/README.md new file mode 100644 index 00000000000..2723b8ceb8c --- /dev/null +++ b/mlengine/online-prediction/README.md @@ -0,0 +1,18 @@ +# 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 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/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..c7e87f0c740 --- /dev/null +++ b/mlengine/online-prediction/pom.xml @@ -0,0 +1,62 @@ + + + + 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..6f70dad7009 --- /dev/null +++ b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java @@ -0,0 +1,73 @@ +/* + * 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; +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; + +/* + * 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(); + 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"; + // 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( + "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); + } +} +