@Generated(value="by gapic-generator") @BetaApi public class PredictionServiceClient extends Object implements com.google.api.gax.core.BackgroundResource
On any input that is documented to expect a string parameter in snake_case or kebab-case, either of those cases is accepted.
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name, payload, params);
}
Note: close() needs to be called on the predictionServiceClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
See the individual methods for example code.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
This class can be customized by passing in a custom instance of PredictionServiceSettings to create(). For example:
To customize credentials:
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To customize the endpoint:
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
| Modifier | Constructor and Description |
|---|---|
protected |
PredictionServiceClient(PredictionServiceSettings settings)
Constructs an instance of PredictionServiceClient, using the given settings.
|
protected |
PredictionServiceClient(PredictionServiceStub stub) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long duration,
TimeUnit unit) |
void |
close() |
static PredictionServiceClient |
create()
Constructs an instance of PredictionServiceClient with default settings.
|
static PredictionServiceClient |
create(PredictionServiceSettings settings)
Constructs an instance of PredictionServiceClient, using the given settings.
|
static PredictionServiceClient |
create(PredictionServiceStub stub)
Constructs an instance of PredictionServiceClient, using the given stub for making calls.
|
PredictionServiceSettings |
getSettings() |
PredictionServiceStub |
getStub() |
boolean |
isShutdown() |
boolean |
isTerminated() |
PredictResponse |
predict(ModelName name,
ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
PredictResponse |
predict(PredictRequest request)
Perform an online prediction.
|
PredictResponse |
predict(String name,
ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> |
predictCallable()
Perform an online prediction.
|
void |
shutdown() |
void |
shutdownNow() |
protected PredictionServiceClient(PredictionServiceSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") protected PredictionServiceClient(PredictionServiceStub stub)
public static final PredictionServiceClient create() throws IOException
IOExceptionpublic static final PredictionServiceClient create(PredictionServiceSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public static final PredictionServiceClient create(PredictionServiceStub stub)
public final PredictionServiceSettings getSettings()
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public PredictionServiceStub getStub()
public final PredictResponse predict(ModelName name, ExamplePayload payload, Map<String,String> params)
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name, payload, params);
}
name - Name of the model requested to serve the prediction.payload - Required. Payload to perform a prediction on. The payload must match the problem
type that the model was trained to solve.params - Additional domain-specific parameters, any string must be up to 25000 characters
long.com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictResponse predict(String name, ExamplePayload payload, Map<String,String> params)
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name.toString(), payload, params);
}
name - Name of the model requested to serve the prediction.payload - Required. Payload to perform a prediction on. The payload must match the problem
type that the model was trained to solve.params - Additional domain-specific parameters, any string must be up to 25000 characters
long.com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictResponse predict(PredictRequest request)
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
PredictRequest request = PredictRequest.newBuilder()
.setName(name.toString())
.setPayload(payload)
.build();
PredictResponse response = predictionServiceClient.predict(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> predictCallable()
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
PredictRequest request = PredictRequest.newBuilder()
.setName(name.toString())
.setPayload(payload)
.build();
ApiFuture<PredictResponse> future = predictionServiceClient.predictCallable().futureCall(request);
// Do something
PredictResponse response = future.get();
}
public final void close()
close in interface AutoCloseablepublic void shutdown()
shutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isShutdown()
isShutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isTerminated()
isTerminated in interface com.google.api.gax.core.BackgroundResourcepublic void shutdownNow()
shutdownNow in interface com.google.api.gax.core.BackgroundResourcepublic boolean awaitTermination(long duration,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface com.google.api.gax.core.BackgroundResourceInterruptedExceptionCopyright © 2019 Google LLC. All rights reserved.