This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add product import samples (#149)
* feat: product_number variable is replaced with product_id Import product code samples are added * revert changes in README * fix typo in TEST_RESOURCES_SETUP_CLEANUP.md * include project number in import_products_big_query_table.py * use project_number in import_products_gcs.py; use f-string * use project_number in import_products_inline_source.py * use project_number in product/setup/setup_cleanup.py * use project_number in search_simple_query.py * use project_number in search_with_boost_spec.py * use project_number in search_with_filtering.py * use project number in search_with_ordering.py * use project_number in search_with_pagination.py * use project_number in search_with_query_expansion_spec.py * use project_number in test_resources_recovery * use project_number in search_with_facet_spec.py * fix typo in f-string * fix typos * use correct path to resources * lint * revert change to paths * resolve error where bq table doesn't exist * make setup more robust * lint * import product from inline source is fixed * use google.cloud.bigquery client * update test to reflect changes * lint Co-authored-by: Karl Weinmeister <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
- Loading branch information
1 parent
5762804
commit b4c8608
Showing
20 changed files
with
1,167 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
samples/interactive-tutorials/product/import_products_big_query_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START retail_import_products_from_big_query] | ||
# Import products into a catalog from big query table using Retail API | ||
# | ||
import os | ||
import time | ||
|
||
from google.cloud.retail import ( | ||
BigQuerySource, | ||
ImportProductsRequest, | ||
ProductInputConfig, | ||
ProductServiceClient, | ||
) | ||
|
||
project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
|
||
default_catalog = f"projects/{project_number}/locations/global/catalogs/default_catalog/branches/default_branch" | ||
dataset_id = "products" | ||
table_id = "products" | ||
|
||
|
||
# TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS: | ||
# table_id = "products_some_invalid" | ||
|
||
|
||
# get import products from big query request | ||
def get_import_products_big_query_request(reconciliation_mode): | ||
# TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: | ||
# default_catalog = "invalid_catalog_name" | ||
big_query_source = BigQuerySource() | ||
big_query_source.project_id = project_id | ||
big_query_source.dataset_id = dataset_id | ||
big_query_source.table_id = table_id | ||
big_query_source.data_schema = "product" | ||
|
||
input_config = ProductInputConfig() | ||
input_config.big_query_source = big_query_source | ||
|
||
import_request = ImportProductsRequest() | ||
import_request.parent = default_catalog | ||
import_request.reconciliation_mode = reconciliation_mode | ||
import_request.input_config = input_config | ||
|
||
print("---import products from big query table request---") | ||
print(import_request) | ||
|
||
return import_request | ||
|
||
|
||
# call the Retail API to import products | ||
def import_products_from_big_query(): | ||
# TRY THE FULL RECONCILIATION MODE HERE: | ||
reconciliation_mode = ImportProductsRequest.ReconciliationMode.INCREMENTAL | ||
|
||
import_big_query_request = get_import_products_big_query_request( | ||
reconciliation_mode | ||
) | ||
big_query_operation = ProductServiceClient().import_products( | ||
import_big_query_request | ||
) | ||
|
||
print("---the operation was started:----") | ||
print(big_query_operation.operation.name) | ||
|
||
while not big_query_operation.done(): | ||
print("---please wait till operation is done---") | ||
time.sleep(30) | ||
print("---import products operation is done---") | ||
|
||
if big_query_operation.metadata is not None: | ||
print("---number of successfully imported products---") | ||
print(big_query_operation.metadata.success_count) | ||
print("---number of failures during the importing---") | ||
print(big_query_operation.metadata.failure_count) | ||
else: | ||
print("---operation.metadata is empty---") | ||
|
||
if big_query_operation.result is not None: | ||
print("---operation result:---") | ||
print(big_query_operation.result()) | ||
else: | ||
print("---operation.result is empty---") | ||
|
||
|
||
import_products_from_big_query() | ||
|
||
# [END retail_import_products_from_big_query] |
31 changes: 31 additions & 0 deletions
31
samples/interactive-tutorials/product/import_products_bq_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import re | ||
import subprocess | ||
|
||
|
||
def test_import_products_bq(): | ||
output = str( | ||
subprocess.check_output("python import_products_big_query_table.py", shell=True) | ||
) | ||
|
||
assert re.match(".*import products from big query table request.*", output) | ||
assert re.match(".*the operation was started.*", output) | ||
assert re.match( | ||
".*projects/.*/locations/global/catalogs/default_catalog/branches/0/operations/import-products.*", | ||
output, | ||
) | ||
|
||
assert re.match(".*number of successfully imported products.*316.*", output) |
111 changes: 111 additions & 0 deletions
111
samples/interactive-tutorials/product/import_products_gcs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START retail_import_products_from_gcs] | ||
# Import products into a catalog from gcs using Retail API | ||
# | ||
import os | ||
import time | ||
|
||
from google.cloud.retail import ( | ||
GcsSource, | ||
ImportErrorsConfig, | ||
ImportProductsRequest, | ||
ProductInputConfig, | ||
ProductServiceClient, | ||
) | ||
|
||
|
||
# Read the project number from the environment variable | ||
project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
bucket_name = os.environ["BUCKET_NAME"] | ||
|
||
# You can change the branch here. The "default_branch" is set to point to the branch "0" | ||
default_catalog = f"projects/{project_number}/locations/global/catalogs/default_catalog/branches/default_branch" | ||
|
||
gcs_bucket = f"gs://{bucket_name}" | ||
gcs_errors_bucket = f"{gcs_bucket}/error" | ||
gcs_products_object = "products.json" | ||
|
||
|
||
# TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT | ||
# gcs_products_object = "products_some_invalid.json" | ||
|
||
|
||
# get import products from gcs request | ||
def get_import_products_gcs_request(gcs_object_name: str): | ||
# TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE: | ||
# default_catalog = "invalid_catalog_name" | ||
gcs_source = GcsSource() | ||
gcs_source.input_uris = [f"{gcs_bucket}/{gcs_object_name}"] | ||
|
||
input_config = ProductInputConfig() | ||
input_config.gcs_source = gcs_source | ||
print("GRS source:") | ||
print(gcs_source.input_uris) | ||
|
||
errors_config = ImportErrorsConfig() | ||
errors_config.gcs_prefix = gcs_errors_bucket | ||
|
||
import_request = ImportProductsRequest() | ||
import_request.parent = default_catalog | ||
import_request.reconciliation_mode = ( | ||
ImportProductsRequest.ReconciliationMode.INCREMENTAL | ||
) | ||
import_request.input_config = input_config | ||
import_request.errors_config = errors_config | ||
|
||
print("---import products from google cloud source request---") | ||
print(import_request) | ||
|
||
return import_request | ||
|
||
|
||
# call the Retail API to import products | ||
def import_products_from_gcs(): | ||
import_gcs_request = get_import_products_gcs_request(gcs_products_object) | ||
gcs_operation = ProductServiceClient().import_products(import_gcs_request) | ||
|
||
print("---the operation was started:----") | ||
print(gcs_operation.operation.name) | ||
|
||
while not gcs_operation.done(): | ||
print("---please wait till operation is done---") | ||
time.sleep(30) | ||
print("---import products operation is done---") | ||
|
||
if gcs_operation.metadata is not None: | ||
print("---number of successfully imported products---") | ||
print(gcs_operation.metadata.success_count) | ||
print("---number of failures during the importing---") | ||
print(gcs_operation.metadata.failure_count) | ||
else: | ||
print("---operation.metadata is empty---") | ||
|
||
if gcs_operation.result is not None: | ||
print("---operation result:---") | ||
print(gcs_operation.result()) | ||
else: | ||
print("---operation.result is empty---") | ||
|
||
# The imported products needs to be indexed in the catalog before they become available for search. | ||
print( | ||
"Wait 2-5 minutes till products become indexed in the catalog, after that they will be available for search" | ||
) | ||
|
||
|
||
import_products_from_gcs() | ||
|
||
# [END retail_import_products_from_gcs] |
30 changes: 30 additions & 0 deletions
30
samples/interactive-tutorials/product/import_products_gcs_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import re | ||
import subprocess | ||
|
||
|
||
def test_import_products_gcs(): | ||
output = str(subprocess.check_output("python import_products_gcs.py", shell=True)) | ||
|
||
assert re.match(".*import products from google cloud source request.*", output) | ||
assert re.match('.*input_uris: "gs://.*/products.json".*', output) | ||
assert re.match(".*the operation was started.*", output) | ||
assert re.match( | ||
".*projects/.*/locations/global/catalogs/default_catalog/branches/0/operations/import-products.*", | ||
output, | ||
) | ||
|
||
assert re.match(".*number of successfully imported products.*316.*", output) |
Oops, something went wrong.