Skip to content

Commit 8730a1e

Browse files
sborisenkoxgcf-owl-bot[bot]Neenu1995
authored andcommitted
chore(samples): Retail Tutorials. Events import (#302)
* Add import events. * Add kokoro configuration. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixes. * Change versions * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Refactoring code. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix review comments. * Add google test library. Minor fixes. * Readme changes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Change unit tests. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Minor changes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Neenu Shaji <[email protected]>
1 parent c221281 commit 8730a1e

File tree

6 files changed

+614
-0
lines changed

6 files changed

+614
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_big_query]
18+
19+
/*
20+
* Import user events into a catalog from GCS using Retail API
21+
*/
22+
23+
package events;
24+
25+
import com.google.cloud.ServiceOptions;
26+
import com.google.cloud.bigquery.BigQueryException;
27+
import com.google.cloud.retail.v2.BigQuerySource;
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.UserEventInputConfig;
32+
import com.google.cloud.retail.v2.UserEventServiceClient;
33+
import com.google.longrunning.Operation;
34+
import com.google.longrunning.OperationsClient;
35+
import java.io.IOException;
36+
37+
public class ImportUserEventsBigQuery {
38+
39+
public static void main(String[] args) throws IOException, InterruptedException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = ServiceOptions.getDefaultProjectId();
42+
String defaultCatalog =
43+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
44+
// TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: defaultCatalog =
45+
// "invalid_catalog_name"
46+
String datasetId = "user_events";
47+
String tableId = "events";
48+
// TO CHECK ERROR HANDLING USE THE TABLE OF INVALID USER EVENTS: tableId = "events_some_invalid"
49+
50+
importUserEventsFromBigQuery(projectId, defaultCatalog, datasetId, tableId);
51+
}
52+
53+
public static void importUserEventsFromBigQuery(
54+
String projectId, String defaultCatalog, String datasetId, String tableId)
55+
throws IOException, InterruptedException {
56+
try {
57+
String dataSchema = "user_event";
58+
59+
BigQuerySource bigQuerySource =
60+
BigQuerySource.newBuilder()
61+
.setProjectId(projectId)
62+
.setDatasetId(datasetId)
63+
.setTableId(tableId)
64+
.setDataSchema(dataSchema)
65+
.build();
66+
67+
UserEventInputConfig inputConfig =
68+
UserEventInputConfig.newBuilder().setBigQuerySource(bigQuerySource).build();
69+
70+
ImportUserEventsRequest importRequest =
71+
ImportUserEventsRequest.newBuilder()
72+
.setParent(defaultCatalog)
73+
.setInputConfig(inputConfig)
74+
.build();
75+
76+
System.out.printf("Import user events from BigQuery source request: %s%n", importRequest);
77+
78+
// Initialize client that will be used to send requests. This client only needs to be created
79+
// once, and can be reused for multiple requests. After completing all of your requests, call
80+
// the "close" method on the client to safely clean up any remaining background resources.
81+
try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
82+
String operationName =
83+
serviceClient.importUserEventsCallable().call(importRequest).getName();
84+
85+
System.out.printf("OperationName = %s\n", operationName);
86+
OperationsClient operationsClient = serviceClient.getOperationsClient();
87+
Operation operation = operationsClient.getOperation(operationName);
88+
89+
while (!operation.getDone()) {
90+
// Keep polling the operation periodically until the import task is done.
91+
int awaitDuration = 30000;
92+
Thread.sleep(awaitDuration);
93+
operation = operationsClient.getOperation(operationName);
94+
}
95+
96+
if (operation.hasMetadata()) {
97+
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
98+
System.out.printf(
99+
"Number of successfully imported events: %s\n", metadata.getSuccessCount());
100+
System.out.printf(
101+
"Number of failures during the importing: %s\n", metadata.getFailureCount());
102+
}
103+
104+
if (operation.hasResponse()) {
105+
ImportUserEventsResponse response =
106+
operation.getResponse().unpack(ImportUserEventsResponse.class);
107+
System.out.printf("Operation result: %s%n", response);
108+
}
109+
}
110+
} catch (BigQueryException e) {
111+
System.out.printf("Exception message: %s", e.getMessage());
112+
}
113+
}
114+
}
115+
116+
// [END retail_import_user_events_from_big_query]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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_gcs]
18+
19+
/*
20+
* Import user events into a catalog from GCS using Retail API
21+
*/
22+
23+
package events;
24+
25+
import com.google.api.gax.rpc.InvalidArgumentException;
26+
import com.google.cloud.ServiceOptions;
27+
import com.google.cloud.bigquery.BigQueryException;
28+
import com.google.cloud.retail.v2.GcsSource;
29+
import com.google.cloud.retail.v2.ImportErrorsConfig;
30+
import com.google.cloud.retail.v2.ImportMetadata;
31+
import com.google.cloud.retail.v2.ImportUserEventsRequest;
32+
import com.google.cloud.retail.v2.ImportUserEventsResponse;
33+
import com.google.cloud.retail.v2.UserEventInputConfig;
34+
import com.google.cloud.retail.v2.UserEventServiceClient;
35+
import com.google.longrunning.Operation;
36+
import com.google.longrunning.OperationsClient;
37+
import java.io.IOException;
38+
39+
public class ImportUserEventsGcs {
40+
41+
public static void main(String[] args) throws IOException, InterruptedException {
42+
// TODO(developer): Replace these variables before running the sample.
43+
String projectId = ServiceOptions.getDefaultProjectId();
44+
String defaultCatalog =
45+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
46+
// TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: defaultCatalog =
47+
// "invalid_catalog_name"
48+
String gcsEventsObject = "user_events.json";
49+
// TO CHECK ERROR HANDLING USE THE JSON WITH INVALID USER EVENT: gcsEventsObject =
50+
// "user_events_some_invalid.json"
51+
52+
importUserEventsFromGcs(gcsEventsObject, defaultCatalog);
53+
}
54+
55+
public static void importUserEventsFromGcs(String gcsEventsObject, String defaultCatalog)
56+
throws IOException, InterruptedException {
57+
try {
58+
String gcsBucket = String.format("gs://%s", System.getenv("EVENTS_BUCKET_NAME"));
59+
String gcsErrorsBucket = String.format("%s/error", gcsBucket);
60+
61+
GcsSource gcsSource =
62+
GcsSource.newBuilder()
63+
.addInputUris(String.format("%s/%s", gcsBucket, gcsEventsObject))
64+
.build();
65+
66+
UserEventInputConfig inputConfig =
67+
UserEventInputConfig.newBuilder().setGcsSource(gcsSource).build();
68+
69+
ImportErrorsConfig errorsConfig =
70+
ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorsBucket).build();
71+
72+
ImportUserEventsRequest importRequest =
73+
ImportUserEventsRequest.newBuilder()
74+
.setParent(defaultCatalog)
75+
.setInputConfig(inputConfig)
76+
.setErrorsConfig(errorsConfig)
77+
.build();
78+
79+
System.out.printf("Import user events from google cloud source request: %s%n", importRequest);
80+
81+
// Initialize client that will be used to send requests. This client only needs to be created
82+
// once, and can be reused for multiple requests. After completing all of your requests, call
83+
// the "close" method on the client to safely clean up any remaining background resources.
84+
try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
85+
String operationName =
86+
serviceClient.importUserEventsCallable().call(importRequest).getName();
87+
88+
System.out.printf("OperationName = %s\n", operationName);
89+
90+
OperationsClient operationsClient = serviceClient.getOperationsClient();
91+
Operation operation = operationsClient.getOperation(operationName);
92+
93+
while (!operation.getDone()) {
94+
// Keep polling the operation periodically until the import task is done.
95+
int awaitDuration = 30000;
96+
Thread.sleep(awaitDuration);
97+
operation = operationsClient.getOperation(operationName);
98+
}
99+
100+
if (operation.hasMetadata()) {
101+
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
102+
System.out.printf(
103+
"Number of successfully imported events: %s\n", metadata.getSuccessCount());
104+
System.out.printf(
105+
"Number of failures during the importing: %s\n", metadata.getFailureCount());
106+
}
107+
108+
if (operation.hasResponse()) {
109+
ImportUserEventsResponse response =
110+
operation.getResponse().unpack(ImportUserEventsResponse.class);
111+
System.out.printf("Operation result: %s%n", response);
112+
}
113+
} catch (InvalidArgumentException e) {
114+
System.out.printf(
115+
"Given GCS input path was not found. %n%s%n "
116+
+ "Please run CreateTestResources class to create resources.",
117+
e.getMessage());
118+
}
119+
} catch (BigQueryException e) {
120+
System.out.printf("Exception message: %s", e.getMessage());
121+
}
122+
}
123+
}
124+
125+
// [END retail_import_user_events_from_gcs]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)