使用以下代码示例,使用AutoML Java SDK与私有服务终端点进行通信:
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.AutoMlSettings;
import com.google.cloud.automl.v1beta1.PredictResponse;
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
import com.google.cloud.automl.v1beta1.PredictionServiceSettings;
import com.google.protobuf.ByteString;
import java.util.logging.Logger;
public class AutomlPredict {
private static final Logger LOGGER = Logger.getLogger(AutomlPredict.class.getName());
private static final String PROJECT_ID = "YOUR_PROJECT_ID";
private static final String MODEL_ID = "YOUR_MODEL_ID";
private static final String CONTENT = "YOUR_CONTENT";
private static final String REGION = "YOUR_REGION";
private static final String ENDPOINT = "YOUR_ENDPOINT";
public static void main(String[] args) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
PredictionServiceClient client;
try {
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setEndpoint(ENDPOINT + ":443")
.build();
client = PredictionServiceClient.create(predictionServiceSettings);
} catch (Exception e) {
LOGGER.warning(e.getMessage());
return;
}
// Get the full path of the model.
String modelFullId = String.format("projects/%s/locations/%s/models/%s", PROJECT_ID, REGION, MODEL_ID);
// Prepare the content to be predicted.
ByteString content = ByteString.copyFromUtf8(CONTENT);
// Use the client to send the prediction request.
PredictResponse response = client.predict(modelFullId, content);
// Print the response.
LOGGER.info("Prediction Results:");
LOGGER.info(response.toString());
// Shut down the client.
client.close();
} catch (Exception e) {
LOGGER.warning(e.getMessage());
}
}
}