应用程序脚本写入Big Query未知错误

Apps script write to Big Query unknown error

本文关键字:Query 未知 错误 Big 脚本 应用程序      更新时间:2023-09-26

这应该在CSV中读取,然后将其写入bigquery。然而,当它运行时,不会写入任何内容,也不会记录任何错误。我读到我需要写一个csv,然后把它变成一个八位流。我不确定这是否与谷歌bigquery兼容。

function test(){
try{
  var tableReference = BigQuery.newTableReference();
  tableReference.setProjectId(PROJECT_ID);
  tableReference.setDatasetId(datasetId);
  tableReference.setTableId(tableId);
  var schema = "CUSTOMER:string, CLASSNUM:integer, CLASSDESC:string, CSR:string, CSR2:string, INSURANCE:string, REFERRALGENERAL:string, REFERRALSPECIFIC:string, NOTES:string, INMIN:integer, INHR:integer, OUTMIN:integer, OUTHR:integer, WAITMIN:integer, WAITHR:integer, DATETIMESTAMP:float, DATEYR:integer,DATEMONTH:integer, DATEDAY:integer";
  var load = BigQuery.newJobConfigurationLoad();
  load.setDestinationTable(tableReference);
  load.setSourceUris(URIs);
  load.setSourceFormat('NEWLINE_DELIMITED_JSON');
  load.setSchema(schema);
  load.setMaxBadRecords(0);
  load.setWriteDisposition('WRITE_TRUNCATE');
  var configuration = BigQuery.newJobConfiguration();
  configuration.setLoad(load);
  var newJob = BigQuery.newJob();
  newJob.setConfiguration(configuration);

  var loadr = DriveApp.getFilesByName("test.csv");
  var x = loadr.next().getBlob();
  Logger.log(x.getDataAsString());
  var d = DriveApp.getFilesByName("test.csv");
  var id = d.next().getId();
  Logger.log(id);
  var data = DocsList.getFileById(id).getBlob().getDataAsString();
  var mediaData = Utilities.newBlob(data, 'application/octet-stream');
  BigQuery.Jobs.insert(newJob, PROJECT_ID, mediaData) 
 }
 catch(error){Logger.log("A" + error.message);}
}

CSV文件的sourceFormat错误:

数据文件的格式。对于CSV文件,请指定"CSV"。对于数据存储备份,指定"datastore_BACKUP"。对于换行符JSON,指定"NEWLINE_DELITED_JSON"。默认值为CSV。

https://developers.google.com/bigquery/docs/reference/v2/jobs#configuration.load.sourceUris

另一方面,我认为您根本不需要load.setSourceUris(URIs);,因为您尝试从本地文件加载,而不是从谷歌云存储加载。检查这个python示例https://developers.google.com/bigquery/loading-data-into-bigquery