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?

Databricks Free Editionで始めるデータパイプライン作成:Qiita記事の取得から加工・活用まで⑥ メトリクスビューの作成

Posted at

こちらの続きです。

はじめに

初回記事より。

Databricks Intelligence Platformはいわずと知れた(?)強力なデータ+AIプラットフォームです。個人的には(そして本業的にも)、その中でもデータパイプライン構築機能はお気に入りです。

この良さを少しでも伝えられるように、具体的なサンプルを用いたデータパイプライン構築の記事を書くことに今回チャレンジします。

前回はパイプラインを改修してディメンジョナルモデルを構築しました。
今回は作成したデータオブジェクトをDatabricksのUnity Catalog メトリクスビューを使ってデータモデルの利用を強化します。

参考:メトリクスビューとは

詳細はこちらの公式ドキュメントを参照ください。

最近出ていたこちらの記事も是非。わかりやすいです。

自分なりの理解で言えば、Unity Catalog メトリクスビューはセマンティックレイヤーを実現するひとつの形です。

7. メトリクスビューを作る

メトリクスビューはUIからも作成できるのですが、Asset Bundle内で管理するためにもSQLから作成します。

まず、metric_viewsというフォルダを作成し、metric_view_creationという名前でノートブックを作成し、以下のコードを記載します。

metric_views/metric_view_creation.ipynb
%sql

use catalog identifier(:catalog);
use schema identifier(:schema);

declare or replace qry_str string;

set var qry_str = 
"CREATE OR REPLACE VIEW qiita_metric_view
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1

source: fact_item

joins:
  - name: dim_author
    source: dim_author
    using:
      - author_id
  - name: dim_created_calendar
    source: dim_calendar
    on: source.created_date_id = dim_created_calendar.date_id
  - name: dim_updated_calendar
    source: dim_calendar
    on: source.updated_date_id = dim_updated_calendar.date_id
  - name: dim_imported_calendar
    source: dim_calendar
    on: source.imported_date_id = dim_imported_calendar.date_id
  - name: dim_item_category
    source: dim_item_category
    using:
      - item_category_id

dimensions:
  - name: author_id
    expr: dim_author.author_id
    comment: 各アイテムの著者ID
    display_name: 著者ID
  - name: author_name
    expr: dim_author.author_name
    comment: 各アイテムの著者名
    display_name: 著者名
  - name: created_date
    expr: dim_created_calendar.date_id
    comment: 作成日(dim_created_calendarのdate_id)
    display_name: 作成日
    format:
      type: date
      date_format: year_month_day
      leading_zeros: false
  - name: created_day_of_week
    expr: dayofweek(dim_created_calendar.date_id)
    comment: 作成日の曜日
    display_name: 曜日(作成日)
  - name: created_year
    expr: year(dim_created_calendar.date_id)
    comment: 作成日の年
    display_name: 年(作成日)
  - name: created_month
    expr: month(dim_created_calendar.date_id)
    comment: 作成日の月
    display_name: 月(作成日)
  - name: created_day
    expr: day(dim_created_calendar.date_id)
    comment: 作成日の日
    display_name: 日(作成日)
  - name: updated_date
    expr: dim_updated_calendar.date_id
    comment: 更新日(dim_updated_calendarのdate_id)
    display_name: 更新日
    format:
      type: date
      date_format: year_month_day
      leading_zeros: false
  - name: updated_day_of_week
    expr: dayofweek(dim_updated_calendar.date_id)
    comment: 更新日の曜日
    display_name: 曜日(更新日)
  - name: updated_year
    expr: year(dim_updated_calendar.date_id)
    comment: 更新日の年
    display_name: 年(更新日)
  - name: updated_month
    expr: month(dim_updated_calendar.date_id)
    comment: 更新日の月
    display_name: 月(更新日)
  - name: updated_day
    expr: day(dim_updated_calendar.date_id)
    comment: 更新日の日
    display_name: 日(更新日)
  - name: imported_date
    expr: dim_imported_calendar.date_id
    comment: 取込日(dim_imported_calendarのdate_id)
    display_name: 取込日
    format:
      type: date
      date_format: year_month_day
      leading_zeros: false
  - name: imported_day_of_week
    expr: dayofweek(dim_imported_calendar.date_id)
    comment: 取込日の曜日
    display_name: 曜日(取込日)
  - name: imported_year
    expr: year(dim_imported_calendar.date_id)
    comment: 取込日の年
    display_name: 年(取込日)
  - name: imported_month
    expr: month(dim_imported_calendar.date_id)
    comment: 取込日の月
    display_name: 月(取込日)
  - name: imported_day
    expr: day(dim_imported_calendar.date_id)
    comment: 取込日の日
    display_name: 日(取込日)
  - name: item_category_name
    expr: dim_item_category.item_category_name
    comment: アイテムカテゴリ名(dim_item_categoryのitem_category_name)
    display_name: アイテムカテゴリ名
  - name: tag_ids
    expr: tag_ids
    display_name: タグIDs
  - name: item_keywords
    expr: item_keywords
    comment: アイテムのキーワード
    display_name: キーワード

measures:
  - name: total_item_count
    expr: count(distinct id)
    comment: アイテムのユニーク件数
    display_name: 件数_合計
  - name: total_likes_count
    expr: SUM(likes_count)
    comment: 全アイテムのいいね数合計
    display_name: いいね数_合計
  - name: total_comments_count
    expr: SUM(comments_count)
    comment: 全アイテムのコメント数合計
    display_name: コメント数_合計
  - name: total_reactions_count
    expr: SUM(reactions_count)
    comment: 全アイテムのリアクション数合計
    display_name: リアクション数_合計
  - name: total_stocks_count
    expr: SUM(stocks_count)
    comment: 全アイテムのストック数合計
    display_name: ストック数_合計
  - name: total_page_views_count
    expr: SUM(page_views_count)
    comment: 全アイテムのページビュー数合計
    display_name: ページビュー数_合計
$$";

execute immediate qry_str;

メトリクスビューはYAML形式の定義から作成することができます。
スタースキーマとスノーフレークスキーマのデータモデルに対応しており、dimensionsmeasuresを元のテーブルの項目を利用して定義することができます。

日付関連の分析属性も今回はメトリクスビュー内で定義しました。このように元テーブルに無い分析軸もメトリクスビュー内で動的計算できます。

日付関係を動的計算する必要性はあまりありませんので、本来はパイプライン内で事前計算する方がいいと思います。

catalogならびにschemaパラメータを指定して実行することでメトリクスビューを作成できます。
開発用と本番用で分けてカタログ・スキーマを指定できるように、Asset Bundleでこのノートブックを実行するJobを作りましょう。

resources/qiita_metric_views_setup.job.yml
# メトリクスビュー作成用Job
resources:
  jobs:
    qiita_metric_views_setup_job:
      name: qiita_metric_views_setup_job

      parameters:
        - name: catalog
          default: ${var.catalog}
        - name: schema
          default: ${var.schema}

      tasks:
        - task_key: metric_view_setup
          notebook_task:
            notebook_path: ../src/qiita_items_pipeline_etl/metric_views/metric_view_creation.ipynb
            source: WORKSPACE

      environments:
        - environment_key: default
          spec:
            environment_version: "4"

これをデプロイ・実行することでメトリクスビューが作成されます。

image.png

作成したメトリクスビューはDatabricks SQLやダッシュボード、Genieなどから利用できます。

今回はここまで。次回に続きます

次回はメトリクスビューをGenieで利用し、Qiitaの記事分析を行います。

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?