【问题标题】:how to handle mysql result query to send them over big query table如何处理 mysql 结果查询以通过大查询表发送它们
【发布时间】:2019-10-29 02:50:11
【问题描述】:

我正在编写一个 Google Apps 脚本以将数据从 Mysql 数据库发送到 Big Query 表。

我知道如何查询 mysql 数据库。我知道如何将 csv 文件或 Google 表格中的数据写入大查询。但是,我希望能够将 mysql 查询结果直接发送到 BigQuery。我怎样才能做到这一点?

基本上,我的 SQL 结果存储在一个变量中:

var results = stmt.executeQuery('select * from table');

表格很基本:

Col1        Col2     Col3
String 1    foo1     1
String 2    foo2     2
String 3    foo3     3
etc...      etc...   etc...

从 Big Query 端脚本我有这个:

var data = results.getBlob().setContentType('application/octet-stream');

显然,在运行我的脚本时出现以下错误:

Cannot find function getBlob in object

我不太明白如何访问results 变量中的数据,也不知道如何将它们格式化为可读格式以执行以下.getBlob().setContentType('application/octet-stream')

到目前为止,我已经尝试通过使用以下循环的 push 方法将 results 值传递到名为 rawdata 的新变量中:

  while (results.next()) {
    var rowString = '';
    for (var col = 0; col < numCols; col++) {
      rowString += results.getString(col + 1) + '\t';
    }

    Logger.log(rowString);
    rawdata.push(rowString)
  }

但是rawdata.getBlob().setContentType('application/octet-stream'); 给我返回同样的错误。

我不知道如何格式化我的 rawdata 或 results 变量。

【问题讨论】:

标签: mysql jdbc google-apps-script google-bigquery


【解决方案1】:

问题:

  • 尝试使用 getBlob 从字符串/数组中获取 blob,而这两种方法均不可用。
  • 使用\t 创建csv。

解决办法:

  • csv,默认使用逗号,作为字段分隔符,换行符\n作为行分隔符
  • 您需要使用实用程序创建一个新的 blob。

片段:

  var rawdata = [];
  while (results.next()) {
    var rowString = '';
    for (var col = 0; col < numCols; col++) {
      rowString += '"' + results.getString(col + 1) + '",';//quotes " if present in results data, must be replaced with double quotes ""
    }
    rawdata.push(rowString.slice(0, -1))
  }
 var data = Utilities.newBlob(rawdata.join('\n'),'application/octet-stream');

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多