Skip to content

Commit 27428ad

Browse files
authored
chore(samples): interactive tutorials code samples for import products (#147)
1 parent 1173bb0 commit 27428ad

9 files changed

Lines changed: 661 additions & 0 deletions
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
// [START retail_import_products_from_big_query]
19+
20+
// Imports the Google Cloud client library.
21+
const {ProductServiceClient} = require('@google-cloud/retail').v2;
22+
23+
const projectNumber = process.env['GCLOUD_PROJECT'];
24+
const projectId = process.env['PROJECT_ID'];
25+
26+
const datasetId = 'products';
27+
const tableId = 'products'; // TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS
28+
const dataSchema = 'product';
29+
30+
// Placement
31+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
32+
33+
// The desired input location of the data.
34+
const inputConfig = {
35+
bigQuerySource: {
36+
projectId,
37+
datasetId,
38+
tableId,
39+
dataSchema,
40+
},
41+
};
42+
43+
const reconciliationModes = {
44+
RECONCILIATION_MODE_UNSPECIFIED: 0,
45+
INCREMENTAL: 1,
46+
FULL: 2,
47+
};
48+
49+
const IResponseParams = {
50+
IImportProductsResponse: 0,
51+
IImportMetadata: 1,
52+
IOperation: 2,
53+
};
54+
55+
// The mode of reconciliation between existing products and the products to be imported.
56+
const reconciliationMode = reconciliationModes.INCREMENTAL;
57+
58+
// Instantiates a client.
59+
const retailClient = new ProductServiceClient();
60+
61+
const callImportProducts = async () => {
62+
// Construct request
63+
const request = {
64+
parent,
65+
inputConfig,
66+
reconciliationMode,
67+
};
68+
console.log('Import product request:', request);
69+
70+
// Run request
71+
const [operation] = await retailClient.importProducts(request);
72+
const response = await operation.promise();
73+
const result = response[IResponseParams.IImportMetadata];
74+
console.log(
75+
`Number of successfully imported products: ${result.successCount | 0}`
76+
);
77+
console.log(
78+
`Number of failures during the importing: ${result.failureCount | 0}`
79+
);
80+
console.log(`Operation result: ${JSON.stringify(response)}`);
81+
};
82+
83+
console.log('Start import products');
84+
await callImportProducts();
85+
console.log('Import products finished');
86+
// [END retail_import_products_from_big_query]
87+
}
88+
89+
process.on('unhandledRejection', err => {
90+
console.error(err.message);
91+
process.exitCode = 1;
92+
});
93+
94+
main();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(bucketName) {
18+
// [START retail_import_products_from_gcs]
19+
20+
// Imports the Google Cloud client library.
21+
const {ProductServiceClient} = require('@google-cloud/retail').v2;
22+
23+
const projectNumber = process.env['GCLOUD_PROJECT'];
24+
25+
const gcsBucket = `gs://${bucketName}`;
26+
const gcsErrorsBucket = `gs://${bucketName}/error`;
27+
const gcsProductsObject = 'products.json'; // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT
28+
29+
// Placement
30+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`; //TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
31+
32+
// The desired input location of the data.
33+
const inputConfig = {
34+
gcsSource: {
35+
inputUris: [gcsBucket + '/' + gcsProductsObject],
36+
dataSchema: 'product',
37+
},
38+
};
39+
40+
// The desired location of errors incurred during the Import.
41+
const errorsConfig = {
42+
gcsPrefix: gcsErrorsBucket,
43+
};
44+
45+
const IResponseParams = {
46+
IImportProductsResponse: 0,
47+
IImportMetadata: 1,
48+
IOperation: 2,
49+
};
50+
51+
// Instantiates a client.
52+
const retailClient = new ProductServiceClient();
53+
54+
const callImportProducts = async () => {
55+
// Construct request
56+
const request = {
57+
parent,
58+
inputConfig,
59+
errorsConfig,
60+
};
61+
console.log('Import products request:', request);
62+
63+
// Run request
64+
const [operation] = await retailClient.importProducts(request);
65+
const response = await operation.promise();
66+
const result = response[IResponseParams.IImportMetadata];
67+
console.log(
68+
`Number of successfully imported products: ${result.successCount | 0}`
69+
);
70+
console.log(
71+
`Number of failures during the importing: ${result.failureCount | 0}`
72+
);
73+
console.log(`Operation result: ${JSON.stringify(response)}`);
74+
};
75+
console.log('Start import products');
76+
await callImportProducts();
77+
console.log('Import products finished');
78+
// [END retail_import_products_from_gcs]
79+
}
80+
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
86+
main(
87+
...(() => {
88+
const argv = process.argv.slice(2);
89+
return argv.length ? argv : [process.env['BUCKET_NAME']];
90+
})()
91+
);
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(id1, id2) {
18+
// [START retail_import_products_from_inline_source]
19+
20+
// Imports the Google Cloud client library.
21+
const {ProductServiceClient} = require('@google-cloud/retail').v2;
22+
const utils = require('../setup/setup-cleanup');
23+
24+
const projectNumber = process.env['GCLOUD_PROJECT'];
25+
26+
// Placement
27+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog/branches/default_branch`;
28+
29+
const product1 = {
30+
id: id1 ? id1 : Math.random().toString(36).slice(2).toUpperCase(),
31+
title: '#IamRemarkable Pen', //TO CHECK ERROR HANDLING COMMENT OUT THE PRODUCT TITLE HERE
32+
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Office/IamRemarkable+Pen',
33+
brands: ['#IamRemarkable'],
34+
categories: ['Apparel'],
35+
priceInfo: {
36+
price: 16.0,
37+
originalPrice: 45.0,
38+
cost: 12.0,
39+
currencyCode: 'USD',
40+
},
41+
colorInfo: {
42+
colorFamilies: ['Blue'],
43+
colors: ['Light blue', 'Blue', 'Dark blue'],
44+
},
45+
fulFillmentInfo: {
46+
type: 'pickup-in-store',
47+
placeIds: ['store1', 'store2'],
48+
},
49+
retrievable_fields: {
50+
paths: ['title', 'categories', 'price_info', 'color_info'],
51+
},
52+
};
53+
54+
const product2 = {
55+
id: id2 ? id2 : Math.random().toString(36).slice(2).toUpperCase(),
56+
title: 'Android Embroidered Crewneck Sweater',
57+
uri: 'https://shop.googlemerchandisestore.com/Google+Redesign/Apparel/Android+Embroidered+Crewneck+Sweater',
58+
brands: ['Android'],
59+
categories: ['Apparel'],
60+
priceInfo: {
61+
price: 35.0,
62+
originalPrice: 45.0,
63+
cost: 12.0,
64+
currencyCode: 'USD',
65+
},
66+
colorInfo: {
67+
colorFamilies: ['Blue'],
68+
colors: ['Sky blue'],
69+
},
70+
fulFillmentInfo: {
71+
type: 'pickup-in-store',
72+
placeIds: ['store2', 'store3'],
73+
},
74+
retrievable_fields: {
75+
paths: ['title', 'categories', 'price_info', 'color_info'],
76+
},
77+
};
78+
79+
// The desired input location of the data.
80+
const inputConfig = {
81+
productInlineSource: {
82+
products: [product1, product2],
83+
},
84+
};
85+
86+
const IResponseParams = {
87+
IImportProductsResponse: 0,
88+
IImportMetadata: 1,
89+
IOperation: 2,
90+
};
91+
92+
// Instantiates a client.
93+
const retailClient = new ProductServiceClient();
94+
95+
const callImportProducts = async () => {
96+
// Construct request
97+
const request = {
98+
parent,
99+
inputConfig,
100+
};
101+
console.log('Import products request:', request);
102+
103+
// Run request
104+
const [operation] = await retailClient.importProducts(request);
105+
const response = await operation.promise();
106+
const result = response[IResponseParams.IImportMetadata];
107+
console.log(
108+
`Number of successfully imported products: ${result.successCount | 0}`
109+
);
110+
console.log(
111+
`Number of failures during the importing: ${result.failureCount | 0}`
112+
);
113+
console.log(`Operation result: ${JSON.stringify(response)}`);
114+
};
115+
// Start import products
116+
console.log('Start import products');
117+
await callImportProducts();
118+
console.log('Import products finished');
119+
120+
// Delete imported products
121+
await utils.deleteProductsByIds(projectNumber, [product1.id, product2.id]);
122+
console.log('Products deleted');
123+
// [END retail_import_products_from_inline_source]
124+
}
125+
126+
process.on('unhandledRejection', err => {
127+
console.error(err.message);
128+
process.exitCode = 1;
129+
});
130+
131+
main(...process.argv.slice(2));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
const utils = require('./setup-cleanup');
19+
20+
const dataset = 'products';
21+
const validTable = 'products';
22+
const invalidTable = 'products_some_invalid';
23+
const schema = 'interactive-tutorials/resources/product_schema.json';
24+
const validSourceFile = 'interactive-tutorials/resources/products.json';
25+
const invalidSourceFile =
26+
'interactive-tutorials/resources/products_some_invalid.json';
27+
28+
await utils.createBqDataset(dataset);
29+
await utils.createBqTable(dataset, validTable, schema);
30+
await utils.uploadDataToBqTable(dataset, validTable, validSourceFile, schema);
31+
32+
await utils.createBqTable(dataset, invalidTable, schema);
33+
await utils.uploadDataToBqTable(
34+
dataset,
35+
validTable,
36+
invalidSourceFile,
37+
schema
38+
);
39+
}
40+
41+
process.on('unhandledRejection', err => {
42+
console.error(err.message);
43+
process.exitCode = 1;
44+
});
45+
46+
main();

0 commit comments

Comments
 (0)