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

Posted

技术标签:

【中文标题】用于将数据从谷歌电子表格加载到 bigquery 的独立脚本【英文标题】:standalone script for loading data from google spreadsheet to bigquery 【发布时间】:2016-08-26 07:15:50 【问题描述】:

我编写了一个独立脚本,用于将数据从 csv 文件加载到 bigquery,但我想知道如何将数据从电子表格加载到 bigquery。这是我将数据从 csv 加载到 bigquery 的代码。我想知道在哪里对 google 电子表格而不是 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);

【问题讨论】:

【参考方案1】:

您必须先将数据加载到 BigQuery 中,然后才能运行查询。

您可以通过以下方式load data:

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

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

BigQuery 支持从多种源格式加载数据,包括 CSV、JSON、Avro 和 Google Cloud Datastore 备份文件。如需更多信息,请参阅Data Formats。

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

【讨论】:

感谢您的信息!!但我想知道我们是否可以编写一个脚本来将数据从电子表格加载到 bigquery,而不是从 GCS 或手动加载。 是的,请检查此文档伙伴:cloud.google.com/blog/big-data/2016/05/…

以上是关于用于将数据从谷歌电子表格加载到 bigquery 的独立脚本的主要内容,如果未能解决你的问题,请参考以下文章

从谷歌电子表格将数据导入R

是否使用数据流管道(未内置自定义)从谷歌存储到 bigquery 摄取数据(批量)?

如何使用 BigQuery 连接器将自定义查询从谷歌数据工作室传递到 BigQuery?

javascript 一个应用程序脚本示例,用于从谷歌电子表格中获取大查询数据。更多信息:http://wp.me/pB1lQ-19i

如何在谷歌大查询中从谷歌云存储上传表格

如何从谷歌电子表格中获取 json 数据