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

## 概要

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

```text
---------------------------------------------------------------------------
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/4bd5030f-a9ac-8bf1-ab1c-44cc9a304b22.png)

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

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

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

### 事前準備

#### 1. （省略可）neo4j Aura の作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/d38e87c3-c207-1ab5-d542-244472c276b0.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/36e73fa1-a5c7-66f1-0e59-8ad464d2b07e.png)

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

```python
!pip install rdflib-neo4j -q
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/f04a9494-0439-1051-540f-5776f10c8611.png)

### エラーの再現方法

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


```python
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)
```

```text
---------------------------------------------------------------------------
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/4bd5030f-a9ac-8bf1-ab1c-44cc9a304b22.png)


### エラーの対応方法

#### 1. prefixes 変数を定義

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/e47d9cb6-72c6-9783-4597-faadc59c4e95.png)

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

```python
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/20e7b7e1-8a30-8e1b-1417-725a9c4030eb.png)

#### 3. neo4j 上でデータを確認

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/1b0a127e-a7f8-af40-6d28-0beb25f9db49.png)

### 事後処理

### 1. （省略可） neo4j Aura のインスタンスを削除

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/24031/9d66ec59-8e2a-3eaf-8414-3a08040d9da3.png)