Skip to content

Commit b841509

Browse files
committed
Added AV.BigQuery namespace to support bigquery
1 parent ccdc1ff commit b841509

File tree

2 files changed

+145
-2
lines changed

2 files changed

+145
-2
lines changed

gulpfile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var coreSources = [
3333
'cloudfunction.js',
3434
'push.js',
3535
'status.js',
36-
'search.js'
36+
'search.js',
37+
'bigquery.js'
3738
];
3839

3940
var optionalSources = [
@@ -144,7 +145,8 @@ gulp.task('test', function() {
144145
'master_key.js',
145146
'status.js',
146147
'sms.js',
147-
'search.js'
148+
'search.js',
149+
'bigquery.js'
148150
]))
149151
.pipe(mocha({
150152
timeout: 100000,

lib/bigquery.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
(function(root) {
2+
root.AV = root.AV || {};
3+
var AV = root.AV;
4+
var _ = AV._;
5+
6+
/**
7+
* @namespace 包含了使用了 LeanCloud
8+
* <a href='/docs/bigquery_guide.html'>离线数据分析功能</a>的函数。
9+
* <p><strong><em>
10+
* 部分函数仅在云引擎运行环境下有效。
11+
* </em></strong></p>
12+
*/
13+
AV.BigQuery = AV.BigQuery || {};
14+
15+
_.extend(AV.BigQuery, /** @lends AV.BigQuery */ {
16+
17+
/**
18+
* 开始一个 BigQuery 任务。结果里将返回 Job id,你可以拿得到的 id 使用
19+
* AV.BigQuery.JobQuery 查询任务状态和结果。
20+
* @param {Object} jobConfig 任务配置的 JSON 对象,例如:<code><pre>
21+
* { "sql" : "select count(*) as c,gender from _User group by gender",
22+
* "saveAs": {
23+
* "className" : "UserGender",
24+
* "limit": 1
25+
* }
26+
* }
27+
* </pre></code>
28+
* sql 指定任务执行的 SQL 语句, saveAs(可选) 指定将结果保存在哪张表里,limit 最大 1000。
29+
* @param {Object} options A Backbone-style options object
30+
* options.success, if set, should be a function to handle a successful
31+
* call to a cloud function. options.error should be a function that
32+
* handles an error running the cloud function. Both functions are
33+
* optional. Both functions take a single argument.
34+
* @return {AV.Promise} A promise that will be resolved with the result
35+
* of the function.
36+
*/
37+
startJob: function(jobConfig, options) {
38+
if(!jobConfig || !jobConfig.sql) {
39+
throw 'Please provide the sql to run the job.';
40+
}
41+
var data = {
42+
jobConfig: jobConfig,
43+
appId: AV.applicationId
44+
}
45+
var request = AV._request("bigquery/jobs", name, null, 'POST',
46+
AV._encode(data, null, true));
47+
48+
return request.then(function(resp) {
49+
return AV._decode(null, resp).id;
50+
})._thenRunCallbacks(options);
51+
},
52+
53+
/**
54+
* 监听 BigQuery 任务事件,目前仅支持 end 事件,表示任务完成。
55+
* <p><strong><em>
56+
* 仅在云引擎运行环境下有效。
57+
* </em></strong></p>
58+
* @param {String} event 监听的事件,目前仅支持 'end' ,表示任务完成
59+
* @param {Function} 监听回调函数,接收 (err, id) 两个参数,err 表示错误信息,
60+
* id 表示任务 id。接下来你可以拿这个 id 使用AV.BigQuery.JobQuery 查询任务状态和结果。
61+
*
62+
*/
63+
on: function(event, cb) {
64+
}
65+
});
66+
67+
/**
68+
* 创建一个对象,用于查询 BigQuery 任务状态和结果。
69+
* @class
70+
* @param {String} id 任务 id
71+
* @since 0.5.5
72+
*/
73+
AV.BigQuery.JobQuery = function(id) {
74+
if(!id) {
75+
throw 'Please provide the job id.';
76+
}
77+
this.id = id;
78+
this._skip = 0;
79+
this._limit = 100;
80+
};
81+
82+
AV.BigQuery.JobQuery.prototype = {
83+
84+
/**
85+
* Sets the number of results to skip before returning any results.
86+
* This is useful for pagination.
87+
* Default is to skip zero results.
88+
* @param {Number} n the number of results to skip.
89+
* @return {AV.Query} Returns the query, so you can chain this call.
90+
*/
91+
skip: function(n) {
92+
this._skip = n;
93+
return this;
94+
},
95+
96+
/**
97+
* Sets the limit of the number of results to return. The default limit is
98+
* 100, with a maximum of 1000 results being returned at a time.
99+
* @param {Number} n the number of results to limit to.
100+
* @return {AV.Query} Returns the query, so you can chain this call.
101+
*/
102+
limit: function(n) {
103+
this._limit = n;
104+
return this;
105+
},
106+
107+
/**
108+
* 查询任务状态和结果,任务结果为一个 JSON 对象,包括 status 表示任务状态, totalCount 表示总数,
109+
* results 数组表示任务结果数组,previewCount 表示可以返回的结果总数,任务的开始和截止时间
110+
* startTime、endTime 等信息。
111+
*
112+
* @param {Object} options A Backbone-style options object
113+
* options.success, if set, should be a function to handle a successful
114+
* call to a cloud function. options.error should be a function that
115+
* handles an error running the cloud function. Both functions are
116+
* optional. Both functions take a single argument.
117+
* @return {AV.Promise} A promise that will be resolved with the result
118+
* of the function.
119+
*
120+
*/
121+
find: function(options) {
122+
var params = {
123+
skip: this._skip,
124+
limit: this._limit
125+
};
126+
127+
var request = AV._request("bigquery", 'jobs', this.id, "GET",
128+
params);
129+
return request.then(function(response) {
130+
delete response.nextAnchor;
131+
if(response.error) {
132+
return AV.Promise.error(new AV.Error(response.code, response.error));
133+
}
134+
return response;
135+
})._thenRunCallbacks(options);
136+
137+
}
138+
139+
};
140+
141+
}(this));

0 commit comments

Comments
 (0)