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?

manabianAdvent Calendar 2024

Day 17

RDFLib-Neo4j Getting Started実行時のエラー再現と解決方法

Posted at

概要

RDFLib-Neo4j の Getting Started に記述されているコードを実行した際に発生する下記エラーの解決方法を共有します。Google Colab から RDFLib-Neo4j を実行し、エラーの再現方法と対応方法を記述します。2024年12月17日時点でのドキュメントのコードで発生しています。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-0498e7fe3371> in <cell line: 15>()
     14 # Define your custom mappings & store config
     15 config = Neo4jStoreConfig(auth_data=auth_data,
---> 16                           custom_prefixes=prefixes,
     17                           handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
     18                           batching=True)

NameError: name 'prefixes' is not defined

image.png

エラーの原因としては、prefixes の定義が抜けていることです。

prefixes = {
    "skos": "<http://www.w3.org/2004/02/skos/core#>",
}

エラーの再現方法と対応方法

事前準備

1. (省略可)neo4j Aura の作成

image.png

2. Google Colab のノートブックを作成

image.png

3. Google Colab 上で rdflib-neo4j のインストール

!pip install rdflib-neo4j -q

image.png

エラーの再現方法

1. neo4j Aura に対する認証情報を設定したコード実行がエラーとなることを確認

from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
from rdflib import Graph

# set the configuration to connect to your Aura DB
AURA_DB_URI="your_db_uri"
AURA_DB_USERNAME="neo4j"
AURA_DB_PWD="your_db_pwd"

auth_data = {'uri': AURA_DB_URI,
             'database': "neo4j",
             'user': AURA_DB_USERNAME,
             'pwd': AURA_DB_PWD}

# Define your custom mappings & store config
config = Neo4jStoreConfig(auth_data=auth_data,
                          custom_prefixes=prefixes,
                          handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
                          batching=True)

file_path = 'https://github.com/jbarrasa/gc-2022/raw/main/search/onto/concept-scheme-skos.ttl'

# Create the RDF Graph, parse & ingest the data to Neo4j, and close the store(If the field batching is set to True in the Neo4jStoreConfig, remember to close the store to prevent the loss of any uncommitted records.)
neo4j_aura = Graph(store=Neo4jStore(config=config))
# Calling the parse method will implictly open the store
neo4j_aura.parse(file_path, format="ttl")
neo4j_aura.close(True)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-0498e7fe3371> in <cell line: 15>()
     14 # Define your custom mappings & store config
     15 config = Neo4jStoreConfig(auth_data=auth_data,
---> 16                           custom_prefixes=prefixes,
     17                           handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
     18                           batching=True)

NameError: name 'prefixes' is not defined

image.png

エラーの対応方法

1. prefixes 変数を定義

prefixes = {
    "skos": "<http://www.w3.org/2004/02/skos/core#>",
}

image.png

2. 再度エラーとなったコードを実行

from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
from rdflib import Graph

# set the configuration to connect to your Aura DB
AURA_DB_URI="your_db_uri"
AURA_DB_USERNAME="neo4j"
AURA_DB_PWD="your_db_pwd"

auth_data = {'uri': AURA_DB_URI,
             'database': "neo4j",
             'user': AURA_DB_USERNAME,
             'pwd': AURA_DB_PWD}

# Define your custom mappings & store config
config = Neo4jStoreConfig(auth_data=auth_data,
                          custom_prefixes=prefixes,
                          handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
                          batching=True)

file_path = 'https://github.com/jbarrasa/gc-2022/raw/main/search/onto/concept-scheme-skos.ttl'

# Create the RDF Graph, parse & ingest the data to Neo4j, and close the store(If the field batching is set to True in the Neo4jStoreConfig, remember to close the store to prevent the loss of any uncommitted records.)
neo4j_aura = Graph(store=Neo4jStore(config=config))
# Calling the parse method will implictly open the store
neo4j_aura.parse(file_path, format="ttl")
neo4j_aura.close(True)

image.png

3. neo4j 上でデータを確認

image.png

事後処理

1. (省略可) neo4j Aura のインスタンスを削除

image.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?