从Google Apps脚本的Fusion Tables中查询大数据
问题描述:
我已经将66 MB csv文件加载到Fusion Tables。它大约有475k排,12列宽。从Google Apps脚本的Fusion Tables中查询大数据
我正在使用Google Apps脚本并尝试查询其中的数据。
其中一列是数据所属人员的姓名,例如Joe。
如果我想要把所有乔的数据出来,所以我可以在一个不错的形式显示给他,我正在使用此查询:
var tableId = my_table_id;
var sql1 = "SELECT * FROM " + tableId + " WHERE 'User' = 'Joe'";
var result = FusionTables.Query.sql(sql1,{hdrs : false});
的问题是,乔有大约52K的线数据。我想返回它,以便我可以将它加载到数据集,用户可以对它进行排序并查看所有数据。我得到两个错误之一:
如果我运行查询如上获得:
- 响应码:413消息:响应太大。
如果我只是尝试选择它的所有(SELECT * FROM TABLEID),我得到:
- 响应的大小大于10 MB。请使用媒体下载
对于媒体下载,我试过在参数中指定alt:'media',但我认为这不适用于Google Apps脚本(我无法在任何地方找到文档)。
我也曾尝试通过查询循环,所以选择*极限0,1000,然后选择*极限1001,2000,等。但是,融合表SQL似乎也不支持这一点。
此时,我可能会将CSV保留在我的驱动器中,并在飞行中对其进行解析,但这是我的最后手段。任何意见,将不胜感激!
答
所以我想我明白了这一点。我敢肯定,这不是最完美的解决方案,但在这里有云:
我运行一个快速查询以检查计数()乔,看看有多少记录有且仅当需要的运行循环。我最大设置到40000条记录:
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
}
要注意的事情就是如果用户名是两个词,例如“约翰·史密斯”,你可能需要在引号周围添加您的用户名,所以不是
var total_rows_query = "SELECT COUNT() FROM " + tableId + " WHERE 'User' = " + username;
这将是:
var total_rows_query = "SELECT COUNT() FROM " + tableId + " WHERE 'User' = '" + username + "'";
我花的最后两天试图弄清楚这一点,所以我希望这有助于有人出来!