Search in sources :

Example 1 with UserEventServiceClient

use of com.google.cloud.retail.v2.UserEventServiceClient 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());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Timestamp(com.google.protobuf.Timestamp) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEvent(com.google.cloud.retail.v2.UserEvent) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient) UserEventInlineSource(com.google.cloud.retail.v2.UserEventInlineSource) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) BigQueryException(com.google.cloud.bigquery.BigQueryException) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 2 with UserEventServiceClient

use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.

the class RejoinUserEvent method callRejoinUserEvents.

public static void callRejoinUserEvents(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()) {
        RejoinUserEventsRequest rejoinUserEventsRequest = RejoinUserEventsRequest.newBuilder().setParent(defaultCatalog).setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS).build();
        System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest);
        OperationFuture<RejoinUserEventsResponse, RejoinUserEventsMetadata> rejoinOperation = userEventServiceClient.rejoinUserEventsAsync(rejoinUserEventsRequest);
        System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName());
    }
    purgeUserEvent(visitorId);
}
Also used : RejoinUserEventsRequest(com.google.cloud.retail.v2.RejoinUserEventsRequest) RejoinUserEventsResponse(com.google.cloud.retail.v2.RejoinUserEventsResponse) RejoinUserEventsMetadata(com.google.cloud.retail.v2.RejoinUserEventsMetadata) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient)

Example 3 with UserEventServiceClient

use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.

the class SetupCleanup method purgeUserEvent.

public static void purgeUserEvent(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()) {
        PurgeUserEventsRequest purgeUserEventsRequest = PurgeUserEventsRequest.newBuilder().setFilter(String.format("visitorId=\"%s\"", visitorId)).setParent(DEFAULT_CATALOG).setForce(true).build();
        OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation = userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest);
        System.out.printf("The purge operation was started: %s%n", purgeOperation.getName());
    }
}
Also used : PurgeMetadata(com.google.cloud.retail.v2.PurgeMetadata) PurgeUserEventsRequest(com.google.cloud.retail.v2.PurgeUserEventsRequest) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient) PurgeUserEventsResponse(com.google.cloud.retail.v2.PurgeUserEventsResponse)

Example 4 with UserEventServiceClient

use of com.google.cloud.retail.v2.UserEventServiceClient in project java-retail by googleapis.

the class SetupCleanup method writeUserEvent.

public static UserEvent writeUserEvent(String visitorId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
        WriteUserEventRequest writeUserEventRequest = WriteUserEventRequest.newBuilder().setUserEvent(getUserEvent(visitorId)).setParent(DEFAULT_CATALOG).build();
        UserEvent userEvent = userEventServiceClient.writeUserEvent(writeUserEventRequest);
        System.out.printf("The user event is written. %n%s%n", userEvent);
        return userEvent;
    }
}
Also used : WriteUserEventRequest(com.google.cloud.retail.v2.WriteUserEventRequest) UserEvent(com.google.cloud.retail.v2.UserEvent) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient)

Example 5 with UserEventServiceClient

use of com.google.cloud.retail.v2.UserEventServiceClient 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());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) GcsSource(com.google.cloud.retail.v2.GcsSource) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) BigQueryException(com.google.cloud.bigquery.BigQueryException) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Aggregations

UserEventServiceClient (com.google.cloud.retail.v2.UserEventServiceClient)8 BigQueryException (com.google.cloud.bigquery.BigQueryException)3 ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)3 ImportUserEventsRequest (com.google.cloud.retail.v2.ImportUserEventsRequest)3 ImportUserEventsResponse (com.google.cloud.retail.v2.ImportUserEventsResponse)3 UserEvent (com.google.cloud.retail.v2.UserEvent)3 UserEventInputConfig (com.google.cloud.retail.v2.UserEventInputConfig)3 PurgeMetadata (com.google.cloud.retail.v2.PurgeMetadata)2 PurgeUserEventsRequest (com.google.cloud.retail.v2.PurgeUserEventsRequest)2 PurgeUserEventsResponse (com.google.cloud.retail.v2.PurgeUserEventsResponse)2 WriteUserEventRequest (com.google.cloud.retail.v2.WriteUserEventRequest)2 Operation (com.google.longrunning.Operation)2 OperationsClient (com.google.longrunning.OperationsClient)2 Timestamp (com.google.protobuf.Timestamp)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 BigQuerySource (com.google.cloud.retail.v2.BigQuerySource)1 GcsSource (com.google.cloud.retail.v2.GcsSource)1 ImportErrorsConfig (com.google.cloud.retail.v2.ImportErrorsConfig)1 RejoinUserEventsMetadata (com.google.cloud.retail.v2.RejoinUserEventsMetadata)1 RejoinUserEventsRequest (com.google.cloud.retail.v2.RejoinUserEventsRequest)1