【问题标题】:Query Large Data from Fusion Tables from Google Apps Script通过 Google Apps 脚本从 Fusion Tables 中查询大数据
【发布时间】:2017-10-02 15:53:51
【问题描述】:

我已将 66 MB 的 csv 文件加载到 Fusion Tables。它大约有 475k 行长和 12 列宽。

我正在使用 Google Apps 脚本并尝试查询其中的数据。

其中一列是该数据所属人员的姓名,例如 Joe。

如果我想提取 Joe 的所有数据,以便以一种很好的格式显示给他,我正在使用这个查询:

var tableId = my_table_id;
var sql1 = "SELECT * FROM " + tableId + " WHERE 'User' = 'Joe'";
var result = FusionTables.Query.sql(sql1,{hdrs : false});

问题是 Joe 有大约 52k 行数据。我想将其返回,以便将其加载到数据表中,用户可以对其进行排序并查看所有数据。我收到以下两个错误之一:

如果我像上面那样运行查询,我会得到:

  • 响应代码:413。消息:响应太大。

如果我只是尝试全选(SELECT * FROM tableId),我会得到:

  • 响应大小大于 10 MB。请使用媒体下载

对于媒体下载,我尝试在参数中指定 alt : 'media',但我认为这在 Google Apps 脚本中不起作用(我在任何地方都找不到相关文档)。

我也试过循环查询,所以选择 *limit 0,1000,然后选择 *limit 1001,2000,等等。但是,融合表 SQL 似乎也不支持。

此时,我可能只是将 CSV 留在我的驱动器中,即时解析它,但这是我最后的手段。任何建议将不胜感激!

【问题讨论】:

    标签: google-apps-script google-fusion-tables


    【解决方案1】:

    所以我想我想通了。我敢肯定这不是最优雅的解决方案,但这里是:

    我运行一个快速查询来检查 Joe 的 count() 以查看有多少记录,并且仅在需要时运行循环。我将最大值设置为 40,000 条记录:

      var total_rows_query = "SELECT COUNT() FROM " + tableId + " WHERE 'User' = " + username;
      var total_rows = FusionTables.Query.sql(total_rows_query,{hdrs : false}).rows[0][0];
    

    如果总行数大于我想要的,我使用 OFFSET 和 LIMIT 参数来构造查询:

    max_rows = 40000;
    if(total_rows > max_rows){
    var counter = 0;
    //adding in a zero to the ranges since the last query will be the offset of 0, meaning all of them
    var ranges = [0]
    
    while(counter + chunk_size < total_rows){
    counter = counter + chunk_size;
    ranges.push(counter)
    }
    ranges.push(total_rows)
    
    //Now ranges is an array with zero at the beginning, and counting up by the chunk size I want, ending with the total_rows for the user as the last oen
    
    //This is the array that will be output after concating
    var output = []
    
    //looping through the array, setting the offset to the first item, and the limit to the next item minus the first
    for(i=0;i<ranges.length-1;i++){
    var offset = ranges[i]
      var limit = ranges[i+1] - offset
    
      var query = "SELECT * FROM " + tableId + " WHERE 'User' = '" + username + "' OFFSET " + offset + " LIMIT " + limit;
    output = output.concat(FusionTables.Query.sql(query,{hdrs : false}).rows)
    }
    
    }else{
    //if the count is less or equal to the chunk size, just run the one query
    var query = "SELECT * FROM " + tableId + " WHERE 'User' = " + username;
        var output = FusionTables.Query.sql(query,{hdrs : false}).rows
    }
    

    最后要注意的是,如果用户名是两个词,例如“John Smith”,您可能需要在用户名周围添加引号,所以不要使用

    var total_rows_query = "SELECT COUNT() FROM " + tableId + " WHERE 'User' = " + username;
    

    应该是:

    var total_rows_query = "SELECT COUNT() FROM " + tableId + " WHERE 'User' = '" + username + "'";
    

    我花了最后两天试图解决这个问题,所以我希望它可以帮助那里的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多