入门
本页介绍了如何开始使用此库并设置您的第一个 RDF 导入。
设置 Neo4j
要配置您的 Neo4j 图数据库,该过程非常简单:通过在资源的 URI 上建立唯一性约束来初始化数据库。您可以通过执行以下 Cypher 片段来实现此目的
CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;
此约束确保了资源节点的 URI 的唯一性,从而简化了集成过程。或者,您只需在尝试在 Python 代码中打开存储时简单地设置 create=True
,它会为您创建约束。
加载数据
现在,通过建立一个 RDFLib 图并使用它来解析您的 RDF 数据,您可以将 RDF 数据无缝导入到您的 Neo4j 本地或 Aura 实例中。每个单独的三元组都会在您的 Neo4j 数据库(无论是在 Aura 上还是在本地)中进行透明的持久化。以下是如何实现此集成的分步指南
您可以从 RDF 文档导入数据(例如 此一个使用 N-Triples 序列化)
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)
导入的文件包含从 Wikidata 中提取并使用 SKOS 序列化的技术分类。在运行了前面的代码片段后,您的 Aura DB/Neo4j DB 应该填充了这样的图

您也可以像这样逐个三元组写入图
import rdflib
from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
from rdflib import Graph, RDF, SKOS
# Set up your store config
config = Neo4jStoreConfig(auth_data=auth_data,
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
batching=False)
# Create the graph and open the store
neo4j_aura = Graph(store=Neo4jStore(config=config))
neo4j_aura.open(config)
aura = rdflib.URIRef("https://neo4j.ac.cn/voc/tech#AuraDB")
neo4j_aura.add((aura, RDF.type, SKOS.Concept))
neo4j_aura.add((aura, SKOS.prefLabel, rdflib.Literal("AuraDB")))
neo4j_aura.add((aura, SKOS.broader, rdflib.URIRef("http://www.wikidata.org/entity/Q1628290")))
前面的片段会向图中添加另一个节点,表示 AuraDB 作为与 Neo4j 相关的概念,通过 skos:narrower
,在您的 AuraDB 图中将如下所示
