Search in sources :

Example 1 with UserEvent

use of com.google.cloud.retail.v2.UserEvent 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 UserEvent

use of com.google.cloud.retail.v2.UserEvent 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 3 with UserEvent

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

the class SetupCleanup method getUserEvent.

public static UserEvent getUserEvent(String visitorId) {
    int value = 3;
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build();
    Product product = Product.newBuilder().setId(UUID.randomUUID().toString()).build();
    ProductDetail productDetail = ProductDetail.newBuilder().setProduct(product).setQuantity(Int32Value.newBuilder().setValue(value).build()).build();
    UserEvent userEvent = UserEvent.newBuilder().setEventType("detail-page-view").setVisitorId(visitorId).setEventTime(timestamp).addAllProductDetails(Collections.singletonList(productDetail)).build();
    System.out.println(userEvent);
    return userEvent;
}
Also used : ProductDetail(com.google.cloud.retail.v2.ProductDetail) Product(com.google.cloud.retail.v2.Product) Timestamp(com.google.protobuf.Timestamp) UserEvent(com.google.cloud.retail.v2.UserEvent)

Example 4 with UserEvent

use of com.google.cloud.retail.v2.UserEvent 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);
}
Also used : WriteUserEventRequest(com.google.cloud.retail.v2.WriteUserEventRequest) Timestamp(com.google.protobuf.Timestamp) SetupCleanup.purgeUserEvent(setup.SetupCleanup.purgeUserEvent) UserEvent(com.google.cloud.retail.v2.UserEvent) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient)

Aggregations

UserEvent (com.google.cloud.retail.v2.UserEvent)4 UserEventServiceClient (com.google.cloud.retail.v2.UserEventServiceClient)3 Timestamp (com.google.protobuf.Timestamp)3 WriteUserEventRequest (com.google.cloud.retail.v2.WriteUserEventRequest)2 BigQueryException (com.google.cloud.bigquery.BigQueryException)1 ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)1 ImportUserEventsRequest (com.google.cloud.retail.v2.ImportUserEventsRequest)1 ImportUserEventsResponse (com.google.cloud.retail.v2.ImportUserEventsResponse)1 Product (com.google.cloud.retail.v2.Product)1 ProductDetail (com.google.cloud.retail.v2.ProductDetail)1 UserEventInlineSource (com.google.cloud.retail.v2.UserEventInlineSource)1 UserEventInputConfig (com.google.cloud.retail.v2.UserEventInputConfig)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 SetupCleanup.purgeUserEvent (setup.SetupCleanup.purgeUserEvent)1