4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BigQuery Storage API を触ってみた

Last updated at Posted at 2019-04-26

Google Cloud Next '19で発表された新機能を紹介します! (Cloud Run, BigQuery Storage API, Cloud Data Fusion) - ZOZO Technologies TECH BLOG の記事を読んで、BigQuery Strage APIに興味が湧きました。APIの概要や、簡単に実験して分かったことをまとめました。

BigQuery Storage API の概要

  • BigQueryからデータを取得するための、tabledata.listjobs.getQueryResults のREST API method、extract jobs とは異なるアプローチを提供
  • BigQuery(ストレージ層)からのデータの読み取りをストリーム処理で実行できる
    • データの格納はできない
  • 複数のストリームから互いに独立した行のセットを読み取ることができる
  • ストリームに対してlatencyを減らすようにデータを割り当ててくれるので、複雑な負荷分散ロジックが必要ない

ML分野ではとても役立ちそうです:relaxed:

Doc

簡単に触ってみるにはこの辺りを読むと良いと思います

API使用の流れ

https://cloud.google.com/bigquery/docs/reference/storage/Basic API flow に記載があります。ここでは各処理を1行で説明。

  • Create a session
    • 読み取り対象(table,columns等)の宣言をし、ストリームの要求をする

  • Read from a session stream
    • streaming RPCでデータを読み取る

  • Decode row blocks
    • Avro binary formatのRow blocksをデシリアライズする

APIを使用したデータ取得の実装に関して

BigQuery Storage API を使用して BigQuery データを pandas にダウンロードする  |  BigQuery  |  Google Cloud
もしくは
Python Client for BigQuery Storage API (Beta) — google-cloud 8b18324 documentation
に実装例があります。パラメータを設定する程度で、ほぼ実装例そのままで動きました。

速度検証してみた

テーブルの全データを読み出してPandasDataFrameに変換するにはどの程度の時間がかかるかを検証しました。(※ 環境、実装により速度は変わるので参考値です。:pray:)

検証条件

結果

ストリームが1つしか確立されたない状況でしたが、テーブルの全データを読み出してPandasDataFrameに変換するには300秒程度でした。extract jobsでGCSにAVROに書き出して、GCSからlocalにダウンロードして、PandasDataFrameに変換する場合だと260秒だったので良い勝負が出来ているかと思います。(ストリーム処理で扱うには多いデータ量でしたが、さくっと試すにはこのデータしかなかった:sweat_smile:)

わかったこと

  • 実装も複雑にならないし、データ読み出しの速さもあるので十分に活用できそう
  • ストリーム数の調整が上手くできなかった:sweat:
    • 下記パッケージドキュメントの解説通りにrequested_streamsを指定したが、単一ストリームになってしまう。requested_streamsを指定していない際に同じテーブル、Column Projectionでも複数のストリームが作成されたり、単一のストリームだったりと条件が理解できなかった。パッケージドキュメントのrequested_streamsThe number of streams may be lower than the requested number, depending on the amount parallelism that is reasonable for the table and the maximum amount of parallelism allowed by the systemとあるが、requested_streamsには2~3程度して指定していないのに..
    • https://cloud.google.com/bigquery/docs/reference/storage/rpc/google.cloud.bigquery.storage.v1beta1#google.cloud.bigquery.storage.v1beta1.CreateReadSessionRequest

私の理解度だとこれが限度だったので、ストリーム数の調整方法やご指摘等ありましたらご連絡ください :pray:

4
2
1

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?