big query meet node.js
TRANSCRIPT
![Page 1: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/1.jpg)
● 專案開通:https://sites.google.com/a/mitac.com.tw/google-cloud-platform/getting-start● Billing Enable:https://sites.google.com/a/mitac.com.tw/google-cloud-platform/getting-
start/enable-billing● 安裝Google Cloud SDK:
○ Windows: https://sites.google.com/a/mitac.com.tw/google-cloud-platform/getting-start/windows-install-google-cloud-sdk
○ Others: https://developers.google.com/cloud/sdk/
● 18 : 45 - 19 : 00 報到
● 19 : 00 - 19 : 30 當 BigQuery 碰上 Node.js● 19 : 30 - 19 : 40 休息
● 19 : 40 - 21 : 00 BigQuery 資料上傳實作
準備...基地台名稱 - MiTAC Guest連線帳號 - gcp連線密碼 - mitacmitac
http://goo.gl/32E4LZ
![Page 2: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/2.jpg)
Simon @ MiCloud
![Page 3: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/3.jpg)
We are….
![Page 4: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/4.jpg)
http://opennodes.arecord.us
![Page 5: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/5.jpg)
今天講的是.... Google BigQuery
![Page 6: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/6.jpg)
今天用的是.... Node.js
![Page 7: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/7.jpg)
今天要做的是...
![Page 8: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/8.jpg)
Tools
Req?
Web
App
Service
![Page 9: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/9.jpg)
時代不同,面對資料的態度也不同...
翟本喬:
● 存得起來的,叫做Storage (儲存)
● 看得到的,叫做Data (資料)
● 看得懂的,叫做Information (資訊)
● 用得出來的,才能夠叫做Intelligent (智慧)
![Page 10: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/10.jpg)
沒有不該儲存的資料...
● POS data● Log data● User behavior● Data Warehouse ● Transactions
![Page 11: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/11.jpg)
例如: 看似無用的Log...
WHO DO WHATWHEN Questions...
![Page 12: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/12.jpg)
所以... 我們需要的是
● 一個存取的方式● 越簡單越好...● 輕量... ● 快速...
天下武功,唯快不破...
![Page 13: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/13.jpg)
Tools
AuthReq?
Web
App
Service
![Page 14: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/14.jpg)
Authenticate with Oauth2.0
![Page 15: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/15.jpg)
$ npm search oauth
![Page 16: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/16.jpg)
google-api-utility模組開發歷程
● 透過初始化設定之後,即可以直接進行api呼叫動作○ 設定檔抽離○ 結合request模組進行api調用
![Page 17: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/17.jpg)
Google Service Account
web server applicationservice account
v.s.
![Page 18: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/18.jpg)
Service Owner在Google的設定
![Page 19: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/19.jpg)
Prepare Authentications
![Page 20: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/20.jpg)
$ openssl pkcs12 -in privatekey.p12 -out privatekey.pem -nocerts
$ openssl rsa -in privatekey.pem -out key.pem
Generate key.pem
$ openssl pkcs12 -in privatekey.p12 -nodes -nocerts > key.pem
or
![Page 21: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/21.jpg)
google-api-utility module
基本資訊● https://github.com/peihsinsu/google-api-utility
安裝● npm install google-api-utility
操作● apiutil.init(config)● apiutil.request(options, callback)
![Page 22: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/22.jpg)
var auth = require('google-api-utility')
auth.init({
scope: 'https://www.googleapis.com/auth/bigquery https://www.googleapis.
com/auth/cloud-platform',
client_secret: '/path-to-client_secret.json',
key_pem: '/path-to-key.pem'
});
使用範例 - 初始化
此處需要綁定所欲呼叫的 API相關授權之Scope位置
設定client_secret.json與相關pem檔案位置,供 jwt運算使用
![Page 23: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/23.jpg)
使用範例 - 呼叫BigQueryvar request = auth.request;
var bqurl = 'https://www.googleapis.com/bigquery/v2/projects/%s/datasets';
request({
url: util.format(bqurl, project),
method: 'GET'
}, function(err, req, doc){
// implements
});
結合原request模組之function,供api呼叫使用
同原request模組操作方式
![Page 24: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/24.jpg)
Tools
AuthReq?
APIWeb
App
Service
![Page 25: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/25.jpg)
Google API Explore https://developers.google.com/apis-explorer/
![Page 26: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/26.jpg)
Google API Explore - Query
![Page 27: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/27.jpg)
Google API Explore - Auth
Operation Scope
![Page 28: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/28.jpg)
Google API Explore - Response
![Page 29: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/29.jpg)
Tools
AuthReq?
APISDK Web
App
Service
![Page 30: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/30.jpg)
Idea...● bigquery.init({...configurations...})● bigquery.dataset.list(....)● bigquery.table.load(..., callback)
![Page 31: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/31.jpg)
bigquery module
基本資訊● https://github.com/peihsinsu/bigquery
安裝● npm install bigquery
操作● bigquery.init(config)● bigquery.[category].[operation](options, callback)
![Page 32: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/32.jpg)
重新包裝 - bigquery模組
var bq = require('bigquery') , prjId = 'your-bigquery-project-id';
bq.init({ client_secret: '/path/to/client_secret.json', key_pem: '/path-to-key.pem'});
bq.dataset.list(prjId, function(e,r,d){ if(e) console.log(e); console.log(JSON.stringify(d));}); 操作時,透過bq呼叫job之下的function做操作
bigquery模組可參考:https://github.com/peihsinsu/bigquery
![Page 33: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/33.jpg)
Source Code...var util = require('util') , auth = require('google-api-utility') , request = auth.request , _ = require('underscore')
exports.init = auth.init;
exports.job = { token: '', listds : function(project, cb){ var bqurl = 'https://www.googleapis.com/bigquery/v2/projects/%s/datasets'; request({ url: util.format(bqurl, project), method: 'GET' }, cb?cb:auth.commonCb); }, … (skip)}
封裝相同類別的api在一起,ex: job相關的放在 job物件中
![Page 34: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/34.jpg)
An interest demo...
![Page 35: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/35.jpg)
![Page 36: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/36.jpg)
Google provided node.js tool
![Page 37: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/37.jpg)
Operation with googleapis
var googleapis = require('googleapis');
var jwt = new googleapis.auth.JWT( '[email protected]', '/path/to/key.pem', null, [
'https://www.googleapis.com/auth/bigquery', 'https://www.googleapis.com/auth/cloud-platform'
]);
![Page 38: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/38.jpg)
Operation with googleapisjwt.authorize(function(err, tokens) {
googleapis.discover('bigquery', 'v2').execute(function(e,client) {
if(e)
console.log(e);
else
client.bigquery.datasets.list(param).withAuthClient(jwt).execute(function(err, response) {
if(err) console.log(err);
console.log(JSON.stringify(response));
});
});
});
![Page 39: Big query meet node.js](https://reader031.vdocuments.site/reader031/viewer/2022020306/5558bd73d8b42a7e298b5191/html5/thumbnails/39.jpg)
Full Code