|
| 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 | + // Imports the Google Cloud client library. |
| 19 | + const {UserEventServiceClient} = require('@google-cloud/retail').v2; |
| 20 | + |
| 21 | + // Instantiates a client. |
| 22 | + const retailClient = new UserEventServiceClient(); |
| 23 | + |
| 24 | + const projectId = await retailClient.getProjectId(); |
| 25 | + |
| 26 | + //TODO(developer) set the environment variable value which will be used as the bucket name |
| 27 | + const gcsBucket = `gs://${bucketName}`; |
| 28 | + const gcsErrorsBucket = `gs://${bucketName}/error`; |
| 29 | + const gcsEventsObject = 'user_events.json'; // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID USER EVENTS |
| 30 | + |
| 31 | + // Placement |
| 32 | + const parent = `projects/${projectId}/locations/global/catalogs/default_catalog`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE |
| 33 | + |
| 34 | + // The desired input location of the data. |
| 35 | + const inputConfig = { |
| 36 | + gcsSource: { |
| 37 | + inputUris: [gcsBucket + '/' + gcsEventsObject], |
| 38 | + dataSchema: 'user_event', |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + // The desired location of errors incurred during the Import. |
| 43 | + const errorsConfig = { |
| 44 | + gcsPrefix: gcsErrorsBucket, |
| 45 | + }; |
| 46 | + |
| 47 | + const IResponseParams = { |
| 48 | + IImportUserEventsResponse: 0, |
| 49 | + IImportMetadata: 1, |
| 50 | + IOperation: 2, |
| 51 | + }; |
| 52 | + |
| 53 | + const callImportUserEvents = async () => { |
| 54 | + // Construct request |
| 55 | + const request = { |
| 56 | + parent, |
| 57 | + inputConfig, |
| 58 | + errorsConfig, |
| 59 | + }; |
| 60 | + |
| 61 | + console.log('Import request: ', request); |
| 62 | + |
| 63 | + // Run request |
| 64 | + const [operation] = await retailClient.importUserEvents(request); |
| 65 | + const response = await operation.promise(); |
| 66 | + const result = response[IResponseParams.IImportMetadata]; |
| 67 | + console.log( |
| 68 | + `Number of successfully imported events: ${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 | + |
| 76 | + console.log('Start events import'); |
| 77 | + await callImportUserEvents(); |
| 78 | + console.log('Events import finished'); |
| 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['EVENTS_BUCKET_NAME']]; |
| 90 | + })() |
| 91 | +); |
0 commit comments