2
0

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 1 year has passed since last update.

BigQuery Client for Javaでqueryメソッドを使用したTableResultをJSONにする方法

Posted at

困りごと

BigQueryでSQLを使用してテーブルからデータを取得する際、BigQuery APIを使用するとJSONで結果を取得することができる。

しかし、JavaのクライアントライブラリでSQLの結果を取得した場合、FieldValueのAttributeにRECORDとREPEATEDが混じっていると構造が複雑になり、すんなりとJSONには変換できない。

動作確認環境

解決方法

BigQueryのSQLでTO_JSON_STRING関数を使用すれば結果がJSON文字列で取得できることを利用する。

SELECT
    TO_JSON_STRING(result) AS json
FROM (
    SELECT
        *
    FROM
        `$projectId.$datasetName.$tableName`
) AS result
;
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.google.cloud.bigquery.BigQueryOptions
import com.google.cloud.bigquery.QueryJobConfiguration


fun query(): String {
    val projectId = "MY_PROJECT_ID"
    val datasetName = "MY_DATASET_NAME"
    val tableName = "MY_TABLE_NAME"
    val query = "SELECT TO_JSON_STRING(result) AS json FROM (SELECT * FROM `$projectId.$datasetName.$tableName`) AS result"

    val bigQuery = BigQueryOptions.getDefaultInstance().service
    val queryConfig = QueryJobConfiguration.of(query)
    val results = bigQuery.query(queryConfig)

    val mapper = jacksonObjectMapper()

    // 一度オブジェクトに変換する
    val resultList = results.iterateAll().map { row ->
        val jsonString = row.get("json").stringValue

        mapper.readValue<Any>(
            jsonString,
            object: TypeReference<Any>() {}
        )
    }

    // JSON文字列として出力する
    return mapper.writeValueAsString(resultList)
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?