【问题标题】:Double Quoted data error in BigQuery CSV uploadBigQuery CSV 上传中的双引号数据错误
【发布时间】:2018-09-26 16:11:39
【问题描述】:

在将数据从 Google 表格上传到 BigQuery 时,“cmets”字段包含如下数据

function pushToBQ(projectId, datasetId, tableId) {
  var fileId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

  var jobSpec = { configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        },
        allowJaggedRows: true,
        writeDisposition: 'WRITE_TRUNCATE',
        allowQuotedNewlines: true,
        schema: {
          fields: [ 
            {name: 'User_id', type: 'STRING'},
            {name: 'email', type: 'STRING'},
            {name: 'Comments', type: 'STRING'},
          ] 
        }
      }
    }
  };

  var spreadsheet = SpreadsheetApp.openById(fileId);
  var MAX_ROWS = 50000;
  var sheet = spreadsheet.getSheetByName("xyz");
  var data = sheet.getDataRange().getValues();
  var csvdata = "";
  for (var row = 1; row < data.length && row < MAX_ROWS + 1; row++) {
    for (var col = 0; col < data[row].length; col++) {
      var cell = data[row][col].toString();
      if (cell.indexOf(",") != -1) {
        csvdata += "\"" + cell + "\"";
      } else {
        csvdata += cell;
      }

      if (col < data[row].length - 1) {
         csvdata += ",";
      }
    }
    csvdata += "\r\n";

  }

  var data = Utilities.newBlob(csvdata, "application/octet-stream");
  BigQuery.Jobs.insert(jobSpec, projectId, data);
}

function daily_upload(){
  pushToBQ("dev", "sampledataset",'sampletable');
}

请提供解决方案,我想按原样上传 cmets,而不用空格替换它们。

【问题讨论】:

  • 您遇到此问题是因为您没有对原始评论字段中的引号进行转义。你必须这样做。
  • 请查看相关的 BigQuery 问题并说明 1) 您遇到的确切错误,以及 2) 您尝试解决此问题的内容以及尝试的结果。

标签: google-apps-script google-bigquery


【解决方案1】:

除了从工作表创建 CSV 然后将该 CSV 导入 BigQuery 之外的选项:

  1. Read from your sheet in BigQuery by creating an 'external table'(要求数据位于工作表的第一个选项卡中)。然后,您可以从该外部表复制到真正的 bigquery 表。

  2. 使用 BigQuery.Tabledata.insertAll() 将数据以 JSON 格式插入到表中,而不必格式化 CSV。

【讨论】:

    猜你喜欢
    • 2017-10-01
    • 2021-06-07
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2018-11-11
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多