Search in sources :

Example 6 with UserEventServiceClient

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());
    }
}
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 7 with UserEventServiceClient

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);
}
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)

Example 8 with UserEventServiceClient

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());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) BigQueryException(com.google.cloud.bigquery.BigQueryException) BigQuerySource(com.google.cloud.retail.v2.BigQuerySource) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient)

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