Skip to content

Commit 23d0635

Browse files
Merge pull request #34 from neo4j-labs/develop
Release 1.1
2 parents c7fd1b5 + 3e932d4 commit 23d0635

25 files changed

+836
-172
lines changed

docs/getting_started.adoc

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/introduction.adoc

Lines changed: 0 additions & 16 deletions
This file was deleted.

docs/modules/ROOT/nav.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
* xref:index.adoc[Introduction]
1+
* xref:index.adoc[Introduction]
2+
* xref:gettingstarted.adoc[Getting Started]
3+
* xref:neo4jstore.adoc[Neo4j Store]
4+
* xref:neo4jstoreconfig.adoc[Store Configuration]
5+
* xref:examples.adoc[Examples]
6+
* xref:contributing.adoc[Contributing]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Contributing
2+
3+
Contributions to the project are highly welcomed.
4+
If you extend the library with custom functionality, consider creating a Pull Request on our GitHub repository.
5+
6+
7+
We highly recommend to familiarize yourself with the RDFLib core library. You can https://github.com/RDFLib/rdflib/#getting-started[learn more here].
8+
9+
10+
Contribution checklist:
11+
12+
- Find or create an https://github.com/neo4j-labs/rdflib-neo4j/issues[issue] on GitHub.
13+
- Fork the repository, create your own feature branch starting from the `develop` branch.
14+
- Document your code with docstrings or in the documentation (`docs` folder), if applicable.
15+
16+
## Feature Requests / Bugs
17+
If you have a request for a feature, or have found a bug, creating an https://github.com/neo4j-labs/rdflib-neo4j/issues[issue on GitHub] is the best way to reach out.

docs/modules/ROOT/pages/examples.adoc

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
= Examples
2+
3+
This page contains some code snippets with examples on using the library.
4+
5+
== Importing a TTL file
6+
This a basic example for importing a single TTL file.
7+
Insert your own database credentials for `AURA_DB_URI`, `AURA_DB_USERNAME`, `AURA_DB_PWD` to use this template.
8+
9+
[source,python]
10+
----
11+
from rdflib_neo4j import Neo4jStoreConfig
12+
from rdflib_neo4j import HANDLE_VOCAB_URI_STRATEGY
13+
14+
# Get your Aura Db free instance here: https://neo4j.com/cloud/aura-free/#test-drive-section
15+
AURA_DB_URI="your_db_uri"
16+
AURA_DB_USERNAME="neo4j"
17+
AURA_DB_PWD="your_db_pwd"
18+
19+
auth_data = {'uri': AURA_DB_URI,
20+
'database': "neo4j",
21+
'user': AURA_DB_USERNAME,
22+
'pwd': AURA_DB_PWD}
23+
from rdflib import Namespace
24+
25+
# Define your prefixes
26+
prefixes = {
27+
'neo4ind': Namespace('http://neo4j.org/ind#'),
28+
'neo4voc': Namespace('http://neo4j.org/vocab/sw#'),
29+
'nsmntx': Namespace('http://neo4j.org/vocab/NSMNTX#'),
30+
'apoc': Namespace('http://neo4j.org/vocab/APOC#'),
31+
'graphql': Namespace('http://neo4j.org/vocab/GraphQL#')
32+
}
33+
# Define your custom mappings
34+
config = Neo4jStoreConfig(auth_data=auth_data,
35+
custom_prefixes=prefixes,
36+
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
37+
batching=True)
38+
from rdflib_neo4j import Neo4jStore
39+
from rdflib import Graph
40+
file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl'
41+
42+
graph_store = Graph(store=Neo4jStore(config=config))
43+
graph_store.parse(file_path,format="ttl")
44+
graph_store.close(True)
45+
----
46+
47+
== Advanced Examples
48+
49+
=== Initialize Neo4jStore
50+
51+
[source,python]
52+
----
53+
from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
54+
from rdflib import Namespace, Graph, URIRef, RDF, SKOS, Literal
55+
56+
57+
# Define your custom prefixes
58+
prefixes = {
59+
'neo4ind': Namespace('http://neo4j.org/ind#'),
60+
'neo4voc': Namespace('http://neo4j.org/vocab/sw#'),
61+
}
62+
63+
# Neo4j connection credentials
64+
auth_data = {'uri': 'your_neo4j_uri',
65+
'database': 'neo4j',
66+
'user': "neo4j",
67+
'pwd': 'your_password'}
68+
69+
# Define your Neo4jStoreConfig
70+
config = Neo4jStoreConfig(auth_data=auth_data,
71+
custom_prefixes=prefixes,
72+
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
73+
batching=False)
74+
75+
neo4j_store = Neo4jStore(config=config)
76+
graph_store = Graph(store=neo4j_store)
77+
78+
----
79+
80+
=== Import by Reference URL
81+
82+
[source,python]
83+
----
84+
file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl'
85+
graph_store.parse(file_path,format="ttl")
86+
----
87+
88+
=== Write Individual Triples
89+
90+
[source,python]
91+
----
92+
aura = URIRef("http://neo4j.com/voc/tech#AuraDB")
93+
94+
graph_store.add((aura, RDF.type, SKOS.Concept))
95+
graph_store.add((aura, SKOS.prefLabel, Literal("AuraDB")))
96+
graph_store.add((aura, SKOS.broader, URIRef("http://neo4j.org/ind#neo4j355")))
97+
98+
----
99+
100+
=== SPARQL Query with Batching
101+
102+
[source,python]
103+
----
104+
import requests
105+
import urllib.parse
106+
107+
endpoint = "https://id.nlm.nih.gov/mesh/sparql"
108+
sparql = """
109+
PREFIX rdfs:
110+
PREFIX meshv:
111+
PREFIX mesh:
112+
PREFIX rdf:
113+
114+
CONSTRUCT { ?s ?p ?o }
115+
FROM
116+
WHERE {
117+
{
118+
?s ?p ?o
119+
filter(?s = mesh:D000086402 || ?o = mesh:D000086402)
120+
}
121+
union
122+
{
123+
mesh:D000086402 ?x ?s .
124+
?s ?p ?o .
125+
filter(?x != rdf:type && (isLiteral(?o) || ?p = rdf:type))
126+
}
127+
union
128+
{
129+
?s ?x mesh:D000086402 .
130+
?s ?p ?o .
131+
filter(isLiteral(?o|| ?p = rdf:type))
132+
}
133+
}
134+
"""
135+
136+
# Define your Neo4jStoreConfig
137+
config = Neo4jStoreConfig(auth_data=auth_data,
138+
custom_prefixes=prefixes,
139+
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
140+
batching=True)
141+
142+
neo4j_store = Neo4jStore(config=config)
143+
graph_store = Graph(store=neo4j_store)
144+
145+
query_response = requests.get(endpoint, params = {"query": sparql , "format" : "TURTLE"})
146+
graph_store.parse(data=query_response.text,format='ttl')
147+
graph_store.close(commit_pending_transaction=True)
148+
149+
----
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
= Getting Started
2+
3+
This page describes how to get started with this library, and set up your first RDF import.
4+
5+
== Set up Neo4j
6+
To configure your Neo4j Graph DB, the process is simplified: initialize the database by establishing a uniqueness constraint on Resources' URIs. You can achieve this by executing the following Cypher fragment:
7+
8+
[source,cypher]
9+
----
10+
CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;
11+
----
12+
This constraint ensures the uniqueness of URIs for Resource nodes, streamlining the integration process. Alternatively, you can simply set `create=True` when attempting to open the store in your Python code, and it will create the constraint for you.
13+
14+
== Set up Python environment
15+
`rdflib-neo4j` can be installed with Python's package management tool `pip`:
16+
17+
[source,shell]
18+
----
19+
$ pip install rdflib-neo4j
20+
----
21+
22+
== Loading data
23+
Now, seamlessly import RDF data into your Neo4j On-premise or Aura instance by establishing an RDFLib graph and employing it to parse your RDF data. Each individual triple undergoes transparent persistence within your Neo4j database(whether it is on Aura or on-premise). Here's a step-by-step guide to achieve this integration:
24+
25+
You can import the data from an RDF document (for example link:https://github.com/jbarrasa/datasets/blob/master/rdf/music.nt[this one serialised using N-Triples]):
26+
27+
[source,python]
28+
----
29+
from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
30+
from rdflib import Graph
31+
32+
# set the configuration to connect to your Aura DB
33+
AURA_DB_URI="your_db_uri"
34+
AURA_DB_USERNAME="neo4j"
35+
AURA_DB_PWD="your_db_pwd"
36+
37+
auth_data = {'uri': AURA_DB_URI,
38+
'database': "neo4j",
39+
'user': AURA_DB_USERNAME,
40+
'pwd': AURA_DB_PWD}
41+
42+
# Define your custom mappings & store config
43+
config = Neo4jStoreConfig(auth_data=auth_data,
44+
custom_prefixes=prefixes,
45+
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
46+
batching=True)
47+
48+
file_path = 'https://github.com/jbarrasa/gc-2022/raw/main/search/onto/concept-scheme-skos.ttl'
49+
50+
# 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.)
51+
neo4j_aura = Graph(store=Neo4jStore(config=config))
52+
# Calling the parse method will implictly open the store
53+
neo4j_aura.parse(file_path, format="ttl")
54+
neo4j_aura.close(True)
55+
----
56+
57+
The imported file contains a taxonomy of technologies extracted from Wikidata and serialised using SKOS.
58+
After running the previous code fragment, your Aura DB/Neo4j DB should be populated with a graph like this one:
59+
60+
image::https://raw.githubusercontent.com/neo4j-labs/rdflib-neo4j/master/img/graph-view-aura.png[height="400"]
61+
62+
You can also write to the graph triple by triple like this:
63+
64+
[source,python]
65+
----
66+
import rdflib
67+
from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
68+
from rdflib import Graph, RDF, SKOS
69+
70+
# Set up your store config
71+
config = Neo4jStoreConfig(auth_data=auth_data,
72+
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
73+
batching=False)
74+
75+
# Create the graph and open the store
76+
neo4j_aura = Graph(store=Neo4jStore(config=config))
77+
neo4j_aura.open(config)
78+
79+
aura = rdflib.URIRef("http://neo4j.com/voc/tech#AuraDB")
80+
81+
neo4j_aura.add((aura, RDF.type, SKOS.Concept))
82+
neo4j_aura.add((aura, SKOS.prefLabel, rdflib.Literal("AuraDB")))
83+
neo4j_aura.add((aura, SKOS.broader, rdflib.URIRef("http://www.wikidata.org/entity/Q1628290")))
84+
----
85+
86+
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:
87+
88+
image::https://raw.githubusercontent.com/neo4j-labs/rdflib-neo4j/master/img/graph-view-aura-detail.png[height="150"]

docs/modules/ROOT/pages/index.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
jsdhdfvjfdvfv
1+
# RDFLib-Neo4j
2+
3+
The **rdflib-neo4j** project is a Python-based https://rdflib.readthedocs.io/en/stable/[RDFLib Store] backed by Neo4j.
4+
You can use this library for high-performance RDF data ingestion into the Neo4j database.
5+
6+
This library works with all types of Neo4j deployments, whether on-premise or cloud-hosted (Neo4j Aura).
7+
8+
## Documentation
9+
10+
- To get started, see the link:quickstart[Quickstart] page.
11+
- For details on the available Python classes, see the link:neo4jstore[Neo4j Store] page.
12+
- Example code fragments are available under link:examples[Examples].
13+
- If you want to contribute to this project, see link:contributing[Contributing].

docs/documentation/Neo4jStore-0.1.adoc renamed to docs/modules/ROOT/pages/neo4jstore.adoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
== Neo4j Store
1+
= Neo4j Store
22
[.procedures, opts=header]
33

44
This class is an implementation of the rdflib link:https://rdflib.readthedocs.io/en/stable/_modules/rdflib/store.html[Store class] that uses Neo4j as a backend. In this way it is possible to persist you RDF data directly in Neo4j, with the power of rdflib to process your data.
55

6-
=== Object Initialization
6+
== Constructor
77
|===
88
| Name | Type | Required | Default | Description
9-
|config|Neo4jStoreConfig|True||Neo4jStoreConfig object that contains all the useful informations to initialize the store.
9+
|config|Neo4jStoreConfig|True||Neo4jStoreConfig object that contains all the useful information to initialize the store.
10+
|driver|Neo4jStoreConfig|False|None|A pre-built Neo4j driver object to use to connect to the database. You cannot specify both a driver and credentials in the Neo4jStoreConfig.
1011
|===
1112

1213
== Functions

0 commit comments

Comments
 (0)