use of com.google.cloud.retail.v2.ImportUserEventsRequest in project java-retail by googleapis.
the class ImportUserEventsInline method importUserEventsFromInlineSource.
public static void importUserEventsFromInlineSource(String defaultCatalog) throws IOException, ExecutionException, InterruptedException {
try {
int userEventsNumber = 3;
int awaitDuration = 30;
List<UserEvent> userEvents = new ArrayList<>();
for (int i = 0; i < userEventsNumber; i++) {
Instant time = Instant.now();
Timestamp timestamp = Timestamp.newBuilder().setSeconds(time.getEpochSecond()).build();
UserEvent userEvent = UserEvent.newBuilder().setEventType("home-page-view").setVisitorId(UUID.randomUUID().toString()).setEventTime(timestamp).build();
userEvents.add(userEvent);
System.out.printf("User Event: %s%n", i);
System.out.println(userEvent);
}
UserEventInlineSource inlineSource = UserEventInlineSource.newBuilder().addAllUserEvents(userEvents).build();
UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setUserEventInlineSource(inlineSource).build();
ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).build();
System.out.printf("Import user events from inline source request: %s%n", importRequest);
// the "close" method on the client to safely clean up any remaining background resources.
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
OperationFuture<ImportUserEventsResponse, ImportMetadata> importOperation = userEventServiceClient.importUserEventsAsync(importRequest);
System.out.printf("The operation was started: %s%n", importOperation.getName());
System.out.println("Please wait till operation is done.");
userEventServiceClient.awaitTermination(awaitDuration, TimeUnit.SECONDS);
System.out.println("Import user events operation is done.");
if (importOperation.getMetadata().get() != null) {
System.out.printf("Number of successfully imported events: %s%n", importOperation.getMetadata().get().getSuccessCount());
System.out.printf("Number of failures during the importing: %s%n", importOperation.getMetadata().get().getFailureCount());
} else {
System.out.println("Metadata in bigQuery operation is empty.");
}
if (importOperation.get() != null) {
System.out.printf("Operation result: %s%n", importOperation.get());
} else {
System.out.println("Operation result is empty.");
}
}
} catch (BigQueryException e) {
System.out.printf("Exception message: %s", e.getMessage());
}
}
use of com.google.cloud.retail.v2.ImportUserEventsRequest in project java-retail by googleapis.
the class ImportUserEventsGcs method importUserEventsFromGcs.
public static void importUserEventsFromGcs(String gcsEventsObject, String defaultCatalog) throws IOException, InterruptedException {
try {
String gcsBucket = String.format("gs://%s", EventsCreateGcsBucket.getBucketName());
String gcsErrorsBucket = String.format("%s/error", gcsBucket);
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(String.format("%s/%s", gcsBucket, gcsEventsObject)).build();
UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setGcsSource(gcsSource).build();
ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorsBucket).build();
ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
System.out.printf("Import user events from google cloud 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 (InvalidArgumentException e) {
System.out.printf("Given GCS input path was not found. %n%s%n " + "Please run CreateTestResources class to create resources.", e.getMessage());
}
} catch (BigQueryException e) {
System.out.printf("Exception message: %s", e.getMessage());
}
}
use of com.google.cloud.retail.v2.ImportUserEventsRequest 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