2020
2121package product ;
2222
23+ import com .google .api .gax .rpc .InvalidArgumentException ;
24+ import com .google .api .gax .rpc .PermissionDeniedException ;
2325import com .google .cloud .ServiceOptions ;
2426import com .google .cloud .retail .v2 .GcsSource ;
2527import com .google .cloud .retail .v2 .ImportErrorsConfig ;
3234import com .google .longrunning .Operation ;
3335import com .google .longrunning .OperationsClient ;
3436import java .io .IOException ;
37+ import java .time .Instant ;
3538import java .util .Collections ;
39+ import java .util .concurrent .TimeUnit ;
3640
3741public class ImportProductsGcs {
3842
3943 public static void main (String [] args ) throws IOException , InterruptedException {
40- // TODO(developer): Replace these variables before running the sample.
4144 String projectId = ServiceOptions .getDefaultProjectId ();
4245 String branchName =
4346 String .format (
4447 "projects/%s/locations/global/catalogs/default_catalog/branches/0" , projectId );
48+
49+ String bucketName = System .getenv ("BUCKET_NAME" );
50+ String gcsBucket = String .format ("gs://%s" , bucketName );
51+ String gcsErrorBucket = String .format ("%s/errors" , gcsBucket );
52+
4553 // To check error handling, use an invalid catalog in request
4654 // branchName =
4755 // String.format("projects/%s/locations/global/catalogs/invalid_catalog/branches/default_branch", projectId);
4856
49- String gcsBucket = String .format ("gs://%s" , System .getenv ("BUCKET_NAME" ));
50- String gcsErrorBucket = String .format ("%s/errors" , gcsBucket );
5157 String gcsProductsObject = "products.json" ;
5258 // To check error handling, use an invalid product JSON.
53- // gcsProductsObject = "products_some_invalid.json";
59+ // gcsProductsObject = "products_some_invalid.json"
5460
55- ImportProductsRequest importGcsRequest =
56- getImportProductsGcsRequest (gcsProductsObject , gcsBucket , gcsErrorBucket , branchName );
57- waitForOperationCompletion (importGcsRequest );
61+ importProductsFromGcs (branchName , gcsBucket , gcsProductsObject );
5862 }
5963
60- public static ImportProductsRequest getImportProductsGcsRequest (
61- String gcsObjectName , String gcsBucket , String gcsErrorBucket , String branchName ) {
64+ public static void importProductsFromGcs (
65+ String branchName , String gcsBucket , String gcsProductsObject )
66+ throws IOException , InterruptedException {
67+ String gcsErrorBucket = String .format ("%s/errors" , gcsBucket );
68+
6269 GcsSource gcsSource =
6370 GcsSource .newBuilder ()
6471 .addAllInputUris (
65- Collections .singleton (String .format ("%s/%s" , gcsBucket , gcsObjectName )))
72+ Collections .singleton (String .format ("%s/%s" , gcsBucket , gcsProductsObject )))
6673 .build ();
6774
6875 ProductInputConfig inputConfig =
@@ -81,39 +88,53 @@ public static ImportProductsRequest getImportProductsGcsRequest(
8188 .setErrorsConfig (errorsConfig )
8289 .build ();
8390
84- System .out .println ("Import products from google cloud source request: " + importRequest );
85-
86- return importRequest ;
87- }
91+ System .out .printf ("Import products from google cloud source request: %s%n" , importRequest );
8892
89- public static void waitForOperationCompletion (ImportProductsRequest importRequest )
90- throws IOException , InterruptedException {
93+ // Initialize client that will be used to send requests. This client only
94+ // needs to be created once, and can be reused for multiple requests. After
95+ // completing all of your requests, call the "close" method on the client to
96+ // safely clean up any remaining background resources.
9197 try (ProductServiceClient serviceClient = ProductServiceClient .create ()) {
9298 String operationName = serviceClient .importProductsCallable ().call (importRequest ).getName ();
93- System .out .printf ("OperationName = %s\n " , operationName );
99+
100+ System .out .println ("The operation was started." );
101+ System .out .printf ("OperationName = %s%n" , operationName );
94102
95103 OperationsClient operationsClient = serviceClient .getOperationsClient ();
96104 Operation operation = operationsClient .getOperation (operationName );
97105
98- while (!operation .getDone ()) {
99- // Keep polling the operation periodically until the import task is done.
100- Thread .sleep (30_000 );
106+ Instant deadline = Instant .now ().plusSeconds (60 );
107+
108+ while (!operation .getDone () || Instant .now ().isBefore (deadline )) {
109+ System .out .println ("Please wait till operation is done." );
110+ TimeUnit .SECONDS .sleep (30 );
101111 operation = operationsClient .getOperation (operationName );
102112 }
103113
104114 if (operation .hasMetadata ()) {
105115 ImportMetadata metadata = operation .getMetadata ().unpack (ImportMetadata .class );
106116 System .out .printf (
107- "Number of successfully imported products: %s\ n " , metadata .getSuccessCount ());
117+ "Number of successfully imported products: %s% n" , metadata .getSuccessCount ());
108118 System .out .printf (
109- "Number of failures during the importing: %s\n " , metadata .getFailureCount ());
119+ "Number of failures during the importing: %s%n" , metadata .getFailureCount ());
120+ } else {
121+ System .out .println ("Metadata is empty." );
110122 }
111123
112124 if (operation .hasResponse ()) {
113125 ImportProductsResponse response =
114126 operation .getResponse ().unpack (ImportProductsResponse .class );
115127 System .out .printf ("Operation result: %s%n" , response );
128+ } else {
129+ System .out .println ("Operation result is empty." );
116130 }
131+ } catch (InvalidArgumentException e ) {
132+ System .out .printf (
133+ "%s%n'%s' file does not exist in the bucket. Please "
134+ + "make sure you have followed the setting up instructions." ,
135+ e .getMessage (), gcsProductsObject );
136+ } catch (PermissionDeniedException e ) {
137+ System .out .println (e .getMessage ());
117138 }
118139 }
119140}
0 commit comments