@BetaApi @Generated(value="by gapic-generator-java") public class PredictionServiceClient extends Object implements com.google.api.gax.core.BackgroundResource
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:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PlacementName name =
PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
UserEvent userEvent = UserEvent.newBuilder().build();
for (Map.Entry<String, Value> element :
predictionServiceClient.predict(name, userEvent).iterateAll()) {
// doThingsWith(element);
}
}
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().
| Method | Description | Method Variants |
|---|---|---|
| Predict | Makes a recommendation prediction. If using API Key based authentication, the API Key must be registered using the [PredictionApiKeyRegistry][google.cloud.recommendationengine.v1beta1.PredictionApiKeyRegistry] service. [Learn more](/recommendations-ai/docs/setting-up#register-key). |
Request object method variants only take one parameter, a request object, which must be constructed before the call.
"Flattened" method variants have converted the fields of the request object into function parameters to enable multiple ways to call the same method.
Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.
|
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:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To customize the endpoint:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over the wire:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newHttpJsonBuilder().build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
Please refer to the GitHub repository's samples for more quickstart code snippets.
| Modifier and Type | Class and Description |
|---|---|
static class |
PredictionServiceClient.PredictFixedSizeCollection |
static class |
PredictionServiceClient.PredictPage |
static class |
PredictionServiceClient.PredictPagedResponse |
| 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() |
PredictionServiceClient.PredictPagedResponse |
predict(PlacementName name,
UserEvent userEvent)
Makes a recommendation prediction.
|
PredictionServiceClient.PredictPagedResponse |
predict(PredictRequest request)
Makes a recommendation prediction.
|
PredictionServiceClient.PredictPagedResponse |
predict(String name,
UserEvent userEvent)
Makes a recommendation prediction.
|
com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> |
predictCallable()
Makes a recommendation prediction.
|
com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictionServiceClient.PredictPagedResponse> |
predictPagedCallable()
Makes a recommendation prediction.
|
void |
shutdown() |
void |
shutdownNow() |
protected PredictionServiceClient(PredictionServiceSettings settings) throws IOException
IOExceptionprotected PredictionServiceClient(PredictionServiceStub stub)
public static final PredictionServiceClient create() throws IOException
IOExceptionpublic static final PredictionServiceClient create(PredictionServiceSettings settings) throws IOException
IOExceptionpublic static final PredictionServiceClient create(PredictionServiceStub stub)
public final PredictionServiceSettings getSettings()
public PredictionServiceStub getStub()
public final PredictionServiceClient.PredictPagedResponse predict(PlacementName name, UserEvent userEvent)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PlacementName name =
PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]");
UserEvent userEvent = UserEvent.newBuilder().build();
for (Map.Entry<String, Value> element :
predictionServiceClient.predict(name, userEvent).iterateAll()) {
// doThingsWith(element);
}
}
name - Required. Full resource name of the format:
`{name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*}`
The id of the recommendation engine placement. This id is used to identify the set of
models that will be used to make the prediction.
We currently support three placements with the following IDs by default:
The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard
userEvent - Required. Context about the user, what they are looking at and what action
they took to trigger the predict request. Note that this user event detail won't be
ingested to userEvent logs. Thus, a separate userEvent write request is required for event
logging.com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictionServiceClient.PredictPagedResponse predict(String name, UserEvent userEvent)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
String name =
PlacementName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]")
.toString();
UserEvent userEvent = UserEvent.newBuilder().build();
for (Map.Entry<String, Value> element :
predictionServiceClient.predict(name, userEvent).iterateAll()) {
// doThingsWith(element);
}
}
name - Required. Full resource name of the format:
`{name=projects/*/locations/global/catalogs/default_catalog/eventStores/default_event_store/placements/*}`
The id of the recommendation engine placement. This id is used to identify the set of
models that will be used to make the prediction.
We currently support three placements with the following IDs by default:
The full list of available placements can be seen at https://console.cloud.google.com/recommendation/datafeeds/default_catalog/dashboard
userEvent - Required. Context about the user, what they are looking at and what action
they took to trigger the predict request. Note that this user event detail won't be
ingested to userEvent logs. Thus, a separate userEvent write request is required for event
logging.com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictionServiceClient.PredictPagedResponse predict(PredictRequest request)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PredictRequest request =
PredictRequest.newBuilder()
.setName(
PlacementName.of(
"[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]")
.toString())
.setUserEvent(UserEvent.newBuilder().build())
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.setFilter("filter-1274492040")
.setDryRun(true)
.putAllParams(new HashMap<String, Value>())
.putAllLabels(new HashMap<String, String>())
.build();
for (Map.Entry<String, Value> element :
predictionServiceClient.predict(request).iterateAll()) {
// doThingsWith(element);
}
}
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,PredictionServiceClient.PredictPagedResponse> predictPagedCallable()
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PredictRequest request =
PredictRequest.newBuilder()
.setName(
PlacementName.of(
"[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]")
.toString())
.setUserEvent(UserEvent.newBuilder().build())
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.setFilter("filter-1274492040")
.setDryRun(true)
.putAllParams(new HashMap<String, Value>())
.putAllLabels(new HashMap<String, String>())
.build();
ApiFuture<Map.Entry<String, Value>> future =
predictionServiceClient.predictPagedCallable().futureCall(request);
// Do something.
for (Map.Entry<String, Value> element : future.get().iterateAll()) {
// doThingsWith(element);
}
}
public final com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> predictCallable()
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PredictRequest request =
PredictRequest.newBuilder()
.setName(
PlacementName.of(
"[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]")
.toString())
.setUserEvent(UserEvent.newBuilder().build())
.setPageSize(883849137)
.setPageToken("pageToken873572522")
.setFilter("filter-1274492040")
.setDryRun(true)
.putAllParams(new HashMap<String, Value>())
.putAllLabels(new HashMap<String, String>())
.build();
while (true) {
PredictResponse response = predictionServiceClient.predictCallable().call(request);
for (Map.Entry<String, Value> element : response.getMetadataList()) {
// doThingsWith(element);
}
String nextPageToken = response.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
request = request.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
}
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 © 2024 Google LLC. All rights reserved.