Skip to content

Commit 0ed9463

Browse files
sborisenkoxNeenu1995gcf-owl-bot[bot]
authored andcommitted
chore(samples): Major update according review comments. (#369)
* Events crud changes. * Create/remove resources changes. * Search changes. * Import user events changes. * Product changes. * please revert this commit before merging * Product changes according to review comments. * Fix createTable NPE. * Resources logic, minor changes. * Remove init resources tests. * Add test resources creation. * Update config. * Change bucket name read logic. * Change bucket name read logic. * Change bucket name read logic. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Code style changes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Minor code 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: Neenu Shaji <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent c5b2e61 commit 0ed9463

File tree

55 files changed

+965
-934
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+965
-934
lines changed

retail/interactive-tutorials/src/main/java/events/ImportUserEventsGcs.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.cloud.retail.v2.UserEventServiceClient;
3535
import com.google.longrunning.Operation;
3636
import com.google.longrunning.OperationsClient;
37+
import events.setup.EventsCreateGcsBucket;
3738
import java.io.IOException;
3839

3940
public class ImportUserEventsGcs {
@@ -55,7 +56,7 @@ public static void main(String[] args) throws IOException, InterruptedException
5556
public static void importUserEventsFromGcs(String gcsEventsObject, String defaultCatalog)
5657
throws IOException, InterruptedException {
5758
try {
58-
String gcsBucket = String.format("gs://%s", System.getenv("EVENTS_BUCKET_NAME"));
59+
String gcsBucket = String.format("gs://%s", EventsCreateGcsBucket.getBucketName());
5960
String gcsErrorsBucket = String.format("%s/error", gcsBucket);
6061

6162
GcsSource gcsSource =

retail/interactive-tutorials/src/main/java/events/PurgeUserEvent.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static setup.SetupCleanup.writeUserEvent;
2626

2727
import com.google.api.gax.longrunning.OperationFuture;
28+
import com.google.cloud.ServiceOptions;
2829
import com.google.cloud.retail.v2.PurgeMetadata;
2930
import com.google.cloud.retail.v2.PurgeUserEventsRequest;
3031
import com.google.cloud.retail.v2.PurgeUserEventsResponse;
@@ -38,7 +39,7 @@ public class PurgeUserEvent {
3839
public static void main(String[] args)
3940
throws IOException, ExecutionException, InterruptedException {
4041
// TODO(developer): Replace these variables before running the sample.
41-
String projectId = System.getenv("PROJECT_ID");
42+
String projectId = ServiceOptions.getDefaultProjectId();
4243
String defaultCatalog =
4344
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
4445
// visitorId generated randomly.

retail/interactive-tutorials/src/main/java/events/RejoinUserEvent.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static setup.SetupCleanup.writeUserEvent;
2727

2828
import com.google.api.gax.longrunning.OperationFuture;
29+
import com.google.cloud.ServiceOptions;
2930
import com.google.cloud.retail.v2.RejoinUserEventsMetadata;
3031
import com.google.cloud.retail.v2.RejoinUserEventsRequest;
3132
import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope;
@@ -40,7 +41,7 @@ public class RejoinUserEvent {
4041
public static void main(String[] args)
4142
throws IOException, ExecutionException, InterruptedException {
4243
// TODO(developer): Replace these variables before running the sample.
43-
String projectId = System.getenv("PROJECT_ID");
44+
String projectId = ServiceOptions.getDefaultProjectId();
4445
String defaultCatalog =
4546
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
4647
// visitorId generated randomly.

retail/interactive-tutorials/src/main/java/events/WriteUserEvent.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static setup.SetupCleanup.purgeUserEvent;
2626

27+
import com.google.cloud.ServiceOptions;
2728
import com.google.cloud.retail.v2.UserEvent;
2829
import com.google.cloud.retail.v2.UserEventServiceClient;
2930
import com.google.cloud.retail.v2.WriteUserEventRequest;
@@ -38,17 +39,17 @@ public class WriteUserEvent {
3839
public static void main(String[] args)
3940
throws IOException, ExecutionException, InterruptedException {
4041
// TODO(developer): Replace these variables before running the sample.
41-
String projectId = System.getenv("PROJECT_ID");
42+
String projectId = ServiceOptions.getDefaultProjectId();
4243
String defaultCatalog =
4344
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
4445
// visitorId generated randomly.
4546
String visitorId = UUID.randomUUID().toString();
4647

4748
writeUserEvent(defaultCatalog, visitorId);
48-
purgeUserEvent(visitorId);
4949
}
5050

51-
public static void writeUserEvent(String defaultCatalog, String visitorId) throws IOException {
51+
public static void writeUserEvent(String defaultCatalog, String visitorId)
52+
throws IOException, ExecutionException, InterruptedException {
5253
// Initialize client that will be used to send requests. This client only needs to be created
5354
// once, and can be reused for multiple requests. After completing all of your requests, call
5455
// the "close" method on the client to safely clean up any remaining background resources.
@@ -74,6 +75,8 @@ public static void writeUserEvent(String defaultCatalog, String visitorId) throw
7475
userEventServiceClient.writeUserEvent(writeUserEventRequest);
7576
System.out.printf("Written user event: %s%n", userEvent);
7677
}
78+
79+
purgeUserEvent(visitorId);
7780
}
7881
}
7982

retail/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package events.setup;
1818

19-
import static events.setup.EventsCreateGcsBucket.eventsCreateGcsBucketAndUploadJsonFiles;
2019
import static setup.SetupCleanup.createBqDataset;
2120
import static setup.SetupCleanup.createBqTable;
2221
import static setup.SetupCleanup.getGson;
@@ -31,9 +30,7 @@
3130

3231
public class EventsCreateBigQueryTable {
3332

34-
public static void createBqTableWithEvents() throws IOException {
35-
eventsCreateGcsBucketAndUploadJsonFiles();
36-
33+
public static void main(String... args) throws IOException {
3734
String dataset = "user_events";
3835
String validEventsTable = "events";
3936
String invalidEventsTable = "events_some_invalid";

retail/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,40 @@
1919
import static setup.SetupCleanup.createBucket;
2020
import static setup.SetupCleanup.uploadObject;
2121

22+
import com.google.cloud.ServiceOptions;
23+
import com.google.protobuf.Timestamp;
2224
import java.io.IOException;
25+
import java.time.Instant;
2326

2427
public class EventsCreateGcsBucket {
2528

26-
private static final String BUCKET_NAME = System.getenv("BUCKET_NAME");
29+
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
2730

28-
public static String getBucketName() {
29-
return BUCKET_NAME;
30-
}
31+
private static final Timestamp CURRENT_DATE =
32+
Timestamp.newBuilder()
33+
.setSeconds(Instant.now().getEpochSecond())
34+
.setNanos(Instant.now().getNano())
35+
.build();
3136

32-
public static void eventsCreateGcsBucketAndUploadJsonFiles() throws IOException {
37+
private static final String BUCKET_NAME =
38+
String.format("%s_events_%s", PROJECT_ID, CURRENT_DATE.getSeconds());
39+
40+
public static void main(String... args) throws IOException {
3341
createBucket(BUCKET_NAME);
42+
System.out.printf("Events gcs bucket %s was created.", BUCKET_NAME);
43+
3444
uploadObject(BUCKET_NAME, "user_events.json", "src/main/resources/user_events.json");
45+
System.out.printf("File 'user_events.json' was uploaded into bucket '%s'.", BUCKET_NAME);
46+
3547
uploadObject(
3648
BUCKET_NAME,
3749
"user_events_some_invalid.json",
3850
"src/main/resources/user_events_some_invalid.json");
51+
System.out.printf(
52+
"File 'user_events_some_invalid.json' was uploaded into bucket '%s'.", BUCKET_NAME);
53+
}
54+
55+
public static String getBucketName() {
56+
return BUCKET_NAME;
3957
}
4058
}

retail/interactive-tutorials/src/main/java/init/CreateTestResources.java

+28-32
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
package init;
1818

19-
import static events.setup.EventsCreateBigQueryTable.createBqTableWithEvents;
20-
import static events.setup.EventsCreateGcsBucket.eventsCreateGcsBucketAndUploadJsonFiles;
21-
import static product.setup.ProductsCreateBigqueryTable.createBqTableWithProducts;
22-
import static product.setup.ProductsCreateGcsBucket.productsCreateGcsBucketAndUploadJsonFiles;
23-
19+
import com.google.cloud.ServiceOptions;
2420
import com.google.cloud.retail.v2.GcsSource;
2521
import com.google.cloud.retail.v2.ImportErrorsConfig;
2622
import com.google.cloud.retail.v2.ImportMetadata;
@@ -31,56 +27,57 @@
3127
import com.google.cloud.retail.v2.ProductServiceClient;
3228
import com.google.longrunning.Operation;
3329
import com.google.longrunning.OperationsClient;
30+
import events.setup.EventsCreateBigQueryTable;
31+
import events.setup.EventsCreateGcsBucket;
3432
import java.io.IOException;
3533
import java.util.Collections;
34+
import product.setup.ProductsCreateBigqueryTable;
35+
import product.setup.ProductsCreateGcsBucket;
3636

3737
public class CreateTestResources {
38-
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
39-
private static final String BUCKET_NAME = System.getenv("BUCKET_NAME");
40-
private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME"));
41-
private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET);
42-
private static final String DEFAULT_CATALOG =
43-
String.format(
44-
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
4538

46-
public static void main(String[] args) throws IOException, InterruptedException {
47-
productsCreateGcsBucketAndUploadJsonFiles();
48-
eventsCreateGcsBucketAndUploadJsonFiles();
49-
importProductsFromGcs();
50-
createBqTableWithProducts();
51-
createBqTableWithEvents();
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 bucketName = System.getenv("BUCKET_NAME");
43+
String gcsBucket = String.format("gs://%s", System.getenv("BUCKET_NAME"));
44+
String gcsErrorBucket = String.format("%s/errors", gcsBucket);
45+
String branchName =
46+
String.format(
47+
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
48+
49+
ProductsCreateGcsBucket.main();
50+
EventsCreateGcsBucket.main();
51+
importProductsFromGcs(bucketName, gcsErrorBucket, branchName);
52+
ProductsCreateBigqueryTable.main();
53+
EventsCreateBigQueryTable.main();
5254
}
5355

54-
public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName) {
56+
public static void importProductsFromGcs(
57+
String bucketName, String gcsErrorBucket, String branchName)
58+
throws IOException, InterruptedException {
5559
GcsSource gcsSource =
5660
GcsSource.newBuilder()
5761
.addAllInputUris(
58-
Collections.singleton(String.format("gs://%s/%s", BUCKET_NAME, gcsObjectName)))
62+
Collections.singleton(String.format("gs://%s/%s", bucketName, "products.json")))
5963
.build();
6064
ProductInputConfig inputConfig =
6165
ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
6266
System.out.println("GRS source: " + gcsSource.getInputUrisList());
6367

6468
ImportErrorsConfig errorsConfig =
65-
ImportErrorsConfig.newBuilder().setGcsPrefix(GCS_ERROR_BUCKET).build();
69+
ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorBucket).build();
6670
ImportProductsRequest importRequest =
6771
ImportProductsRequest.newBuilder()
68-
.setParent(DEFAULT_CATALOG)
72+
.setParent(branchName)
6973
.setReconciliationMode(ReconciliationMode.INCREMENTAL)
7074
.setInputConfig(inputConfig)
7175
.setErrorsConfig(errorsConfig)
7276
.build();
7377
System.out.println("Import products from google cloud source request: " + importRequest);
7478

75-
return importRequest;
76-
}
77-
78-
public static void importProductsFromGcs() throws IOException, InterruptedException {
79-
ImportProductsRequest importGcsRequest = getImportProductsGcsRequest("products.json");
80-
8179
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
82-
String operationName =
83-
serviceClient.importProductsCallable().call(importGcsRequest).getName();
80+
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
8481
System.out.printf("OperationName = %s\n", operationName);
8582

8683
OperationsClient operationsClient = serviceClient.getOperationsClient();
@@ -89,8 +86,7 @@ public static void importProductsFromGcs() throws IOException, InterruptedExcept
8986
while (!operation.getDone()) {
9087
System.out.println("Please wait till operation is completed.");
9188
// Keep polling the operation periodically until the import task is done.
92-
int awaitDuration = 30000;
93-
Thread.sleep(awaitDuration);
89+
Thread.sleep(30_000);
9490
operation = operationsClient.getOperation(operationName);
9591
}
9692

retail/interactive-tutorials/src/main/java/init/RemoveTestResources.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static setup.SetupCleanup.deleteDataset;
2121

2222
import com.google.api.gax.rpc.PermissionDeniedException;
23+
import com.google.cloud.ServiceOptions;
2324
import com.google.cloud.retail.v2.DeleteProductRequest;
2425
import com.google.cloud.retail.v2.ListProductsRequest;
2526
import com.google.cloud.retail.v2.Product;
@@ -29,25 +30,26 @@
2930

3031
public class RemoveTestResources {
3132

32-
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
33-
private static final String BUCKET_NAME = System.getenv("BUCKET_NAME");
34-
private static final String DEFAULT_CATALOG =
35-
String.format(
36-
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
33+
public static void main(String... args) throws IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = ServiceOptions.getDefaultProjectId();
36+
String bucketName = System.getenv("BUCKET_NAME");
37+
String branchName =
38+
String.format(
39+
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
3740

38-
public static void main(String[] args) throws IOException {
39-
deleteBucket(BUCKET_NAME);
40-
deleteAllProducts();
41-
deleteDataset(PROJECT_ID, "products");
42-
deleteDataset(PROJECT_ID, "user_events");
41+
deleteBucket(bucketName);
42+
deleteAllProducts(branchName);
43+
deleteDataset(projectId, "products");
44+
deleteDataset(projectId, "user_events");
4345
}
4446

45-
public static void deleteAllProducts() throws IOException {
47+
public static void deleteAllProducts(String branchName) throws IOException {
4648
System.out.println("Deleting products in process, please wait...");
4749

4850
try (ProductServiceClient productServiceClient = ProductServiceClient.create()) {
4951
ListProductsRequest listRequest =
50-
ListProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).build();
52+
ListProductsRequest.newBuilder().setParent(branchName).build();
5153
ListProductsPagedResponse products = productServiceClient.listProducts(listRequest);
5254

5355
int deleteCount = 0;
@@ -66,7 +68,7 @@ public static void deleteAllProducts() throws IOException {
6668
}
6769
}
6870

69-
System.out.printf("%s products were deleted from %s%n", deleteCount, DEFAULT_CATALOG);
71+
System.out.printf("%s products were deleted from %s%n", deleteCount, branchName);
7072
}
7173
}
7274
}

retail/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static setup.SetupCleanup.deleteProduct;
2525
import static setup.SetupCleanup.getProduct;
2626

27+
import com.google.cloud.ServiceOptions;
2728
import com.google.cloud.retail.v2.AddFulfillmentPlacesRequest;
2829
import com.google.cloud.retail.v2.ProductServiceClient;
2930
import com.google.protobuf.Timestamp;
@@ -35,11 +36,11 @@ public class AddFulfillmentPlaces {
3536

3637
public static void main(String[] args) throws IOException, InterruptedException {
3738
// TODO(developer): Replace these variables before running the sample.
38-
String projectId = System.getenv("PROJECT_ID");
39+
String projectId = ServiceOptions.getDefaultProjectId();
3940
String generatedProductId = UUID.randomUUID().toString();
4041
String productName =
4142
String.format(
42-
"projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s",
43+
"projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s",
4344
projectId, generatedProductId);
4445
Timestamp currentDate =
4546
Timestamp.newBuilder()
@@ -57,13 +58,15 @@ public static void addFulfillmentPlaces(String productName, Timestamp timestamp,
5758
throws IOException, InterruptedException {
5859
AddFulfillmentPlacesRequest addFulfillmentRequest =
5960
getAddFulfillmentRequest(productName, timestamp, placeId);
60-
ProductServiceClient.create().addFulfillmentPlacesAsync(addFulfillmentRequest);
61-
/*
62-
This is a long-running operation and its result is not immediately
63-
present with get operations,thus we simulate wait with sleep method.
64-
*/
65-
System.out.println("Add fulfillment places, wait 30 seconds: ");
66-
Thread.sleep(30_000);
61+
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
62+
serviceClient.addFulfillmentPlacesAsync(addFulfillmentRequest);
63+
/*
64+
This is a long-running operation and its result is not immediately
65+
present with get operations,thus we simulate wait with sleep method.
66+
*/
67+
System.out.println("Add fulfillment places, wait 30 seconds: ");
68+
Thread.sleep(30_000);
69+
}
6770
}
6871

6972
public static AddFulfillmentPlacesRequest getAddFulfillmentRequest(

0 commit comments

Comments
 (0)