LoginSignup
5
3

More than 1 year has passed since last update.

BigQueryのschema json を bigquery.SchemaField に変換するpythonスクリプト

Last updated at Posted at 2022-06-14

背景

pythonでBigQueryへのLoadJobConfigを書く場合、bigquery.SchemaFieldが必要になる。

bigqueryのschemaは bq show --schema --format=prettyjson などで取得できるjson型で取り扱うことが多い。

json型のschema情報を bigquery.SchemaField の形にconvertする機会が多いので、そのpythonスクリプトをメモしておく.

実装

    # jsonからbigquery.SchemaFieldを生成する
    def _get_field_schema(self, field):
        name = field['name']
        field_type = field.get('type', 'STRING')
        mode = field.get('mode', 'NULLABLE')
        description = field.get('description', '')
        fields = field.get('fields', [])

        if fields:
            subschema = []
            for f in fields:
                fields_res = self._get_field_schema(f)
                subschema.append(fields_res)
        else:
            subschema = []

        field_schema = bigquery.SchemaField(
            name=name,
            field_type=field_type,
            mode=mode,
            description=description,
            fields=subschema
        )
        return field_schema

    # jsonを読み込んで bigquery.ScheaField の配列を返す
    def get_schema(self, path_to_schema_file):
        schema = []
        with open(path_to_schema_file, 'r') as f:
            jsonschema = json.load(f)

        for field in jsonschema:
            schema.append(self._get_field_schema(field))

        return schema

参照

5
3
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
5
3