Code Monkey home page Code Monkey logo

rdflib-neo4j's Introduction

rdflib-neo4j

RDFLib Store backed by neo4j + n10s

With rdflib-neo4j you will be able to make your RDFLib code interact (read/write) with a Neo4j Graph Database through the neosemantics(n10s) plugin

If you're not familiar with RDFLib you can learn more here.

If you're not familiar with n10s you can learn more here.

Getting Started... if you can install n10s on your DB. (For AuraDB check the following section)

Here are the steps you need to follow on your Neo4j database and on your python code:

On the Neo4j side

In order to set up your Neo4j Graph DB to work with RDF data you need to do two things:

1. Install the n10s plugin

You can do it in a couple of clicks from the neo4j desktop (plugins section of your DB)

Or you can do it manually following the instructions in the manual

2. Initialise the Database

Connect to your database and either from the browser or the console, create a uniqueness constraint on Resources' uri by running the following cypher fragment:

CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;
  • Set the configuration of the graph: The simplest way of doing it is by running the following cypher expression: (all default options)
CALL n10s.graphconfig.init();

If you want to know more about additional config options, have a look at the n10s reference.

On the Python side

rdflib-neo4j can be installed with Python's package management tool pip:

$ pip install rdflib-neo4j

You're ready to go!

You can now create an RDFLib graph and load RDF data in it. Every single triple will be transparently persisted in your Neo4j DB. Here's how:

You can import the data from an RDF document (for example this one serialised using N-Triples):

import rdflib

# create a neo4j backed Graph
g = rdflib.Graph(store='neo4j-n10s')

# set the configuration to connect to your Neo4j DB 
theconfig = {'uri': "neo4j://localhost:7687", 'database': '<your_db_name>', 'auth': {'user': "neo4j", 'pwd': "<your_pwd>"}}

g.open(theconfig, create = False)

g.load("https://raw.githubusercontent.com/jbarrasa/datasets/master/rdf/music.nt", format="nt")

# For each foaf:Person in the store, print out their mbox property's value.
print("--- printing band's names ---")
for band in g.subjects(rdflib.RDF.type, rdflib.URIRef("http://neo4j.com/voc/music#Band")):
    for bandName in g.objects(band, rdflib.URIRef("http://neo4j.com/voc/music#name")):
        print(bandName)

This fragment retunrs a listing with the names of the bands in the dataset and at the same time, has populated your Neo4j DB with a graph like this one:

You can also write to the graph triple by triple like this:

MUSIC = rdflib.Namespace("http://neo4j.com/voc/music#")
fm = rdflib.URIRef("http://neo4j.com/indiv#Fleetwood_Mac")

g.add((fm, rdflib.RDF.type, MUSIC.Band))
g.add((fm, MUSIC.name, rdflib.Literal("Fleetwood Mac")))

Where rdflib.RDFS is the RDFS Namespace, MUSIC is a custom namespace for the vocabulary used in our dataset, and rdflib.URIRef and rdflib.Literal are used to construct URI objects and Literal objects from their string representations.

The previous fragment would add another node to the graph representing the band Fleetwood Mac.

It is also possible to serialise your graph like this:

print(g.serialize(format="turtle").decode("utf-8"))

Producing the following result (truncated):

<http://neo4j.com/indiv#The_Beatles> a <http://neo4j.com/voc/music#Artist>,
        <http://neo4j.com/voc/music#Band> ;
    <http://neo4j.com/voc/music#member> <http://neo4j.com/indiv#George_Harrison>,
        <http://neo4j.com/indiv#John_Lennon>,
        <http://neo4j.com/indiv#Paul_McCartney>,
        <http://neo4j.com/indiv#Ringo_Starr> ;
    <http://neo4j.com/voc/music#name> "The Beatles"^^<http://www.w3.org/2001/XMLSchema#string> .
    
    [...]

Getting Started... when you cannot install n10s (for example if you're using AuraDB)

warning: This is a (WIP) limited option that only supports RDF import. Watch this space for updates.

Here are the steps you need to follow on your Neo4j database and on your python code:

On the Neo4j side

In order to set up your Neo4j Graph DB, all you need to do is nitialise the Database by creating a uniqueness constraint on Resources' uri by running the following cypher fragment:

CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;

On the Python side

rdflib-neo4j can be installed with Python's package management tool pip:

$ pip install rdflib-neo4j

You're ready to go!

You can now import RDF data into your Aura instance by creating an RDFLib graph and using it to parse the RDF data. Every single triple will be transparently persisted in your Aura DB. Here's how:

You can import the data from an RDF document (for example this one serialised using N-Triples):

from rdflib import Graph, store

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

# set the configuration to connect to your Aura DB 
theconfig = {'uri': "neo4j+s://<your_db_id>.databases.neo4j.io", 'database': 'neo4j', 'auth': {'user': "neo4j", 'pwd': "<DB-PWD>"}}

g.open(theconfig, create = False)

# optional batch loading. If parse is not wrapped into start+end batch, then triples will be written to AuraDB one by one
# performance will be significantly reduced
g.store.startBatchedWrite()
g.parse("https://github.com/jbarrasa/gc-2022/raw/main/search/onto/concept-scheme-skos.ttl", format="ttl")
g.store.endBatchedWrite()

The imported file contains a taxonomy of technologies extracted from Wikidata and serialised using SKOS. After running the previous code fragment, your Aura DB should be populated with a graph like this one:

You can also write to the graph triple by triple like this:

from rdflib import RDF, SKOS

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

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

The previous fragment would add another node to the graph representing AuraDB as a concept related to Neo4j via skos:narrower, which in your AuraDB graph would look as follows:

rdflib-neo4j's People

Contributors

jbarrasa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.