-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(samples): interactive tutorials code samples for import products (
#147)
- Loading branch information
1 parent
1173bb0
commit 27428ad
Showing
9 changed files
with
661 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
retail/interactive-tutorials/product/import-products-big-query-table.js
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,94 @@ | ||
// Copyright 2022 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. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
// [START retail_import_products_from_big_query] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
const projectId = process.env['PROJECT_ID']; | ||
|
||
const datasetId = 'products'; | ||
const tableId = 'products'; // TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS | ||
const dataSchema = 'product'; | ||
|
||
// Placement | ||
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE | ||
|
||
// The desired input location of the data. | ||
const inputConfig = { | ||
bigQuerySource: { | ||
projectId, | ||
datasetId, | ||
tableId, | ||
dataSchema, | ||
}, | ||
}; | ||
|
||
const reconciliationModes = { | ||
RECONCILIATION_MODE_UNSPECIFIED: 0, | ||
INCREMENTAL: 1, | ||
FULL: 2, | ||
}; | ||
|
||
const IResponseParams = { | ||
IImportProductsResponse: 0, | ||
IImportMetadata: 1, | ||
IOperation: 2, | ||
}; | ||
|
||
// The mode of reconciliation between existing products and the products to be imported. | ||
const reconciliationMode = reconciliationModes.INCREMENTAL; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callImportProducts = async () => { | ||
// Construct request | ||
const request = { | ||
parent, | ||
inputConfig, | ||
reconciliationMode, | ||
}; | ||
console.log('Import product request:', request); | ||
|
||
// Run request | ||
const [operation] = await retailClient.importProducts(request); | ||
const response = await operation.promise(); | ||
const result = response[IResponseParams.IImportMetadata]; | ||
console.log( | ||
`Number of successfully imported products: ${result.successCount | 0}` | ||
); | ||
console.log( | ||
`Number of failures during the importing: ${result.failureCount | 0}` | ||
); | ||
console.log(`Operation result: ${JSON.stringify(response)}`); | ||
}; | ||
|
||
console.log('Start import products'); | ||
await callImportProducts(); | ||
console.log('Import products finished'); | ||
// [END retail_import_products_from_big_query] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
91 changes: 91 additions & 0 deletions
91
retail/interactive-tutorials/product/import-products-gcs.js
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,91 @@ | ||
// Copyright 2022 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. | ||
|
||
'use strict'; | ||
|
||
async function main(bucketName) { | ||
// [START retail_import_products_from_gcs] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
const gcsBucket = `gs://${bucketName}`; | ||
const gcsErrorsBucket = `gs://${bucketName}/error`; | ||
const gcsProductsObject = 'products.json'; // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT | ||
|
||
// Placement | ||
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; //TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE | ||
|
||
// The desired input location of the data. | ||
const inputConfig = { | ||
gcsSource: { | ||
inputUris: [gcsBucket + '/' + gcsProductsObject], | ||
dataSchema: 'product', | ||
}, | ||
}; | ||
|
||
// The desired location of errors incurred during the Import. | ||
const errorsConfig = { | ||
gcsPrefix: gcsErrorsBucket, | ||
}; | ||
|
||
const IResponseParams = { | ||
IImportProductsResponse: 0, | ||
IImportMetadata: 1, | ||
IOperation: 2, | ||
}; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callImportProducts = async () => { | ||
// Construct request | ||
const request = { | ||
parent, | ||
inputConfig, | ||
errorsConfig, | ||
}; | ||
console.log('Import products request:', request); | ||
|
||
// Run request | ||
const [operation] = await retailClient.importProducts(request); | ||
const response = await operation.promise(); | ||
const result = response[IResponseParams.IImportMetadata]; | ||
console.log( | ||
`Number of successfully imported products: ${result.successCount | 0}` | ||
); | ||
console.log( | ||
`Number of failures during the importing: ${result.failureCount | 0}` | ||
); | ||
console.log(`Operation result: ${JSON.stringify(response)}`); | ||
}; | ||
console.log('Start import products'); | ||
await callImportProducts(); | ||
console.log('Import products finished'); | ||
// [END retail_import_products_from_gcs] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main( | ||
...(() => { | ||
const argv = process.argv.slice(2); | ||
return argv.length ? argv : [process.env['BUCKET_NAME']]; | ||
})() | ||
); |
131 changes: 131 additions & 0 deletions
131
retail/interactive-tutorials/product/import-products-inline-source.js
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,131 @@ | ||
// Copyright 2022 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. | ||
|
||
'use strict'; | ||
|
||
async function main(id1, id2) { | ||
// [START retail_import_products_from_inline_source] | ||
|
||
// Imports the Google Cloud client library. | ||
const {ProductServiceClient} = require('@google-cloud/retail').v2; | ||
const utils = require('../setup/setup-cleanup'); | ||
|
||
const projectNumber = process.env['GCLOUD_PROJECT']; | ||
|
||
// Placement | ||
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; | ||
|
||
const product1 = { | ||
id: id1 ? id1 : Math.random().toString(36).slice(2).toUpperCase(), | ||
title: '#IamRemarkable Pen', //TO CHECK ERROR HANDLING COMMENT OUT THE PRODUCT TITLE HERE | ||
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Office/IamRemarkable+Pen', | ||
brands: ['#IamRemarkable'], | ||
categories: ['Apparel'], | ||
priceInfo: { | ||
price: 16.0, | ||
originalPrice: 45.0, | ||
cost: 12.0, | ||
currencyCode: 'USD', | ||
}, | ||
colorInfo: { | ||
colorFamilies: ['Blue'], | ||
colors: ['Light blue', 'Blue', 'Dark blue'], | ||
}, | ||
fulFillmentInfo: { | ||
type: 'pickup-in-store', | ||
placeIds: ['store1', 'store2'], | ||
}, | ||
retrievable_fields: { | ||
paths: ['title', 'categories', 'price_info', 'color_info'], | ||
}, | ||
}; | ||
|
||
const product2 = { | ||
id: id2 ? id2 : Math.random().toString(36).slice(2).toUpperCase(), | ||
title: 'Android Embroidered Crewneck Sweater', | ||
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Android+Embroidered+Crewneck+Sweater', | ||
brands: ['Android'], | ||
categories: ['Apparel'], | ||
priceInfo: { | ||
price: 35.0, | ||
originalPrice: 45.0, | ||
cost: 12.0, | ||
currencyCode: 'USD', | ||
}, | ||
colorInfo: { | ||
colorFamilies: ['Blue'], | ||
colors: ['Sky blue'], | ||
}, | ||
fulFillmentInfo: { | ||
type: 'pickup-in-store', | ||
placeIds: ['store2', 'store3'], | ||
}, | ||
retrievable_fields: { | ||
paths: ['title', 'categories', 'price_info', 'color_info'], | ||
}, | ||
}; | ||
|
||
// The desired input location of the data. | ||
const inputConfig = { | ||
productInlineSource: { | ||
products: [product1, product2], | ||
}, | ||
}; | ||
|
||
const IResponseParams = { | ||
IImportProductsResponse: 0, | ||
IImportMetadata: 1, | ||
IOperation: 2, | ||
}; | ||
|
||
// Instantiates a client. | ||
const retailClient = new ProductServiceClient(); | ||
|
||
const callImportProducts = async () => { | ||
// Construct request | ||
const request = { | ||
parent, | ||
inputConfig, | ||
}; | ||
console.log('Import products request:', request); | ||
|
||
// Run request | ||
const [operation] = await retailClient.importProducts(request); | ||
const response = await operation.promise(); | ||
const result = response[IResponseParams.IImportMetadata]; | ||
console.log( | ||
`Number of successfully imported products: ${result.successCount | 0}` | ||
); | ||
console.log( | ||
`Number of failures during the importing: ${result.failureCount | 0}` | ||
); | ||
console.log(`Operation result: ${JSON.stringify(response)}`); | ||
}; | ||
// Start import products | ||
console.log('Start import products'); | ||
await callImportProducts(); | ||
console.log('Import products finished'); | ||
|
||
// Delete imported products | ||
await utils.deleteProductsByIds(projectNumber, [product1.id, product2.id]); | ||
console.log('Products deleted'); | ||
// [END retail_import_products_from_inline_source] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(...process.argv.slice(2)); |
46 changes: 46 additions & 0 deletions
46
retail/interactive-tutorials/setup/create-bigquery-table.js
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,46 @@ | ||
// Copyright 2022 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. | ||
|
||
'use strict'; | ||
|
||
async function main() { | ||
const utils = require('./setup-cleanup'); | ||
|
||
const dataset = 'products'; | ||
const validTable = 'products'; | ||
const invalidTable = 'products_some_invalid'; | ||
const schema = 'interactive-tutorials/resources/product_schema.json'; | ||
const validSourceFile = 'interactive-tutorials/resources/products.json'; | ||
const invalidSourceFile = | ||
'interactive-tutorials/resources/products_some_invalid.json'; | ||
|
||
await utils.createBqDataset(dataset); | ||
await utils.createBqTable(dataset, validTable, schema); | ||
await utils.uploadDataToBqTable(dataset, validTable, validSourceFile, schema); | ||
|
||
await utils.createBqTable(dataset, invalidTable, schema); | ||
await utils.uploadDataToBqTable( | ||
dataset, | ||
validTable, | ||
invalidSourceFile, | ||
schema | ||
); | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
|
||
main(); |
Oops, something went wrong.