これは はてなエンジニア Advent Calendar 2024 25日目の記事です。
Google BigQuery REST API (v2)のjobs.query を実装した Perl モジュールを作りました。
おすすめポイント
- 列の値をフラットな hashref へ変換します
->{f}->[0]->{v}
みたいな値の探り方をしなくていいので便利です- なぜこういうデータ構造になっているのか全然わからない
- 列の値のデータ型を JSON Schema で定義できるようになっています
- データ入出力の境界には型があると安心です
やったこと
MinillaでPerlモジュールの雛形を作るだけでした。簡単。
モジュールを作ろうと思った理由
Google::BigQuery は Discovery document という仕組みを使っていて、リクエスト先のURLを実行時に組み立てるようになっています。
しかし、いつからか Discovery document の返り値の path
要素に含まれるパス変数の形式が変化してしまい、動かなくなってしまっていました。
- 期待値
projects/{projectId}/queries
- https://metacpan.org/release/SHOJIKAI/Google-BigQuery-1.02/source/lib/Google/BigQuery/V2.pm#L40-44
my $path = join('/', $self->{GOOGLE_BIGQUERY_API_BASE_URL}, $rest_description->{path}); $path =~ s/{projectId}/$project_id/; $path =~ s/{datasetId}/$dataset_id/; $path =~ s/{tableId}/$table_id/; $path =~ s/{jobId}/$job_id/;
- 実際の値
projects/{+projectId}/queries
"query": { "description": "Runs a BigQuery SQL query synchronously and returns query results if the query completes within a specified timeout.", "flatPath": "projects/{projectsId}/queries", "httpMethod": "POST", "id": "bigquery.jobs.query", "parameterOrder": [ "projectId" ], "parameters": { "projectId": { "description": "Required. Project ID of the query request.", "location": "path", "pattern": "^[^/]+$", "required": true, "type": "string" } }, "path": "projects/{+projectId}/queries", "request": { "$ref": "QueryRequest" }, "response": { "$ref": "QueryResponse" }, "scopes": [ "https://www.googleapis.com/auth/bigquery", "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/cloud-platform.read-only" ] }
やらなかったこと
CPANへの登録
perlnewmod - 新しいモジュールを配布するには - perldoc.jp を読んで片手間ではやりきれんな、と思ってやめました。
Google::BigQuery に変更を提案
10年前に作られたモジュールで、GitHub リポジトリも消えているので、これ以上時間かけてもな、と思ってやめました。
明日の担当は id:k1s1eee さんです。