LoginSignup
19
19

More than 5 years have passed since last update.

Go言語からBigQueryのAPIを使う

Last updated at Posted at 2014-09-19

[追記] JWTまわりのAPIが変わったようなので、以下のサンプルそのままだと動かない可能性あり

Googleの提供するライブラリをimportする。

go
import (
    bigquery "code.google.com/p/google-api-go-client/bigquery/v2"
    "code.google.com/p/goauth2/oauth/jwt"
)

アクセスに必要なトークンを準備する。

go
iss := "yourname@developer.gserviceaccount.com"
scope := bigquery.BigqueryScope
pem := `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`
token := jwt.NewToken(iss, scope, []byte(pem))

HTTPクライアントを準備する。

go
transport, err := jwt.NewTransport(token)
client := transport.Client()
bq, err := bigquery.New(client)

APIを呼ぶ。
以下の例ではBigQueryに保存された行を取得している。

go
call := bq.Tabledata.List("projectid", "dataset", "table")
call.MaxResults(10)
list, err := call.Do()

上記のlistをJSON形式に変換して表示する場合。

go
buf, err := json.Marshal(list)
fmt.Println(string(buf))

ちなみに行をBigQueryに挿入したい場合はTabledata.InsertAllというAPIを使う。

19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19