|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -# [START retail_import_products_from_big_query] |
16 |
| -# Import products into a catalog from big query table using Retail API |
17 |
| -# |
| 15 | +import argparse |
18 | 16 | import os
|
19 |
| -import time |
20 |
| - |
21 |
| -from google.cloud.retail import ( |
22 |
| - BigQuerySource, |
23 |
| - ImportProductsRequest, |
24 |
| - ProductInputConfig, |
25 |
| - ProductServiceClient, |
26 |
| -) |
27 | 17 |
|
28 |
| -project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] |
29 | 18 | project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
|
30 | 19 |
|
31 |
| -default_catalog = f"projects/{project_number}/locations/global/catalogs/default_catalog/branches/default_branch" |
32 |
| -dataset_id = "products" |
33 |
| -table_id = "products" |
34 | 20 |
|
| 21 | +def main(project_id, dataset_id, table_id): |
| 22 | + # [START retail_import_products_from_big_query] |
| 23 | + # TODO: Set project_id to your Google Cloud Platform project ID. |
| 24 | + # project_id = "my-project" |
35 | 25 |
|
36 |
| -# TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS: |
37 |
| -# table_id = "products_some_invalid" |
| 26 | + # TODO: Set dataset_id |
| 27 | + # dataset_id = "products" |
38 | 28 |
|
| 29 | + # TODO: Set dataset_id |
| 30 | + # table_id = "products" |
39 | 31 |
|
40 |
| -# get import products from big query request |
41 |
| -def get_import_products_big_query_request(reconciliation_mode): |
42 |
| - # TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: |
43 |
| - # default_catalog = "invalid_catalog_name" |
44 |
| - big_query_source = BigQuerySource() |
45 |
| - big_query_source.project_id = project_id |
46 |
| - big_query_source.dataset_id = dataset_id |
47 |
| - big_query_source.table_id = table_id |
48 |
| - big_query_source.data_schema = "product" |
| 32 | + # Import products into a catalog from big query table using Retail API |
| 33 | + import time |
49 | 34 |
|
50 |
| - input_config = ProductInputConfig() |
51 |
| - input_config.big_query_source = big_query_source |
| 35 | + from google.cloud.retail import ( |
| 36 | + BigQuerySource, |
| 37 | + ImportProductsRequest, |
| 38 | + ProductInputConfig, |
| 39 | + ProductServiceClient, |
| 40 | + ) |
52 | 41 |
|
53 |
| - import_request = ImportProductsRequest() |
54 |
| - import_request.parent = default_catalog |
55 |
| - import_request.reconciliation_mode = reconciliation_mode |
56 |
| - import_request.input_config = input_config |
| 42 | + default_catalog = f"projects/{project_id}/locations/global/catalogs/default_catalog/branches/default_branch" |
57 | 43 |
|
58 |
| - print("---import products from big query table request---") |
59 |
| - print(import_request) |
| 44 | + # TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS: |
| 45 | + # table_id = "products_some_invalid" |
60 | 46 |
|
61 |
| - return import_request |
| 47 | + # get import products from big query request |
| 48 | + def get_import_products_big_query_request(reconciliation_mode): |
| 49 | + # TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: |
| 50 | + # default_catalog = "invalid_catalog_name" |
| 51 | + big_query_source = BigQuerySource() |
| 52 | + big_query_source.project_id = project_id |
| 53 | + big_query_source.dataset_id = dataset_id |
| 54 | + big_query_source.table_id = table_id |
| 55 | + big_query_source.data_schema = "product" |
62 | 56 |
|
| 57 | + input_config = ProductInputConfig() |
| 58 | + input_config.big_query_source = big_query_source |
63 | 59 |
|
64 |
| -# call the Retail API to import products |
65 |
| -def import_products_from_big_query(): |
66 |
| - # TRY THE FULL RECONCILIATION MODE HERE: |
67 |
| - reconciliation_mode = ImportProductsRequest.ReconciliationMode.INCREMENTAL |
| 60 | + import_request = ImportProductsRequest() |
| 61 | + import_request.parent = default_catalog |
| 62 | + import_request.reconciliation_mode = reconciliation_mode |
| 63 | + import_request.input_config = input_config |
68 | 64 |
|
69 |
| - import_big_query_request = get_import_products_big_query_request( |
70 |
| - reconciliation_mode |
71 |
| - ) |
72 |
| - big_query_operation = ProductServiceClient().import_products( |
73 |
| - import_big_query_request |
74 |
| - ) |
| 65 | + print("---import products from big query table request---") |
| 66 | + print(import_request) |
75 | 67 |
|
76 |
| - print("---the operation was started:----") |
77 |
| - print(big_query_operation.operation.name) |
| 68 | + return import_request |
78 | 69 |
|
79 |
| - while not big_query_operation.done(): |
80 |
| - print("---please wait till operation is done---") |
81 |
| - time.sleep(30) |
82 |
| - print("---import products operation is done---") |
| 70 | + # call the Retail API to import products |
| 71 | + def import_products_from_big_query(): |
| 72 | + # TRY THE FULL RECONCILIATION MODE HERE: |
| 73 | + reconciliation_mode = ImportProductsRequest.ReconciliationMode.INCREMENTAL |
83 | 74 |
|
84 |
| - if big_query_operation.metadata is not None: |
85 |
| - print("---number of successfully imported products---") |
86 |
| - print(big_query_operation.metadata.success_count) |
87 |
| - print("---number of failures during the importing---") |
88 |
| - print(big_query_operation.metadata.failure_count) |
89 |
| - else: |
90 |
| - print("---operation.metadata is empty---") |
| 75 | + import_big_query_request = get_import_products_big_query_request( |
| 76 | + reconciliation_mode |
| 77 | + ) |
| 78 | + big_query_operation = ProductServiceClient().import_products( |
| 79 | + import_big_query_request |
| 80 | + ) |
91 | 81 |
|
92 |
| - if big_query_operation.result is not None: |
93 |
| - print("---operation result:---") |
94 |
| - print(big_query_operation.result()) |
95 |
| - else: |
96 |
| - print("---operation.result is empty---") |
| 82 | + print("---the operation was started:----") |
| 83 | + print(big_query_operation.operation.name) |
97 | 84 |
|
| 85 | + while not big_query_operation.done(): |
| 86 | + print("---please wait till operation is done---") |
| 87 | + time.sleep(30) |
| 88 | + print("---import products operation is done---") |
| 89 | + |
| 90 | + if big_query_operation.metadata is not None: |
| 91 | + print("---number of successfully imported products---") |
| 92 | + print(big_query_operation.metadata.success_count) |
| 93 | + print("---number of failures during the importing---") |
| 94 | + print(big_query_operation.metadata.failure_count) |
| 95 | + else: |
| 96 | + print("---operation.metadata is empty---") |
| 97 | + |
| 98 | + if big_query_operation.result is not None: |
| 99 | + print("---operation result:---") |
| 100 | + print(big_query_operation.result()) |
| 101 | + else: |
| 102 | + print("---operation.result is empty---") |
| 103 | + |
| 104 | + import_products_from_big_query() |
98 | 105 |
|
99 |
| -import_products_from_big_query() |
100 | 106 |
|
101 | 107 | # [END retail_import_products_from_big_query]
|
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + parser = argparse.ArgumentParser() |
| 112 | + parser.add_argument("dataset_id") |
| 113 | + parser.add_argument("table_id") |
| 114 | + args = parser.parse_args() |
| 115 | + main(project_id, args.dataset_id, args.table_id) |
0 commit comments