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

Dataplex(Data Catalog) でカスタムエントリを更新する - カラムの description を変えてみる

Last updated at Posted at 2023-01-17

Dataplex(統合前のData Catalog) では、BigQuery などのスキーマ情報は自動的に登録されますが、これに加えて外部のサービスのメタデータも任意に管理対象とすることができます。例えば、RDBMS のスキーマ情報や Hadoop/Hive の Hive Metastore のスキーマ情報などです。

これらのスキーマ情報は、Dataplex のカスタムエントリというものを使うことで管理できます。API が用意されているのでそちらを利用して任意に運用ができます。一方で、いくつかの特定のデータベースに対しては、Connector が用意されていてそれを利用することで簡単にスキーマ情報を Dataplex に連携することができます。

例えば、以下の画面は、自前で管理する Hadoop クラスタの Hive Metastore のスキーマ情報を datacatalog-connectors-hive を使って Dataplex に入れたものです。
Screenshot 2023-01-17 at 21.56.12.png

今回はこの Description を変更してみます。変更するためには API から実行する必要がありますので、python のクライアントライブラリを使って次のようなコードで実装します。

update_entry.py
from google.cloud import datacatalog_v1

client = datacatalog_v1.DataCatalogClient()

# 特定のテーブルの Entry を取得
req = datacatalog_v1.GetEntryRequest(name='projects/{project_id}/locations/us/entryGroups/hive/entries/default__orders',)
res = datacatalog.get_entry(request=req)

# 取得したエントリを更新用に別変数へ
entry = res

# とあるカラムの Description を変更して、 Entry を更新
entry.schema.columns[7].description = 'The total price of the order'
req = datacatalog_v1.UpdateEntryRequest(entry=entry)
res = datacatalog.update_entry(request=req)

と以上です。いくつか補足解説します。

下の部分で取得する Entry のテーブルを指定しています。ここでは、nameprojects/{project_id}/locations/{location}/entryGroups/{entry_group_id}/entries/{entry_id} の形式で指定します。これをどこから取るかというと、変更したいテーブルの Entry を Dataplex で開いた際に URL に含まれているのでそれを流用するのが手っ取り早いかと思います。

datacatalog_v1.GetEntryRequest(name='projects/{project_id}/locations/us/entryGroups/hive/entries/default__orders',)

で、ここで6つめのカラム "totalprice" の Description を変更しています。

entry.schema.columns[7].description = 'The total price of the order'

ちなみに、カラム情報はこんな感じで参照できます。

>>> print(entry)
name: "projects/{project_id}/locations/us/entryGroups/hive/entries/default__orders"
display_name: "orders"
schema {
  columns {
    type: "string"
    mode: "NULLABLE"
    column: "clerk"
  }
  columns {
    type: "string"
    description: "Free comment"
    mode: "NULLABLE"
    column: "comment"
  }
  columns {
    type: "string"
    description: "Customer key"
    mode: "NULLABLE"
    column: "custkey"
  }
  columns {
    type: "string"
    description: "\346\263\250\346\226\207\343\202\255\343\203\274"
    mode: "NULLABLE"
    column: "orderkey"
  }
  columns {
    type: "string"
    mode: "NULLABLE"
    column: "orderpriority"
  }
  columns {
    type: "string"
    mode: "NULLABLE"
    column: "orderstatus"
  }
  columns {
    type: "string"
    mode: "NULLABLE"
    column: "shippriority"
  }
  columns {
    type: "string"
    mode: "NULLABLE"
    column: "totalprice"
  }
}
source_system_timestamps {
  create_time {
    seconds: 1656508836
  }
  update_time {
    seconds: 1656508836
  }
}
linked_resource: "//{hive_metastore_host}//gs://{data_location_bucket}/orders"
user_specified_type: "table"
user_specified_system: "hive"

結果以下のようにカラムの Description が更新されています。

Screenshot 2023-01-17 at 22.19.50.png

以上です。

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