|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START retail_import_user_events_from_inline_source] |
| 18 | + |
| 19 | +/* |
| 20 | + * Import user events into a catalog from inline source using Retail API |
| 21 | + */ |
| 22 | + |
| 23 | +package events; |
| 24 | + |
| 25 | +import com.google.api.gax.longrunning.OperationFuture; |
| 26 | +import com.google.cloud.ServiceOptions; |
| 27 | +import com.google.cloud.bigquery.BigQueryException; |
| 28 | +import com.google.cloud.retail.v2.ImportMetadata; |
| 29 | +import com.google.cloud.retail.v2.ImportUserEventsRequest; |
| 30 | +import com.google.cloud.retail.v2.ImportUserEventsResponse; |
| 31 | +import com.google.cloud.retail.v2.UserEvent; |
| 32 | +import com.google.cloud.retail.v2.UserEventInlineSource; |
| 33 | +import com.google.cloud.retail.v2.UserEventInputConfig; |
| 34 | +import com.google.cloud.retail.v2.UserEventServiceClient; |
| 35 | +import com.google.protobuf.Timestamp; |
| 36 | +import java.io.IOException; |
| 37 | +import java.time.Instant; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.UUID; |
| 41 | +import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | + |
| 44 | +public class ImportUserEventsInline { |
| 45 | + |
| 46 | + public static void main(String[] args) |
| 47 | + throws IOException, ExecutionException, InterruptedException { |
| 48 | + // TODO(developer): Replace these variables before running the sample. |
| 49 | + String projectId = ServiceOptions.getDefaultProjectId(); |
| 50 | + String defaultCatalog = |
| 51 | + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); |
| 52 | + |
| 53 | + importUserEventsFromInlineSource(defaultCatalog); |
| 54 | + } |
| 55 | + |
| 56 | + public static void importUserEventsFromInlineSource(String defaultCatalog) |
| 57 | + throws IOException, ExecutionException, InterruptedException { |
| 58 | + try { |
| 59 | + int userEventsNumber = 3; |
| 60 | + int awaitDuration = 30; |
| 61 | + List<UserEvent> userEvents = new ArrayList<>(); |
| 62 | + |
| 63 | + for (int i = 0; i < userEventsNumber; i++) { |
| 64 | + Instant time = Instant.now(); |
| 65 | + Timestamp timestamp = Timestamp.newBuilder().setSeconds(time.getEpochSecond()).build(); |
| 66 | + |
| 67 | + UserEvent userEvent = |
| 68 | + UserEvent.newBuilder() |
| 69 | + .setEventType("home-page-view") |
| 70 | + .setVisitorId(UUID.randomUUID().toString()) |
| 71 | + .setEventTime(timestamp) |
| 72 | + .build(); |
| 73 | + |
| 74 | + userEvents.add(userEvent); |
| 75 | + |
| 76 | + System.out.printf("User Event: %s%n", i); |
| 77 | + System.out.println(userEvent); |
| 78 | + } |
| 79 | + |
| 80 | + UserEventInlineSource inlineSource = |
| 81 | + UserEventInlineSource.newBuilder().addAllUserEvents(userEvents).build(); |
| 82 | + |
| 83 | + UserEventInputConfig inputConfig = |
| 84 | + UserEventInputConfig.newBuilder().setUserEventInlineSource(inlineSource).build(); |
| 85 | + |
| 86 | + ImportUserEventsRequest importRequest = |
| 87 | + ImportUserEventsRequest.newBuilder() |
| 88 | + .setParent(defaultCatalog) |
| 89 | + .setInputConfig(inputConfig) |
| 90 | + .build(); |
| 91 | + System.out.printf("Import user events from inline source request: %s%n", importRequest); |
| 92 | + |
| 93 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 94 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 95 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 96 | + try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { |
| 97 | + OperationFuture<ImportUserEventsResponse, ImportMetadata> importOperation = |
| 98 | + userEventServiceClient.importUserEventsAsync(importRequest); |
| 99 | + |
| 100 | + System.out.printf("The operation was started: %s%n", importOperation.getName()); |
| 101 | + System.out.println("Please wait till operation is done."); |
| 102 | + |
| 103 | + userEventServiceClient.awaitTermination(awaitDuration, TimeUnit.SECONDS); |
| 104 | + System.out.println("Import user events operation is done."); |
| 105 | + |
| 106 | + if (importOperation.getMetadata().get() != null) { |
| 107 | + System.out.printf( |
| 108 | + "Number of successfully imported events: %s%n", |
| 109 | + importOperation.getMetadata().get().getSuccessCount()); |
| 110 | + |
| 111 | + System.out.printf( |
| 112 | + "Number of failures during the importing: %s%n", |
| 113 | + importOperation.getMetadata().get().getFailureCount()); |
| 114 | + } else { |
| 115 | + System.out.println("Metadata in bigQuery operation is empty."); |
| 116 | + } |
| 117 | + if (importOperation.get() != null) { |
| 118 | + System.out.printf("Operation result: %s%n", importOperation.get()); |
| 119 | + } else { |
| 120 | + System.out.println("Operation result is empty."); |
| 121 | + } |
| 122 | + } |
| 123 | + } catch (BigQueryException e) { |
| 124 | + System.out.printf("Exception message: %s", e.getMessage()); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// [END retail_import_user_events_from_inline_source] |
0 commit comments