1
1

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 上でオントロジーとナレッジグラフを構築・活用するための OntoBricks 入門シリーズ 4. シンプルな Action の実装編

1
Last updated at Posted at 2026-09-25

概要

本記事では、Databricks Labs で開発が進められている OntoBricks を用いて、シンプルな Action を構築する手順を紹介します。OntoBricks は、Databricks 上でオントロジーとナレッジグラフを構築・活用するためのツールです。

Databricks Free Edition 上で実施した手順となっています。ただし、複数のリソースを利用するため、利用上限に達する場合がある点にご注意ください。

シンプルな Action の実装編として、ドキュメントにてClass Actions (Unity Catalog functions)として紹介されている機能を実装します。

image.png

出所: OntoBricks User Guide — OntoBricks docs

下記のように、1 個のエンティティに 3 つの Action を紐づけたオントロジーとナレッジグラフを作成します。

image.png

本記事の位置付け

OntoBricks 入門シリーズのシンプルな Action の実装編です。シリーズの構成は次のとおりです。

  1. 環境構築編
  2. シンプルなケースの実装編(Family Tree Ontology Mapping)
  3. 複雑なケースの実装編(Customer Journey Ontology)
  4. シンプルな Action の実装編
  5. データ更新を伴う Action の実装編

手順

カタログとスキーマを作成

%sql
CREATE CATALOG IF NOT EXISTS ontobricks;
CREATE SCHEMA IF NOT EXISTS ontobricks.action_demo;

image.png

テーブルの準備

%sql
CREATE OR REPLACE TABLE ontobricks.action_demo.customer (
    customer_id STRING,
    customer_name STRING
);

INSERT INTO ontobricks.action_demo.customer
VALUES
    ('C001', 'Alice');

image.png

関数を定義

%sql
CREATE OR REPLACE FUNCTION
    ontobricks.action_demo.confirm_customer_action(
        p_customer_id STRING
    )
RETURNS STRING
COMMENT 'Confirm that the action was invoked for this customer'
RETURN concat(
    'ACTION_OK: ',
    p_customer_id
);

image.png

%sql
CREATE OR REPLACE FUNCTION
    ontobricks.action_demo.get_customer_name_action(
        p_customer_id STRING
    )
RETURNS STRING
LANGUAGE SQL
COMMENT 'Get the customer name'
READS SQL DATA
RETURN
    SELECT MAX(c.customer_name)
    FROM ontobricks.action_demo.customer AS c
    WHERE c.customer_id = p_customer_id;

image.png

%sql
CREATE OR REPLACE FUNCTION
    ontobricks.action_demo.get_customer_detail_action(
        p_customer_id STRING
    )
RETURNS TABLE (
    customer_id STRING,
    customer_name STRING
)
COMMENT 'Get customer details'
READS SQL DATA
RETURN
    SELECT
        c.customer_id,
        c.customer_name
    FROM ontobricks.action_demo.customer AS c
    WHERE c.customer_id = p_customer_id;

image.png

ドメインを作成

OntoBricks にログインし、New Domainを選択します。

image.png

Domain nameにActionDemoと入力し、Create Domainを選択します。

image.png

Graph Backend を Lakebase に変更

Information -> Knowledge Graphを選択します。

image.png

Lakebaseを選択し、右上のSaveを選択します。

image.png

Data Sources を追加

Data Sourcesタブをクリックし、Add Data Sourceを選択します。

image.png

ペンのマークを選択します。

image.png

Catalogにてontobricksを、Schemaにてaction_demoを選択します。

image.png

Load Tablesを選択します。

image.png

Import 10 Tablesを選択します。

image.png

Ontology にて Entities を追加

Ontology -> Entitiesを選択します。

image.png

+ Add Entityを選択します。

image.png

NameにCustomerと入力します。

image.png

Entities にて Action を追加

Actionsの+ Addを選択します。

image.png

Catalogにてontobricksを、Schemaにてaction_demoを選択して、関数を 1 つ選びます。

image.png

同様の手順を繰り返し、3 つの関数を追加します。

image.png

OWLタブを選択して、下記のような内容が設定されていることを確認します。

# WARNINGS:
#   1. No relationships defined
#
# ─────────────────────────────────────────────────────────────

@prefix actiondemo: <https://databricks-ontology.com/ActionDemo#> .
@prefix ontobricks: <http://ontobricks.com/schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://databricks-ontology.com/ActionDemo> a owl:Ontology ;
    rdfs:label "actiondemo" .

actiondemo:Customer a owl:Class ;
    rdfs:label "Customer" ;
    ontobricks:actions "[{\"catalog\": \"ontobricks\", \"schema\": \"action_demo\", \"function\": \"confirm_customer_action\", \"fullName\": \"ontobricks.action_demo.confirm_customer_action\", \"description\": \"Confirm that the action was invoked for this customer\", \"returns_table\": false}, {\"catalog\": \"ontobricks\", \"schema\": \"action_demo\", \"function\": \"get_customer_detail_action\", \"fullName\": \"ontobricks.action_demo.get_customer_detail_action\", \"description\": \"Get customer details\", \"returns_table\": true}, {\"catalog\": \"ontobricks\", \"schema\": \"action_demo\", \"function\": \"get_customer_name_action\", \"fullName\": \"ontobricks.action_demo.get_customer_name_action\", \"description\": \"Get the customer name\", \"returns_table\": false}]" ;
    ontobricks:icon "📦" .

image.png

Mapping の定義

Mapping -> Designerを選択します。

image.png

Customerを選択します。

image.png

SQLタブにて下記のクエリを記述し、Runを選択します。

SELECT
    customer_id,
    customer_name
FROM ontobricks.action_demo.customer

image.png

下記のようにマッピングされることを確認します。

image.png

Visual 内の空白の領域を選択し、Customerが緑色になることを確認します。

image.png

R2RMLタブを選択して、下記のような内容が設定されていることを確認します。

@prefix ont: <https://databricks-ontology.com/ActionDemo/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .

ont:TriplesMap_Customer a rr:TriplesMap ;
    rdfs:comment "Mapping for Customer to https://databricks-ontology.com/ActionDemo#Customer" ;
    rr:logicalTable [ rr:sqlQuery """SELECT
    customer_id,
    customer_name
FROM ontobricks.action_demo.customer""" ] ;
    rr:predicateObjectMap [ rr:objectMap [ rr:column "customer_name" ] ;
            rr:predicate rdfs:label ] ;
    rr:subjectMap [ rr:class <https://databricks-ontology.com/ActionDemo#Customer> ;
            rr:template "https://databricks-ontology.com/ActionDemo/Customer/{customer_id}" ] .

image.png

Knowledge Graph の作成

Knowledge Graph -> Buildを選択します。

image.png

右上のBuildを選択します。

image.png

データを確認

Graph Explorer の動作確認

Aliceの Graph を表示し、ACTIONSの項目を選択します。

image.png

項目を順に選択し、下記のように値が表示されることを確認します。

image.png

image.png

image.png

Chat で動作確認

Customer C001 の customer name を Action を使って取得してと入力し、Confirmを選択します。

image.png

関数の結果が表示されることを確認します。

image.png

Customer C001 に対して confirm_customer_action を実行してと入力し、Confirmを選択します。

image.png

関数の結果が表示されることを確認します。

image.png

Customer C001 に対して get_customer_detail_action を実行してと入力し、Confirmを選択します。

image.png

関数の結果が表示されることを確認します。

image.png

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?