Google IO - When Bigquery meeet Node.js

43
Simon @ MiCloud

description

分享在Google IO,介紹Node.js實作BigQuery服務的一些小工具~

Transcript of Google IO - When Bigquery meeet Node.js

Page 1: Google IO - When Bigquery meeet Node.js

Simon @ MiCloud

Page 2: Google IO - When Bigquery meeet Node.js

We are...

Page 3: Google IO - When Bigquery meeet Node.js

The APAC Partner of Google

Page 4: Google IO - When Bigquery meeet Node.js

前情提要

很久很久以前...

Page 5: Google IO - When Bigquery meeet Node.js

Node.js inTaiwan

Page 6: Google IO - When Bigquery meeet Node.js

HTTP://OpenNodes.arecord.us

OR

Sample

Copy

Run!

Page 7: Google IO - When Bigquery meeet Node.js

時代不同,⾯面對資料的態度也不同...

● 存得起來的,叫做Storage (儲存)!

● 看得到的,叫做Data (資料)!

● 看得懂的,叫做Information (資訊)!

● ⽤用得出來的,才能夠叫做Intelligent (智慧)

和沛科技 CEO & Founder - 翟本喬

Page 8: Google IO - When Bigquery meeet Node.js

看似無意義的log

Page 9: Google IO - When Bigquery meeet Node.js

不要⼩小看群眾的⼒力量....

Page 10: Google IO - When Bigquery meeet Node.js

Google BigQuery

Page 11: Google IO - When Bigquery meeet Node.js

吸引⼈人的地⽅方.....

Page 12: Google IO - When Bigquery meeet Node.js

Top 1 - JavaScript / Node.js

select repository language from

372211 259329 223470

Page 13: Google IO - When Bigquery meeet Node.js

Tools

AuthReq?

APISDK Web

App

Service

Page 14: Google IO - When Bigquery meeet Node.js

Node.js是...● 輕鬆接軌...

● 簡單了解...

● 輕量...

● 效能...

天下武功,唯快不破...

Page 15: Google IO - When Bigquery meeet Node.js

絕世武功秘笈

Page 16: Google IO - When Bigquery meeet Node.js

Node.js & Oauth & Google

Authenticate with Oauth2.0

Page 17: Google IO - When Bigquery meeet Node.js

Google Oauth2Web Server Application Service Account

Request token

Authorization code

Exchange code for token

Token response

Use token to call Google API

Your App Google Servers

User login !& consent

User

Create and sign JWT

Use JWT to request token

Token response

Use token to call Google API

Server App

Google Servers

Page 18: Google IO - When Bigquery meeet Node.js

google-api-utility模組開發歷程

透過初始化設定之後, 即可以直接進⾏行api呼叫動作 !1. 設定檔抽離 2. 結合request模組進⾏行API呼叫 3. Access Token cache

Page 19: Google IO - When Bigquery meeet Node.js

google-api-utility module基本資訊 ● https://github.com/peihsinsu/google-api-utility 安裝 ● npm install google-api-utility 操作 ● apiutil.init(config) ● apiutil.request(options, callback)

Page 20: Google IO - When Bigquery meeet Node.js

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 21: Google IO - When Bigquery meeet Node.js

使⽤用範例 - 呼叫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模組操作⽅方式

結合原request模組之function,供api呼叫使⽤用

Page 22: Google IO - When Bigquery meeet Node.js

Google API Explore HTTPS://developers.google.com/apis-explorer/

Page 23: Google IO - When Bigquery meeet Node.js

Google API Explore - Query

Page 24: Google IO - When Bigquery meeet Node.js

Google API Explore - Auth

Operation Scope

Page 25: Google IO - When Bigquery meeet Node.js

Google API Explore - Response

Page 26: Google IO - When Bigquery meeet Node.js

Idea...● bigquery.init({...configurations...})

● bigquery.[what].[do](...)

- bigquery.dataset.list(....)

- bigquery.table.load(..., callback)

Page 27: Google IO - When Bigquery meeet Node.js

bigquery module基本資訊 ● https://github.com/peihsinsu/bigquery 安裝 ● npm install bigquery 操作 ● bigquery.init(config) ● bigquery.[category].[operation](options, callback)

Page 28: Google IO - When Bigquery meeet Node.js

重新包裝 - 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)); });

bigquery模組可參考:https://github.com/peihsinsu/bigquery

透過bq呼叫某個操作之下的function

Page 29: Google IO - When Bigquery meeet Node.js

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 30: Google IO - When Bigquery meeet Node.js

應⽤用.... sql2bq

Page 31: Google IO - When Bigquery meeet Node.js

sql2bq module基本資訊 ● https://github.com/micloud/sql2bq 安裝 ● npm install sql2bq -g 操作 ● sql2bq-init ● sql2bq-load -q [SQL] -t [table]

Page 32: Google IO - When Bigquery meeet Node.js
Page 33: Google IO - When Bigquery meeet Node.js

Google’s Node.js SDK

Page 34: Google IO - When Bigquery meeet Node.js

Operation with googleapisvar 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 35: Google IO - When Bigquery meeet Node.js

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 36: Google IO - When Bigquery meeet Node.js

應⽤用... gapis

Page 37: Google IO - When Bigquery meeet Node.js

gapis module基本資訊 ● https://github.com/peihsinsu/gapis 安裝 ● npm install gapis -g 操作 ● gcli -d project=[PROJECT_ID] -f [API FUNCTION]

Page 38: Google IO - When Bigquery meeet Node.js

NOT ONLY Node.js Love JavaScript…..

Page 39: Google IO - When Bigquery meeet Node.js
Page 40: Google IO - When Bigquery meeet Node.js

The fancy integrate with Sheet

Page 41: Google IO - When Bigquery meeet Node.js

More and more chart….

Page 42: Google IO - When Bigquery meeet Node.js

Charting in Apps Script

Page 43: Google IO - When Bigquery meeet Node.js

http://micloud.tw