【问题标题】:BIGQUERY csv file load with an additional column with a default valueBIGQUERY csv 文件加载带有默认值的附加列
【发布时间】:2021-03-18 12:51:06
【问题描述】:

根据 Google 给出的示例,我已成功按照指南(链接和代码如下)将 CSV 文件加载到 BigQuery(BQ) 表中
现在我想在 BQ 中添加几个文件,并想添加一个包含文件名的新列 filename

有没有办法用默认数据添加列?

https://cloud.google.com/bigquery/docs/loading-data-cloud-storage-csv

// Import the Google Cloud client libraries
const {BigQuery} = require('@google-cloud/bigquery');
const {Storage} = require('@google-cloud/storage');

// Instantiate clients
const bigquery = new BigQuery();
const storage = new Storage();

/**
 * This sample loads the CSV file at
 * https://storage.googleapis.com/cloud-samples-data/bigquery/us-states/us-states.csv
 *
 * TODO(developer): Replace the following lines with the path to your file.
 */
const bucketName = 'cloud-samples-data';
const filename = 'bigquery/us-states/us-states.csv';

async function loadCSVFromGCS() {
  // Imports a GCS file into a table with manually defined schema.

  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const datasetId = 'my_dataset';
  // const tableId = 'my_table';

  // Configure the load job. For full list of options, see:
  // https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationLoad
  const metadata = {
    sourceFormat: 'CSV',
    skipLeadingRows: 1,
    schema: {
      fields: [
        {name: 'name', type: 'STRING'},
        {name: 'post_abbr', type: 'STRING'},
//      {name: 'filemame', type: 'STRING', value=filename} // I WANT TO ADD COLUMN WITH FILE NAME HERE
      ],
    },
    location: 'US',
  };

  // Load data from a Google Cloud Storage file into the table
  const [job] = await bigquery
    .dataset(datasetId)
    .table(tableId)
    .load(storage.bucket(bucketName).file(filename), metadata);

  // load() waits for the job to finish
  console.log(`Job ${job.id} completed.`);

  // Check the job's status for errors
  const errors = job.status.errors;
  if (errors && errors.length > 0) {
    throw errors;
  }
}

【问题讨论】:

    标签: google-cloud-platform google-bigquery


    【解决方案1】:

    根据 BigQuery 的文档 [1],没有为列设置默认值的选项。没有任何后处理的最接近的选择是对可空列使用 NULL 值。

    但是,对此可能的后处理解决方法是创建原始表的视图并添加将 NULL 值映射到任何默认值的脚本。以下是有关 BigQuery [2] 中的脚本编写的一些信息。

    如果可以添加预处理代码,则使用任何脚本语言都可以轻松地向源文件添加值。

    我认为静态和基于函数的值将是 BigQuery 未来范围的一个很好的功能。

    [1] - https://cloud.google.com/bigquery/docs

    [2] - https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting

    【讨论】:

      【解决方案2】:

      我会说你有几个选择。

      1. 在上传前向 CSV 添加一列,例如with awk 或 JS 中的预处理。
      2. 将单个 CSV 文件添加到单独的表中。您可以轻松地将 query across many tables 作为 BigQuery 中的一员。这样您可以轻松查看哪些数据来自哪个文件,并且您可以访问文件名的table meta 数据
      3. 对数据进行后处理,方法是在使用正常的 sql/api 调用加载数据后添加列。
      4. 另请参阅此可能重复的How to add new column with metadata value to csv when loading it to bigquery

      【讨论】:

      • 谢谢。我会选择选项 2。
      【解决方案3】:

      您有多种选择:

      1. 您可以将文件名作为列数据重建 CSV
      2. 您可以将数据加载到临时表中,然后通过指定缺少的文件名列的第二步移至最终表
      3. 将示例转换为外部表,其中_FILE_NAME 是伪列,稍后您可以查询并移动到最终表。查看更多有关此here 的信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-18
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        相关资源
        最近更新 更多