用于从Google电子表格加载数据到bigquery的独立脚本



我写了一个独立的脚本,从csv文件加载数据到bigquery,但我想知道如何从电子表格加载数据到bigquery。这是我的代码加载数据从csv到bigquery。我想知道在哪里改变谷歌电子表格而不是csv文件。

function loadCsv() {
  // Replace this value with the project ID listed in the Google
  // Developers Console project.
  var projectId = '775034665452';
  var datasetId = 'DEV';
  // Sample CSV file of Google Trends data conforming to the schema below.
  // https://docs.google.com/file/d/0BwzA1Orbvy5WMXFLaTR1Z1p2UDg/edit
  var csvFileId = '0BwzA1Orbvy5WMXFLaTR1Z1p2UDg';
  // Create the table.
  var tableId = 'pets_' + new Date().getTime();
  var table = {
    tableReference: {
      projectId: projectId,
      datasetId: datasetId,
      tableId: tableId
    },
    schema: {
      fields: [
        {name: 'week', type: 'STRING'},
        {name: 'cat', type: 'INTEGER'},
        {name: 'dog', type: 'INTEGER'},
        {name: 'bird', type: 'INTEGER'}
      ]
    }
  };
  table = BigQuery.Tables.insert(table, projectId, datasetId);
  Logger.log('Table created: %s', table.id);
  // Load CSV data from Drive and convert to the correct format for upload.
  var file = DriveApp.getFileById(csvFileId);
  var data = file.getBlob().setContentType('application/octet-stream');
  // Create the data upload job.
  var job = {
    configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        },
        skipLeadingRows: 1
      }
    }
  };
  job = BigQuery.Jobs.insert(job, projectId, data);
  Logger.log('Load job started. Check on the status of it here: ' +
      'https://bigquery.cloud.google.com/jobs/%s', projectId);
}

在运行查询之前,必须先将数据加载到BigQuery中。

您可以通过以下方式加载数据:

  • 从Google Cloud Storage加载,包括CSV, JSON(换行分隔)和Avro文件,以及Google Cloud Datastore备份。
  • 直接从可读的数据源加载
  • 使用流插入插入单个记录

加载的数据可以添加到新表中,附加到表中,或者覆盖表。

BigQuery支持从多种源格式加载数据,包括CSV、JSON、Avro和Google Cloud Datastore备份文件。有关详细信息,请参见数据格式。

加载CSV文件,检查此:https://cloud.google.com/bigquery/loading-data#loading_csv_files