Felipe Hoffa使用 Hacker News API将Hacker News的数据导出到了Big Query,
https://bigquery.cloud.google.com/table/fh-bigquery:hackernews.comments

这是一个共享的表,BigQuery每天有1000次的免费查询额度,再利用Google Sheets来保存查询结果并进行数据可视化,实现一个低成本的在线大数据分析流程。

环境准备

不需要安装任何软件,只要有一个Google Account。打开以下两个页面:
https://bigquery.cloud.google.com/table/fh-bigquery:hackernews.comments
http://g.co/sheets
Google Sheets不仅提供数据保存和可视化,还集成了Googel Apps Script,天然支持BigQuery查询。
打开https://bigquery.cloud.google.com/table/fh-bigquery:hackernews.comments
进行一次简单的测试查询

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT a.month month, stories, comments, comment_authors, story_authors
FROM (
SELECT STRFTIME_UTC_USEC(time_ts, '%Y-%m') month, COUNT(*) stories, EXACT_COUNT_DISTINCT(author) story_authors
FROM [fh-bigquery:hackernews.stories]
GROUP BY 1
) a
JOIN (
SELECT STRFTIME_UTC_USEC(time_ts, '%Y-%m') month, COUNT(*) comments, EXACT_COUNT_DISTINCT(author) comment_authors
FROM [fh-bigquery:hackernews.comments]
GROUP BY 1
) b
ON a.month=b.month
ORDER BY 1

表中的记录数和表大小

查询数据

新建一个Google Sheets

命名为Play Hacker News With BigQuery

开启BigQuery支持

点击Tools -> Script Editor...,打开Google Apps Script编程界面。


点击Resouces -> Advanced Google Services...,会先弹出保存保存项目对话框,输入项目名称,点击OK

启用BigQuery支持。

点击Google Developers Console链接,在控制台上启用BigQuery。

启用API,并记录下URL中的Project ID(纯数字,图中被涂部分),获取数据时会用到。

获取数据

使用以下Googel Apps Script代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function runQuery() {
// Replace this value with your Google Developer project number (It is really a number.
// Don't confuse it with an alphanumeric project id)
var projectNumber = '替换为前边提到的ProjectID';
if (projectNumber.length < 1) {
var errMsg = "You forgot to set a project number - So no BQ for you!";
Logger.log(errMsg);
Browser.msgBox(errMsg);
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
var sql = 'SELECT a.month month, stories, comments, comment_authors, story_authors FROM (SELECT STRFTIME_UTC_USEC(time_ts, \'%Y-%m\') month, '+
'COUNT(*) stories, EXACT_COUNT_DISTINCT(author) story_authors FROM [fh-bigquery:hackernews.stories] GROUP BY 1 ) a JOIN '+
'(SELECT STRFTIME_UTC_USEC(time_ts, \'%Y-%m\') month, COUNT(*) comments, EXACT_COUNT_DISTINCT(author) comment_authors '+
'FROM [fh-bigquery:hackernews.comments] GROUP BY 1 ) b ON a.month=b.month ORDER BY 1;';
var queryResults;
// Inserts a Query Job
try {
var queryRequest = BigQuery.newQueryRequest();
queryRequest.setQuery(sql).setTimeoutMs(100000);
queryResults = BigQuery.Jobs.query(queryRequest, projectNumber);
}
catch (err) {
Logger.log(err);
Browser.msgBox(err);
return;
}
// Check on status of the Query Job
while (queryResults.getJobComplete() == false) {
try {
queryResults = BigQuery.Jobs.getQueryResults(projectNumber, queryJob.getJobReference().getJobId());
}
catch (err) {
Logger.log(err);
Browser.msgBox(err);
return;
}
}
// Update the amount of results
var resultCount = queryResults.getTotalRows();
var resultSchema = queryResults.getSchema();
//get cloumn header
// Append the headers.
var headers = queryResults.schema.fields.map(function(field) {
return field.name;
});
sheet.appendRow(headers);
var resultValues = new Array(resultCount+1);
var tableRows = queryResults.getRows();
// Iterate through query results
for (var i = 0; i < tableRows.length; i++) {
var cols = tableRows[i].getF();
resultValues[i] = new Array(cols.length);
// For each column, add values to the result array
for (var j = 0; j < cols.length; j++) {
resultValues[i][j] = cols[j].getV();
}
}
// Update the Spreadsheet with data from the resultValues array, starting from cell A1
sheet.getRange(2, 1, resultCount, tableRows[0].getF().length).setValues(resultValues);
Browser.msgBox("Yo yo! We are done with updating the results");
}
//
// Insert our customize menu item
//
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: 'Run Query', functionName: 'runQuery'} ];
sheet.addMenu('BigQuery Example', menuEntries);
};

保存,在方法下拉框中选择runQuery,点击运行(小三角图标)。

第一次运行需要一次性授权


授权完成,再点击一次运行。
运行完成,切换到新的Google Sheet页面,查看结果。

使用图表

利用Google Sheets提供的强大报表功能,让我们查询出来的数据变得更加具有表现力。
直接点击右下角的Explorer

浏览自动生成的报表,选择喜欢的插入入到文档中。


对报表再进行进一步的定制

最终的效果:

Play Hacker News With BigQuery的链接,可以自由打开,先看下效果。
https://docs.google.com/spreadsheets/d/1iMedOlxn_CBrvYERvap1kIz2l8mT2cqFag11OrmnIX4/edit?usp=sharing

总结

可以看出,对于大概4G的数据量进行BigQuery查询分析,是非常的快,并且利用Google Sheets进行结果展示,让大数据在云端飞起来。
希望看到更多利用大数据分析的应用来丰富我们的生活。

后记

BigQuery的免费使用额度为:10000个查询每天,1000G存储每月。