Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/jbarrasa/goingmeta/llms.txt

Use this file to discover all available pages before exploring further.

Session 12 of Going Meta (broadcast January 16, 2023) demonstrates how to use the rdflib-neo4j adapter to treat Neo4j AuraDB as a native RDFLib graph store. Rather than exporting RDF to a file and importing it separately, you configure RDFLib’s Graph object to write triples directly into AuraDB over the neo4j+s:// protocol — making Python and the semantic web stack first-class citizens for populating your cloud graph database.

Watch Recording

Full session recording on YouTube

Source Code

Jupyter notebook and supporting files

Overview

BroadcastJanuary 16, 2023
TagsPython RDFLib AuraDB
Key libraryrdflib-neo4j

What You Will Learn

  • How the rdflib-neo4j package exposes a neo4j-cypher store backend for RDFLib
  • Connecting an RDFLib Graph to a live Neo4j AuraDB instance
  • Writing individual RDF triples with g.add()
  • Bulk-loading RDF files with g.parse() and batched writes
  • Adding SKOS concepts programmatically and linking them to Wikidata entities

Step-by-Step Walkthrough

1

Install the adapter

Install rdflib-neo4j from PyPI. This single package pulls in both RDFLib and the Neo4j Python driver.
pip install rdflib-neo4j
2

Create a Neo4j-backed RDFLib Graph

Instantiate an RDFLib Graph using the neo4j-cypher store identifier. At this point no connection is made yet.
from rdflib import Graph, store

# create a neo4j backed Graph
g = Graph(store='neo4j-cypher')
3

Configure and open the AuraDB connection

Pass your AuraDB connection details as a dictionary. The neo4j+s:// scheme enforces TLS, which AuraDB requires.
# set the configuration to connect to your Aura DB
theconfig = {
    'uri': "neo4j+s://<DB-ID-HERE>.databases.neo4j.io",
    'database': 'neo4j',
    'auth': {'user': "neo4j", 'pwd': "<PWD-HERE>"}
}

g.open(theconfig, create=False)
Replace <DB-ID-HERE> with your AuraDB instance ID and <PWD-HERE> with the generated password from the AuraDB console.
4

Add triples programmatically

Use RDFLib’s Namespace helper and g.add() to write individual subject–predicate–object triples. Each call translates into a Cypher write against AuraDB.
from rdflib import RDF, SKOS, URIRef, Literal
from rdflib import Namespace

indiv = Namespace("http://neo4j.com/indiv#")
voc   = Namespace("http://neo4j.com/voc#")

alex = indiv.AlexErdl
jb   = indiv.JesusBarrasa

g.add((alex, RDF.type,          voc.Person))
g.add((alex, RDF.type,          voc.Neo4jEmployee))
g.add((alex, voc.name,          Literal("Alex Erdl")))
g.add((alex, voc.twitterHandle, Literal("https://twitter.com/alexandererdl")))

g.add((jb, RDF.type,          voc.Person))
g.add((jb, RDF.type,          voc.Neo4jEmployee))
g.add((jb, voc.name,          Literal("Jesus Barrasa")))
g.add((jb, voc.twitterHandle, Literal("https://twitter.com/barrasadv")))

g.add((jb, voc.friend_of, alex))
5

Bulk-load an RDF file with batched writes

For larger datasets, wrap g.parse() between startBatchedWrite() and endBatchedWrite() to group Cypher writes into efficient batches instead of one round-trip per triple.
# When loading larger datasets it's strongly recommended to batch the writes
# with startBatchedWrite() and endBatchedWrite()
# If parse is not wrapped into start+end batch, then triples will be written
# to AuraDB one by one and performance will be significantly affected

g.store.startBatchedWrite()
g.parse(
    "https://github.com/jbarrasa/gc-2022/raw/main/search/onto/concept-scheme-skos.ttl",
    format="ttl"
)
g.store.endBatchedWrite()
Always use the batched write API for any g.parse() call that loads more than a handful of triples. The performance difference on AuraDB is significant.
6

Add SKOS concepts and link to Wikidata

Individual g.add() calls work for incremental updates. Here a new skos:Concept node is created and linked to its broader Wikidata entity.
from rdflib import RDF, SKOS, URIRef, Literal

aura = URIRef("http://neo4j.com/voc/tech#AuraDB")

g.add((aura, RDF.type,        SKOS.Concept))
g.add((aura, SKOS.prefLabel,  Literal("AuraDB")))
g.add((aura, SKOS.broader,    URIRef("http://www.wikidata.org/entity/Q1628290")))

Key Concepts

Neo4jStore backendrdflib-neo4j registers a custom RDFLib store called neo4j-cypher. When you call Graph(store='neo4j-cypher'), RDFLib routes all triple reads and writes through Cypher queries executed on Neo4j, meaning the graph you interact with through RDFLib’s API is the same property graph you can query with Cypher. Batched writes — The startBatchedWrite() / endBatchedWrite() API accumulates triples in memory and flushes them in bulk. This is essential for loading ontologies or large SKOS concept schemes where thousands of triples are involved. SKOS on AuraDB — Because rdflib-neo4j understands standard RDF vocabularies, SKOS concepts (skos:Concept, skos:prefLabel, skos:broader) are stored and remain queryable both via RDFLib’s SPARQL engine and directly through Cypher.
The rdflib-neo4j adapter is maintained by the Neo4j team. See the rdflib-neo4j GitHub repository for the latest installation instructions and changelog.

Resources

rdflib-neo4j on GitHub

Official adapter repository with full documentation

Neo4j AuraDB

Fully managed cloud graph database

RDFLib Documentation

RDFLib Python library reference

SKOS Reference

W3C Simple Knowledge Organization System

Build docs developers (and LLMs) love