BigQueryの概要を把握したあとGoクライアントで試す
BigQueryの概要を把握し、Goクライアントで色々試します。
ゴール
- BigQueryとはどういうものなのか把握する
- 特にどうやって爆速なクエリ実行を実現しているかが気になる
- Goクライアントからデータ操作を行う
- Fixturesを使ったテストを書く
- GitHub Actions上でテストを実行する
試したリポジトリはこちら:
まずは何よりもトップページの概要:
Serverless, highly scalable, and cost-effective multicloud data warehouse designed for business agility.
ref. https://cloud.google.com/bigquery
Data warehouseなんだな。基本情報技術者とかで覚えた言葉で懐かしい響きがある。
そして売り文句:
- Democratize insights with a secure and scalable platform with built-in machine learning
Democratizeって言葉に力強さを感じる。機械学習使ってるのか。
- Power business decisions from data across clouds with a flexible, multicloud analytics solution
ビシネスの決断の助けをする、と。
- Run analytics at scale with 26%–34% lower three-year TCO than cloud data warehouse alternatives
TCO = Total Cost of Ownership。低コストですよ、と。
- Adapting to your data at any scale, from bytes to petabytes, with zero operational overhead
ペタバイトだろうがどんなスケールでも対応しますよ、と。
Benefits:
Gain insights with real-time and predictive analytics
Query streaming data in real time and get up-to-date information on all your business processes. Predict business outcomes easily with built-in machine learning–without the need to move data.
リアルタイムにストリーミングデータをクエリ。
機械学習はビジネスアウトカムの予測に使われるのか。
Access data and share insights with ease
Securely access and share analytical insights in your organization with a few clicks. Easily create stunning reports and dashboards using popular business intelligence tools, out of the box.
セキュアにインサイトをアクセス、共有できて、超簡単にレポートやダッシュボード作れますよ、と。
Protect your data and operate with trust
Rely on BigQuery’s robust security, governance, and reliability controls that offer high availability and a 99.99% uptime SLA. Protect your data with encryption by default and customer-managed encryption keys.
セキュアで可用性抜群ですよ、と。
Cloud Consoleで試すにはここを見れば全て載ってる:
無料で使えるサンドボックスについては同じページのこちらに載っている:
各言語で使えるライブラリについてはこちら:
上記のドキュメントを元に作ったデータセットで SELECT
文を試す:
package main
import (
"cloud.google.com/go/bigquery"
"context"
"fmt"
"google.golang.org/api/iterator"
"io"
"os"
)
var projectID string
var dataset string
func init() {
projectID = os.Getenv("GCP_PROJECT")
dataset = os.Getenv("BQ_DATASET")
}
func main() {
err := queryBasic(os.Stdout, projectID)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
}
// queryBasic demonstrates issuing a query and reading results.
func queryBasic(w io.Writer, projectID string) error {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
q := client.Query(
"SELECT name, count FROM " + dataset + ".names_2014 " +
"WHERE gender = 'M' " +
"ORDER BY count DESC " +
"LIMIT 5")
// Location must match that of the dataset(s) referenced in the query.
q.Location = "US"
// Run the query and print results when the query job is completed.
job, err := q.Run(ctx)
if err != nil {
return err
}
status, err := job.Wait(ctx)
if err != nil {
return err
}
if err := status.Err(); err != nil {
return err
}
it, err := job.Read(ctx)
for {
var row []bigquery.Value
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
return err
}
fmt.Fprintln(w, row)
}
return nil
}
以下の環境変数にそれぞれセット:
- GCP_PROJECT: プロジェクトID
- BQ_DATASET: データセット名
- GOOGLE_APPLICATION_CREDENTIALS: サービスアカウントのクレデンシャルズJSONへのパス
実行したらうまくいった 🎉
[Noah 19319]
[Liam 18470]
[Mason 17208]
[Jacob 16890]
[William 16832]
これに技術詳細が載ってるっぽい:
あとでじっくり読む。
Fixturesについてはこの記事が参考になりそう: