16
16
17
17
package init ;
18
18
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 ;
24
20
import com .google .cloud .retail .v2 .GcsSource ;
25
21
import com .google .cloud .retail .v2 .ImportErrorsConfig ;
26
22
import com .google .cloud .retail .v2 .ImportMetadata ;
31
27
import com .google .cloud .retail .v2 .ProductServiceClient ;
32
28
import com .google .longrunning .Operation ;
33
29
import com .google .longrunning .OperationsClient ;
30
+ import events .setup .EventsCreateBigQueryTable ;
31
+ import events .setup .EventsCreateGcsBucket ;
34
32
import java .io .IOException ;
35
33
import java .util .Collections ;
34
+ import product .setup .ProductsCreateBigqueryTable ;
35
+ import product .setup .ProductsCreateGcsBucket ;
36
36
37
37
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 );
45
38
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 ();
52
54
}
53
55
54
- public static ImportProductsRequest getImportProductsGcsRequest (String gcsObjectName ) {
56
+ public static void importProductsFromGcs (
57
+ String bucketName , String gcsErrorBucket , String branchName )
58
+ throws IOException , InterruptedException {
55
59
GcsSource gcsSource =
56
60
GcsSource .newBuilder ()
57
61
.addAllInputUris (
58
- Collections .singleton (String .format ("gs://%s/%s" , BUCKET_NAME , gcsObjectName )))
62
+ Collections .singleton (String .format ("gs://%s/%s" , bucketName , "products.json" )))
59
63
.build ();
60
64
ProductInputConfig inputConfig =
61
65
ProductInputConfig .newBuilder ().setGcsSource (gcsSource ).build ();
62
66
System .out .println ("GRS source: " + gcsSource .getInputUrisList ());
63
67
64
68
ImportErrorsConfig errorsConfig =
65
- ImportErrorsConfig .newBuilder ().setGcsPrefix (GCS_ERROR_BUCKET ).build ();
69
+ ImportErrorsConfig .newBuilder ().setGcsPrefix (gcsErrorBucket ).build ();
66
70
ImportProductsRequest importRequest =
67
71
ImportProductsRequest .newBuilder ()
68
- .setParent (DEFAULT_CATALOG )
72
+ .setParent (branchName )
69
73
.setReconciliationMode (ReconciliationMode .INCREMENTAL )
70
74
.setInputConfig (inputConfig )
71
75
.setErrorsConfig (errorsConfig )
72
76
.build ();
73
77
System .out .println ("Import products from google cloud source request: " + importRequest );
74
78
75
- return importRequest ;
76
- }
77
-
78
- public static void importProductsFromGcs () throws IOException , InterruptedException {
79
- ImportProductsRequest importGcsRequest = getImportProductsGcsRequest ("products.json" );
80
-
81
79
try (ProductServiceClient serviceClient = ProductServiceClient .create ()) {
82
- String operationName =
83
- serviceClient .importProductsCallable ().call (importGcsRequest ).getName ();
80
+ String operationName = serviceClient .importProductsCallable ().call (importRequest ).getName ();
84
81
System .out .printf ("OperationName = %s\n " , operationName );
85
82
86
83
OperationsClient operationsClient = serviceClient .getOperationsClient ();
@@ -89,8 +86,7 @@ public static void importProductsFromGcs() throws IOException, InterruptedExcept
89
86
while (!operation .getDone ()) {
90
87
System .out .println ("Please wait till operation is completed." );
91
88
// Keep polling the operation periodically until the import task is done.
92
- int awaitDuration = 30000 ;
93
- Thread .sleep (awaitDuration );
89
+ Thread .sleep (30_000 );
94
90
operation = operationsClient .getOperation (operationName );
95
91
}
96
92
0 commit comments