了解大查询表

时间:2018-11-12 13:05:56

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

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

如果是快照。谁能在使用Node.js编程地更新/替换BigQuery表中的数据时提供任何帮助。

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

该表是一个“快照” ,不会自动更新。关于使用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]
}