0. 動機
- BigQueryの実行結果を整形・表示・チャート化しやすい環境をつくる
- ほぼ自分用
1. Nodeからクエリを実行
GitHub - GoogleCloudPlatform/google-cloud-node: Google Cloud Client Library for Node.js
準備
npm install --save @google-cloud/bigquery
index.js
const config = { projectId: 'foo', keyFilename: '/path/to/keyfile.json' }; const bigquery = require('@google-cloud/bigquery')(config); const query = ` SELECT id FROM [foo:bar] LIMIT 10 `; bigquery.query(query) .then(data => { const rows = data[0]; console.log(rows); }) .catch(err => { console.log(err); res.json(err); });
2. Express上で動かす
const express = require('express'); const router = express.Router(); const path = require('path'); const config = { projectId: 'foo', keyFilename: path.resolve('/path/to/keyfile.json') }; const bigquery = require('@google-cloud/bigquery')(config); router.get('/', (req, res, next) => { // クライアントからクエリを受け取る const query = req.query.query; bigquery.query(query) .then(data => { const rows = data[0]; res.json({ data: rows }) }) .catch(err => { res.json(err); }); }); module.exports = router;
自動再起動
- nodemon
CORS
app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type'); res.setHeader('Access-Control-Allow-Credentials', false); next(); });
3. クライアント側でデータの整形と表示
- ロジックテスト
- HighCharts