Last active
May 15, 2017 14:28
-
-
Save sekky0905/0bea02ab797931f889fd6470da99f8b9 to your computer and use it in GitHub Desktop.
GoでBigQueryクライアントを実装してBigQueryからデータを取得する ref: http://qiita.com/Sekky0905/items/fd6ff9113d301aaa9e1d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go get cloud.google.com/go/bigquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"cloud.google.com/go/bigquery" | |
"context" | |
"fmt" | |
"google.golang.org/api/iterator" | |
"log" | |
) | |
func main() { | |
fetchBigQueryData() | |
} | |
// BigQueryで実行するためのSQL | |
const QUERY = ` | |
SQL | |
` | |
func fetchBigQueryData() { | |
// 空のcontextを生成 | |
ctx := context.Background() | |
// プロジェクトのID | |
projectID := "プロジェクトのID" | |
// contextとprojectIDを元にBigQuery用のclientを生成 | |
client, err := bigquery.NewClient(ctx, projectID) | |
if err != nil { | |
log.Printf("Failed to create client:%v", err) | |
} | |
// 引数で渡した文字列を元にQueryを生成 | |
q := client.Query(QUERY) | |
// 実行のためのqueryをサービスに送信してIteratorを通じて結果を返す | |
// itはIterator | |
it, err := q.Read(ctx) | |
if err != nil { | |
log.Println("Failed to Read Query:%v", err) | |
} | |
for { | |
// BigQueryの結果から、中身を格納するためのBigQuery.Valueのsliceを宣言 | |
// BigQuery.Valueはinterface{}型 | |
var values []bigquery.Value | |
// 引数に与えたvaluesにnextを格納する | |
// Iteratorを返す | |
// これ以上結果が存在しない場合には、iterator.Doneを返す | |
// iterator.Doneが返ってきたら、forを抜ける | |
err := it.Next(&values) | |
if err == iterator.Done { | |
break | |
} | |
if err != nil { | |
log.Println("Failed to Iterate Query:%v", err) | |
} | |
fmt.Println(values) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
* | |
FROM | |
[bigquery-public-data:usa_names.usa_1910_2013] | |
WHERE | |
name = 'Mary' | |
LIMIT | |
100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"cloud.google.com/go/bigquery" | |
"context" | |
"fmt" | |
"google.golang.org/api/iterator" | |
"log" | |
) | |
func main() { | |
fetchBigQueryData() | |
} | |
// BigQueryで実行するためのSQL | |
const QUERY = ` | |
SELECT * | |
FROM [bigquery-public-data:usa_names.usa_1910_2013] | |
WHERE name = 'Mary' | |
LIMIT 10 | |
` | |
func fetchBigQueryData() { | |
// 空のcontextを生成 | |
ctx := context.Background() | |
// プロジェクトのID | |
projectID := "プロジェクトのID" | |
// contextとprojectIDを元にBigQuery用のclientを生成 | |
client, err := bigquery.NewClient(ctx, projectID) | |
if err != nil { | |
log.Printf("Failed to create client:%v", err) | |
} | |
// 引数で渡した文字列を元にQueryを生成 | |
q := client.Query(QUERY) | |
// 実行のためのqueryをサービスに送信してIteratorを通じて結果を返す | |
// itはIterator | |
it, err := q.Read(ctx) | |
if err != nil { | |
log.Println("Failed to Read Query:%v", err) | |
} | |
for { | |
// BigQueryの結果から、中身を格納するためのBigQuery.Valueのsliceを宣言 | |
// BigQuery.Valueはinterface{}型 | |
var values []bigquery.Value | |
// 引数に与えたvaluesにnextを格納する | |
// Iteratorを返す | |
// これ以上結果が存在しない場合には、iterator.Doneを返す | |
// iterator.Doneが返ってきたら、forを抜ける | |
err := it.Next(&values) | |
if err == iterator.Done { | |
break | |
} | |
if err != nil { | |
log.Println("Failed to Iterate Query:%v", err) | |
} | |
fmt.Println(values) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AK F 1912 Mary 9] | |
[AK F 1916 Mary 18] | |
[AK F 1918 Mary 27] | |
[AK F 1919 Mary 22] | |
[AK F 1923 Mary 26] | |
[AK F 1925 Mary 24] | |
[AK F 1926 Mary 39] | |
[AK F 1927 Mary 30] | |
[AK F 1930 Mary 35] | |
[AK F 1931 Mary 41] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment