概要
本記事では、Databricks Labs で開発が進められている OntoBricks を用いて、シンプルな Action を構築する手順を紹介します。OntoBricks は、Databricks 上でオントロジーとナレッジグラフを構築・活用するためのツールです。
Databricks Free Edition 上で実施した手順となっています。ただし、複数のリソースを利用するため、利用上限に達する場合がある点にご注意ください。
シンプルな Action の実装編として、ドキュメントにてClass Actions (Unity Catalog functions)として紹介されている機能を実装します。
出所: OntoBricks User Guide — OntoBricks docs
下記のように、1 個のエンティティに 3 つの Action を紐づけたオントロジーとナレッジグラフを作成します。
本記事の位置付け
OntoBricks 入門シリーズのシンプルな Action の実装編です。シリーズの構成は次のとおりです。
- 環境構築編
- シンプルなケースの実装編(Family Tree Ontology Mapping)
- 複雑なケースの実装編(Customer Journey Ontology)
- シンプルな Action の実装編
- データ更新を伴う Action の実装編
手順
カタログとスキーマを作成
%sql
CREATE CATALOG IF NOT EXISTS ontobricks;
CREATE SCHEMA IF NOT EXISTS ontobricks.action_demo;
テーブルの準備
%sql
CREATE OR REPLACE TABLE ontobricks.action_demo.customer (
customer_id STRING,
customer_name STRING
);
INSERT INTO ontobricks.action_demo.customer
VALUES
('C001', 'Alice');
関数を定義
%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
);
%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;
%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;
ドメインを作成
OntoBricks にログインし、New Domainを選択します。
Domain nameにActionDemoと入力し、Create Domainを選択します。
Graph Backend を Lakebase に変更
Information -> Knowledge Graphを選択します。
Lakebaseを選択し、右上のSaveを選択します。
Data Sources を追加
Data Sourcesタブをクリックし、Add Data Sourceを選択します。
ペンのマークを選択します。
Catalogにてontobricksを、Schemaにてaction_demoを選択します。
Load Tablesを選択します。
Import 10 Tablesを選択します。
Ontology にて Entities を追加
Ontology -> Entitiesを選択します。
+ Add Entityを選択します。
NameにCustomerと入力します。
Entities にて Action を追加
Actionsの+ Addを選択します。
Catalogにてontobricksを、Schemaにてaction_demoを選択して、関数を 1 つ選びます。
同様の手順を繰り返し、3 つの関数を追加します。
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 "📦" .
Mapping の定義
Mapping -> Designerを選択します。
Customerを選択します。
SQLタブにて下記のクエリを記述し、Runを選択します。
SELECT
customer_id,
customer_name
FROM ontobricks.action_demo.customer
下記のようにマッピングされることを確認します。
Visual 内の空白の領域を選択し、Customerが緑色になることを確認します。
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}" ] .
Knowledge Graph の作成
Knowledge Graph -> Buildを選択します。
右上のBuildを選択します。
データを確認
Graph Explorer の動作確認
Aliceの Graph を表示し、ACTIONSの項目を選択します。
項目を順に選択し、下記のように値が表示されることを確認します。
Chat で動作確認
Customer C001 の customer name を Action を使って取得してと入力し、Confirmを選択します。
関数の結果が表示されることを確認します。
Customer C001 に対して confirm_customer_action を実行してと入力し、Confirmを選択します。
関数の結果が表示されることを確認します。
Customer C001 に対して get_customer_detail_action を実行してと入力し、Confirmを選択します。
関数の結果が表示されることを確認します。








































