use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.
the class PurgeUserEvent method callPurgeUserEvents.
public static void callPurgeUserEvents(String defaultCatalog, String visitorId) throws IOException, ExecutionException, InterruptedException {
writeUserEvent(visitorId);
// the "close" method on the client to safely clean up any remaining background resources.
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
PurgeUserEventsRequest purgeUserEventsRequest = PurgeUserEventsRequest.newBuilder().setFilter(String.format("visitorId=\"%s\"", visitorId)).setParent(defaultCatalog).setForce(true).build();
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation = userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest);
System.out.printf("The purge operation was started: %s%n", purgeOperation.getName());
}
}
use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.
the class WriteUserEvent method writeUserEvent.
public static void writeUserEvent(String defaultCatalog, String visitorId) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
Timestamp timestamp = Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build();
UserEvent userEvent = UserEvent.newBuilder().setEventType("home-page-view").setVisitorId(visitorId).setEventTime(timestamp).build();
System.out.println(userEvent);
WriteUserEventRequest writeUserEventRequest = WriteUserEventRequest.newBuilder().setUserEvent(userEvent).setParent(defaultCatalog).build();
System.out.printf("Write user event request: %s%n", writeUserEventRequest);
userEventServiceClient.writeUserEvent(writeUserEventRequest);
System.out.printf("Written user event: %s%n", userEvent);
}
purgeUserEvent(visitorId);
}
use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.
the class ImportUserEventsBigQuery method importUserEventsFromBigQuery.
public static void importUserEventsFromBigQuery(String projectId, String defaultCatalog, String datasetId, String tableId) throws IOException, InterruptedException {
try {
String dataSchema = "user_event";
BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId).setDataSchema(dataSchema).build();
UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setBigQuerySource(bigQuerySource).build();
ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).build();
System.out.printf("Import user events from BigQuery source request: %s%n", importRequest);
// the "close" method on the client to safely clean up any remaining background resources.
try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
String operationName = serviceClient.importUserEventsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s\n", operationName);
OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);
while (!operation.getDone()) {
// Keep polling the operation periodically until the import task is done.
int awaitDuration = 30000;
Thread.sleep(awaitDuration);
operation = operationsClient.getOperation(operationName);
}
if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf("Number of successfully imported events: %s\n", metadata.getSuccessCount());
System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
}
if (operation.hasResponse()) {
ImportUserEventsResponse response = operation.getResponse().unpack(ImportUserEventsResponse.class);
System.out.printf("Operation result: %s%n", response);
}
}
} catch (BigQueryException e) {
System.out.printf("Exception message: %s", e.getMessage());
}
}
Aggregations