Skip to content

Commit

Permalink
Updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdejong committed Jan 23, 2024
1 parent a08adf2 commit 5c3040d
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 39 deletions.
3 changes: 0 additions & 3 deletions docs/getting_started.adoc

This file was deleted.

16 changes: 0 additions & 16 deletions docs/introduction.adoc

This file was deleted.

7 changes: 6 additions & 1 deletion docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
* xref:index.adoc[Introduction]
* xref:index.adoc[Introduction]
* xref:gettingstarted.adoc[Getting Started]
* xref:neo4jstore.adoc[Neo4j Store]
* xref:neo4jstoreconfig.adoc[Store Configuration]
* xref:examples.adoc[Examples]
* xref:contributing.adoc[Contributing]
17 changes: 17 additions & 0 deletions docs/modules/ROOT/pages/contributing.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Contributing

Contributions to the project are highly welcomed.
If you extend the library with custom functionality, consider creating a Pull Request on our GitHub repository.


We highly recommend to familiarize yourself with the RDFLib core library. You can https://github.com/RDFLib/rdflib/#getting-started[learn more here].


Contribution checklist:

- Find or create an https://github.com/neo4j-labs/rdflib-neo4j/issues[issue] on GitHub.
- Fork the repository, create your own feature branch starting from the `develop` branch.
- Document your code with docstrings or in the documentation (`docs` folder), if applicable.
## Feature Requests / Bugs
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.
149 changes: 149 additions & 0 deletions docs/modules/ROOT/pages/examples.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
= Examples

This page contains some code snippets with examples on using the library.

== Importing a TTL file
This a basic example for importing a single TTL file.
Insert your own database credentials for `AURA_DB_URI`, `AURA_DB_USERNAME`, `AURA_DB_PWD` to use this template.

[source,python]
----
from rdflib_neo4j import Neo4jStoreConfig
from rdflib_neo4j import HANDLE_VOCAB_URI_STRATEGY
# Get your Aura Db free instance here: https://neo4j.com/cloud/aura-free/#test-drive-section
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}
from rdflib import Namespace
# Define your prefixes
prefixes = {
'neo4ind': Namespace('http://neo4j.org/ind#'),
'neo4voc': Namespace('http://neo4j.org/vocab/sw#'),
'nsmntx': Namespace('http://neo4j.org/vocab/NSMNTX#'),
'apoc': Namespace('http://neo4j.org/vocab/APOC#'),
'graphql': Namespace('http://neo4j.org/vocab/GraphQL#')
}
# Define your custom mappings
config = Neo4jStoreConfig(auth_data=auth_data,
custom_prefixes=prefixes,
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
batching=True)
from rdflib_neo4j import Neo4jStore
from rdflib import Graph
file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl'
graph_store = Graph(store=Neo4jStore(config=config))
graph_store.parse(file_path,format="ttl")
graph_store.close(True)
----

== Advanced Examples

=== Initialize Neo4jStore

[source,python]
----
from rdflib_neo4j import Neo4jStoreConfig, Neo4jStore, HANDLE_VOCAB_URI_STRATEGY
from rdflib import Namespace, Graph, URIRef, RDF, SKOS, Literal
# Define your custom prefixes
prefixes = {
'neo4ind': Namespace('http://neo4j.org/ind#'),
'neo4voc': Namespace('http://neo4j.org/vocab/sw#'),
}
# Neo4j connection credentials
auth_data = {'uri': 'your_neo4j_uri',
'database': 'neo4j',
'user': "neo4j",
'pwd': 'your_password'}
# Define your Neo4jStoreConfig
config = Neo4jStoreConfig(auth_data=auth_data,
custom_prefixes=prefixes,
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
batching=False)
neo4j_store = Neo4jStore(config=config)
graph_store = Graph(store=neo4j_store)
----

=== Import by Reference URL

[source,python]
----
file_path = 'https://raw.githubusercontent.com/neo4j-labs/neosemantics/3.5/docs/rdf/nsmntx.ttl'
graph_store.parse(file_path,format="ttl")
----

=== Write Individual Triples

[source,python]
----
aura = URIRef("http://neo4j.com/voc/tech#AuraDB")
graph_store.add((aura, RDF.type, SKOS.Concept))
graph_store.add((aura, SKOS.prefLabel, Literal("AuraDB")))
graph_store.add((aura, SKOS.broader, URIRef("http://neo4j.org/ind#neo4j355")))
----

=== SPARQL Query with Batching

[source,python]
----
import requests
import urllib.parse
endpoint = "https://id.nlm.nih.gov/mesh/sparql"
sparql = """
PREFIX rdfs:
PREFIX meshv:
PREFIX mesh:
PREFIX rdf:
CONSTRUCT { ?s ?p ?o }
FROM
WHERE {
{
?s ?p ?o
filter(?s = mesh:D000086402 || ?o = mesh:D000086402)
}
union
{
mesh:D000086402 ?x ?s .
?s ?p ?o .
filter(?x != rdf:type && (isLiteral(?o) || ?p = rdf:type))
}
union
{
?s ?x mesh:D000086402 .
?s ?p ?o .
filter(isLiteral(?o|| ?p = rdf:type))
}
}
"""
# Define your Neo4jStoreConfig
config = Neo4jStoreConfig(auth_data=auth_data,
custom_prefixes=prefixes,
handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE,
batching=True)
neo4j_store = Neo4jStore(config=config)
graph_store = Graph(store=neo4j_store)
query_response = requests.get(endpoint, params = {"query": sparql , "format" : "TURTLE"})
graph_store.parse(data=query_response.text,format='ttl')
graph_store.close(commit_pending_transaction=True)
----
88 changes: 88 additions & 0 deletions docs/modules/ROOT/pages/gettingstarted.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
= Getting Started

This page describes how to get started with this library, and set up your first RDF import.

== Set up Neo4j
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:

[source,cypher]
----
CREATE CONSTRAINT n10s_unique_uri FOR (r:Resource) REQUIRE r.uri IS UNIQUE;
----
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.

== Set up Python environment
`rdflib-neo4j` can be installed with Python's package management tool `pip`:

[source,shell]
----
$ pip install rdflib-neo4j
----

== Loading data
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:

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]):

[source,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)
----

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

image::https://raw.githubusercontent.com/neo4j-labs/rdflib-neo4j/master/img/graph-view-aura.png[height="400"]

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

[source,python]
----
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("http://neo4j.com/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")))
----

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:

image::https://raw.githubusercontent.com/neo4j-labs/rdflib-neo4j/master/img/graph-view-aura-detail.png[height="150"]
14 changes: 13 additions & 1 deletion docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
jsdhdfvjfdvfv
# RDFLib-Neo4j

The **rdflib-neo4j** project is a Python-based https://rdflib.readthedocs.io/en/stable/[RDFLib Store] backed by Neo4j.
You can use this library for high-performance RDF data ingestion into the Neo4j database.

This library works with all types of Neo4j deployments, whether on-premise or cloud-hosted (Neo4j Aura).

## Documentation

- To get started, see the link:quickstart[Quickstart] page.
- For details on the available Python classes, see the link:neo4jstore[Neo4j Store] page.
- Example code fragments are available under link:examples[Examples].
- If you want to contribute to this project, see link:contributing[Contributing].
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
== Neo4j Store
= Neo4j Store
[.procedures, opts=header]

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.

=== Object Initialization
== Constructor
|===
| Name | Type | Required | Default | Description
|config|Neo4jStoreConfig|True||Neo4jStoreConfig object that contains all the useful informations to initialize the store.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
== Neo4j Store Config
= Neo4j Store Config
[.procedures, opts=header]

This object is used to configure the Neo4j Store to connect to your Neo4j Instance and to manage the parsing of a Triple Store.

=== Object Initialization
== Constructor
|===
| Name | Type | Required | Values(Default) | Description
| auth_data | Dictionary | True | ("uri", "database", "user", "pwd") | A dictionary containing authentication data. The required keys are: ["uri", "database", "user", "pwd"].
| batching | Boolean | False | boolean (True) | A boolean indicating whether batching is enabled.
| batch_size | Integer | False | (5000) | An integer representing the batch size (The batch size is intended as number of entities to store inside the database (nodes/relationships) and not triples.
| custom_mappings | List[Tuple[Str,Str,Str]] | False | Empty list | A list of tuples containing custom mappings for prefixes in the form (prefix, object_to_replace, new_object).
| custom_prefixes | Dictionary | True if handle_vocab_uri_strategy == HANDLE_VOCAB_URI_STRATEGY.SHORTEN | ({}) | A dictionary containing custom prefixes.
| handle_vocab_uri_strategy | HANDLE_VOCAB_URI_STRATEGY | False |HANDLE_VOCAB_URI_STRATEGY.IGNORE, HANDLE_VOCAB_URI_STRATEGY.KEEP, HANDLE_VOCAB_URI_STRATEGY.MAP(HANDLE_VOCAB_URI_STRATEGY.SHORTEN) |
| custom_prefixes | Dictionary | True | ({}) | A dictionary containing custom prefixes.
| handle_vocab_uri_strategy | HANDLE_VOCAB_URI_STRATEGY | False |IGNORE, KEEP, MAP, (SHORTEN) |

* 'SHORTEN', full uris are shortened using prefixes for property names, relationship names and labels. Fails if a prefix is not predefined for a namespace in the imported RDF.

Expand All @@ -21,13 +21,15 @@ This object is used to configure the Neo4j Store to connect to your Neo4j Instan

* 'KEEP' uris are kept unchanged

| handle_multival_strategy | HANDLE_MULTIVAL_STRATEGY | False | HANDLE_MULTIVAL_STRATEGY.ARRAY (HANDLE_MULTIVAL_STRATEGY.OVERWRITE)|
| handle_multival_strategy | HANDLE_MULTIVAL_STRATEGY | False | ARRAY (OVERWRITE)|
* 'OVERWRITE' property values are kept single valued. Multiple values in the imported RDF are overwriten (only the last one is kept)

* 'ARRAY' properties are stored in an array enabling storage of multiple values. All of them unless multivalPropList is set.
| multival_props_names | List[Tuple[Str,Str]] | False | ([]) | A list of tuples containing the prefix and property names to be treated as multivalued in the form (prefix, property_name).
|===

① if handle_vocab_uri_strategy == HANDLE_VOCAB_URI_STRATEGY.SHORTEN

== Functions

=== set_handle_vocab_uri_strategy
Expand Down Expand Up @@ -188,7 +190,7 @@ No arguments
| Dictionary | A dictionary containing all prefixes.
|===

== Enumerators
== Enumerated Values

=== HANDLE_VOCAB_URI_STRATEGY

Expand All @@ -204,12 +206,6 @@ Enum class defining different strategies for handling vocabulary URIs.
| IGNORE | Strategy to ignore the Namespace and get only the local part
|===

=== Examples

Here a series of examples of the application of a strategy on a certain triplet.

#TODO: Fill the examples#

=== Shorten

This strategy will shorten the URIs, replacing the prefix with its shorted version. If the Store find a prefix not defined inside its Neo4jStoreConfig object, the parsing will stop, raising a ShortenStrictException error.
Expand All @@ -229,7 +225,7 @@ This strategy will remove the entire prefix from the predicate.

Enum class defining different strategies for handling multiple values.

TO NOTICE: If the strategy is ARRAY and the Neo4jStoreConfig doesn't contain any predicate marked as multivalued, EVERY field will be treated as multivalued.
> If the strategy is ARRAY and the Neo4jStoreConfig doesn't contain any predicate marked as multivalued, EVERY field will be treated as multivalued.

==== Possible Values

Expand All @@ -239,9 +235,6 @@ TO NOTICE: If the strategy is ARRAY and the Neo4jStoreConfig doesn't contain any
| ARRAY | Strategy to treat multiple values as an array
|===

=== Examples

Here a series of examples of the application of a strategy on a certain triplet.

=== Overwrite

Expand Down

0 comments on commit 5c3040d

Please sign in to comment.