|
| 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