この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
BigQueryでは、データを格納するための要素として「データセット」というものが存在します。
当エントリでは、Google BigQueryにおける「データセット」の作成や管理の方法について、実際にコンソール上やプログラム経由で触ってみながら、基本的な部分について理解を深めていきたいと思います。
目次
BigQueryの「データセット」とは
BigQueryにおける「データセットの概要」は以下公式ドキュメントにその内容がまとまっています。
日本語サイトからその一部を抜粋。
データセットは、特定のプロジェクト内に含まれています。データセットは、テーブルとビューへのアクセスを整理して制御するために使用される最上位のコンテナです。テーブルまたはビューはデータセットに属していなければなりません。したがって、データを BigQuery に読み込む前に、1 つ以上のデータセットを作成する必要があります。
テーブルを作成するにあたっては無くてはならない、必須となる要素になるようですね。Amazon Redshiftで言うところのスキーマみたいなものかな?と一旦は理解しました。下記スライドに構成をイメージしやすい図が載っていたので添付しておきます。
BigQueryデータセットの作成
メニューから「BigQuery」を選択。
データセットが1件もプロジェクト内に存在してない状態。ここからまず1個データセットを作ってみます。
ここでは任意の名称、リージョンを指定して作成してみました。テーブルの有効期限、暗号化についてはデフォルト指定としています。
作成完了。
Pythonで作成する場合の実装例は以下。こちらでは説明欄についても作成時に指定可能となっています。また有効期限はミリ秒で記載する形に。プログラムを介する場合、「テーブル」及び「パーティション」の有効期限が設定可能です。
create-bq-datasets.py
# TODO(developer): Import the client library.
from google.cloud import bigquery
# TODO(developer): Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set dataset_id to the ID of the dataset to create.
dataset_id = "cm-da-xxxxxxxxxx.bq_cm_dataset_by_python".format(client.project)
# Construct a full Dataset object to send to the API.
dataset = bigquery.Dataset(dataset_id)
# TODO(developer): Specify the geographic location where the dataset should reside.
dataset.location = "asia-east1"
dataset.description = "Python経由で作られたデータセットです."
dataset.default_partition_expiration_ms = 7776000
dataset.default_table_expiration_ms = 3600000
# Send the dataset to the API for creation.
# Raises google.api_core.exceptions.Conflict if the Dataset already
# exists within the project.
dataset = client.create_dataset(dataset) # Make an API request.
print("Created dataset {}.{}".format(client.project, dataset.dataset_id))
上記Pythonプログラムで作成したデータセット。
CLI(bqコマンド)での作成例は以下の通り。
$ bq --location=asia-northeast2 mk \
> --dataset \
> --default_table_expiration 8000000 \
> --default_partition_expiration 3600000 \
> --description "CLI経由で作成したデータセットです" \
> cm-xxxxxxxxxx:bq_dataset_by_cli
Dataset 'cm-xxxxxxxxxx:bq_dataset_by_cli' successfully created.
データセットの一覧表示
シンプルに「データセットの一覧表示」であればbq lsコマンドで取得可能。
$ bq ls
datasetId
----------------------------
bq_dataset_by_cli
cmbqdataset
また、Pythonプログラムの場合は以下の様な形で情報を取得可能。ただ、個人的には「データセットの詳細をテーブル形式で並べたい」のでちょっと情報としては足りないかな...?別途改めてこの部分については調べてみたいと思います。
list-bq-datasets.py
# TODO(developer): Import the client library.
from google.cloud import bigquery
# TODO(developer): Construct a BigQuery client object.
client = bigquery.Client()
datasets = list(client.list_datasets()) # Make an API request.
project = client.project
if datasets:
print("Datasets in project {}:".format(project))
for dataset in datasets:
print("\t{}".format(dataset.dataset_id))
print("\t{}".format(dataset.friendly_name))
print("\t{}".format(dataset.full_dataset_id))
print("\t{}".format(dataset.labels))
print("\t{}".format(dataset.model))
print("\t{}".format(dataset.project))
print("\t{}".format(dataset.reference))
print("\t{}".format(dataset.routine))
print("\t{}".format(dataset.table))
print(dir(dataset))
else:
print("{} project does not contain any datasets.".format(project))
BigQueryデータセットの変更
作成したデータセットには、「説明」の文字列と、
「ラベル」を設定可能です。
作成したデータセットについては、[共有データセット]のメニューから
任意のアクセス制限を行う事が可能です。
有効期限についてはテーブルに対してのみ、変更が可能となっています。
BigQueryデータセットは「複製」も可能。(※ただし現時点ではプレリリース版の模様です)
試しに何の気無しに動かしてみましたが、別途「Data Transfer Service」なるサービスを有効にしておく必要があるとのことでした。ここについては別途エントリを改める形でチャレンジ出来ればと思います。
BigQueryデータセットの削除
データセットの削除はコンソール上で実行する場合、対象となっているデータセットの名称を入力させるUIになっています。
delete-bq-datasets.py
# TODO(developer): Import the client library.
from google.cloud import bigquery
# TODO(developer): Construct a BigQuery client object.
client = bigquery.Client()
# TODO(developer): Set model_id to the ID of the model to fetch.
dataset_id = 'cm-xxxxxxxxxxxxxx.bq_cm_dataset_by_python'
# Use the delete_contents parameter to delete a dataset and its contents.
# Use the not_found_ok parameter to not receive an error if the dataset has already been deleted.
client.delete_dataset(
dataset_id, delete_contents=True, not_found_ok=True
) # Make an API request.
print("Deleted dataset '{}'.".format(dataset_id))
まとめ
という訳で、Google BigQueryの「データセット」に関する操作方法のざっくりまとめでした。基本的な操作方法を確認していく過程で「あれ?この辺はどうなのかな?」というポイントが幾つか出てきてたので、そのあたりについては別途検証・確認しエントリを改めてアウトプットしていきたいと思います。