新しいファイルがGCSに到達するたびに、avroファイルをGCSからBigQueryに移動するクラウド関数の作成に取り組んでいます。GCPでクラウド機能のUIを使用しています。メモリ用に512MBが割り当てられています。トリガーはGoogleストレージです。ファイナライズ/作成の場合のイベントタイプ。ソースコードはインラインエディタです。
以下は私のコードです。正常にデプロイできますが、デプロイ後に以下のエラーが発生し、BigQueryに何も移動しません。
さらに、バケット内のフォルダーからavroファイルを移動しようとしているため、最上位の親バケットから直接プルしていません。これが、「example_spend /」というフォルダに入ろうとする次のコードの目的です。
エラー:モジュールgoogle-cloud / bigqueryが見つかりません
'use strict';
exports.createExampleTableFromFile = function(event, callback) {
const file = event.data;
if (file.resourceState === 'exists' && file.name &&
file.name.indexOf('example_spend/') !== -1) {
console.log('Processing file: ' + event.data.name);
const BigQuery = require('@google-cloud/bigquery');
const Storage = require('@google-cloud/storage');
const assert = require('assert');
const filename = event.data.name;
const bucketName = event.data.bucket;
const projectId = "gcp-pilot-192921";
const datasetId = "example_etl";
const tableId = filename.slice(0,filename.indexOf(".")).toLowerCase();
const bigquery = new BigQuery({
projectId: projectId,
});
const storage = Storage({
projectId: projectId
});
const metadata = {
sourceFormat: 'AVRO',
autodetect: true,
writeDisposition: 'WRITE_TRUNCATE'
};
bigquery
.dataset(datasetId)
.table(tableId)
.load(storage.bucket(bucketName).file(filename), metadata)
.then(results => {
const job = results[0];
assert.equal(job.status.state, 'DONE');
console.log(`Job ${job.id} to load table ${tableId} completed.`);
const errors = job.status.errors;
if (errors && errors.length > 0) {
throw errors;
}
})
.catch(err => {
console.error('Error during load job:', err);
});
callback();
}};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加