【问题标题】:Understanding Big Query Tables了解大查询表
【发布时间】:2018-11-12 22:06:04
【问题描述】:

我仍在学习大查询,我对数据集和表的工作方式有一些疑问。我运行了一个查询并将结果保存到 BigQuery 表中。现在,该表是我提取的数据的快照,还是会在新数据适合原始查询时更新该表?

如果是快照。任何人都可以提供一些帮助,以使用 Nodejs 以编程方式更新/替换 BigQuery 表中的数据。

感谢您的帮助

【问题讨论】:

    标签: node.js google-cloud-platform google-bigquery


    【解决方案1】:

    表格是“快照”,不会自动更新。关于使用 Node 以编程方式更新/替换表,我建议为此创建一个单独的问题,并尝试提供尽可能多的细节。但是,Node GitHub repo 上的示例可能足以让您入门。 For example:

    function loadCSVFromGCS(datasetId, tableId, projectId) {
      // [START bigquery_load_table_gcs_csv]
      // Imports the Google Cloud client libraries
      const {BigQuery} = require('@google-cloud/bigquery');
      const {Storage} = require('@google-cloud/storage');
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const projectId = "your-project-id";
      // const datasetId = "my_dataset";
      // const tableId = "my_table";
    
      /**
       * 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';
    
      // Instantiates clients
      const bigquery = new BigQuery({
        projectId: projectId,
      });
    
      const storage = new Storage({
        projectId: projectId,
      });
    
      // Configure the load job. For full list of options, see:
      // https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load
      const metadata = {
        sourceFormat: 'CSV',
        skipLeadingRows: 1,
        schema: {
          fields: [
            {name: 'name', type: 'STRING'},
            {name: 'post_abbr', type: 'STRING'},
          ],
        },
      };
    
      // Loads data from a Google Cloud Storage file into the table
      bigquery
        .dataset(datasetId)
        .table(tableId)
        .load(storage.bucket(bucketName).file(filename), metadata)
        .then(results => {
          const job = results[0];
    
          // 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;
          }
        })
        .catch(err => {
          console.error('ERROR:', err);
        });
      // [END bigquery_load_table_gcs_csv]
    }
    

    【讨论】:

    • 听起来不错,谢谢。我将创建另一个问题。我确实遇到了您提供的文档。这实际上是产生问题的原因。使用此代码,我能够从通过 GCP UI 创建的表中导出 CSV,但由于该表是“快照”,如果我想定期导出 CSV,我需要在导出之前更新/替换表.但我不知道如何使用节点来做到这一点。
    • 太棒了。当答案对你有帮助时,接受答案很重要。您可以为自动化创建另一个问题:)
    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多