How to Query Virtuoso from Neo4j using JDBC
Ever wanted to seamlessly run SPARQL queries within your SQL in Neo4j? Now you can with our Virtuoso-to-Neo4j Handler!
Here’s a friendly guide to get you started.
Why You’ll Love This
It showcases how you bring the data connectivity magic of hyperlinks, aided by Linked Data principles, to Neo4j.
Quick Installation Guide
- Download the best matching .JAR file from the Github repo’s Releases Section and the Virtuoso JDBC Driver into your Neo4j installation’s
plugins
directory - Open your
neo4j.conf
file and addopenlink.*
to yourdbms.security.procedures.unrestricted
parameter value. - Restart your Neo4j instance.
- Test that the custom procedure has successfully installed by running:
CALL dbms.procedures() YIELD name
WHERE name STARTS WITH 'openlink'
RETURN name;
- If successful, you can now use openlink.virtuoso_jdbc_connect()
For Developers Who Like to Get Hands-On
-
Clone the Repository
- Clone the GitHub repo:
git clone https://github.com/danielhmills/virtuoso-to-neo4j-handler.git
- Clone the GitHub repo:
-
Build the .JAR File
- Navigate to the project directory and build the .JAR file:
mvn clean package
- Navigate to the project directory and build the .JAR file:
-
Install the .JAR and JDBC Driver
- Copy the .JAR file and the included JDBC Driver to the ‘plugins’ directory of your Neo4j installation.
-
Configure Neo4j
- Open your
neo4j.conf
file. - Add
openlink.*
to thedbms.security.procedures.unrestricted
parameter value.
- Open your
-
Restart Neo4j
- Restart your Neo4j instance.
-
Verify Installation
- Run the following command to check if the procedure is installed correctly:
CALL dbms.procedures() YIELD name WHERE name STARTS WITH 'openlink' RETURN name;
- If successful, you can now use
openlink.virtuoso_jdbc_connect()
.
- Run the following command to check if the procedure is installed correctly:
Running Queries with Your New Procedure
This custom procedure accepts the following parameters:
jdbc_url
: Your JDBC URL string or variablequery
: Your SQL or SPARQL-within-SQL queryread/write
(default = null)'r'
= read'rw'
= read/write
Tip: It’s always a good idea to set the correct privileges on your Virtuoso instance rather than relying solely on this parameter.
Executing SPARQL Queries
SPARQL queries can be executed using SPARQL-within-SQL. This functionality is triggered by adding SPARQL
at the beginning of your query.
Here’s a quick example using a Federated SPARQL Query:
WITH 'jdbc:virtuoso://localhost:1111/UID=demo/PWD=demo/CHARSET=UTF-8' AS url
CALL openlink.virtuoso_jdbc_connect(
url,
'SPARQL SELECT ?person1 ?person2 WHERE { SERVICE <https://linkeddata.uriburner.com/sparql/>{ SELECT * FROM <urn:analytics> WHERE {?person1 foaf:knows ?person2} }}',
'r'
) YIELD value
UNWIND value AS row
WITH row['person1'] AS person1, row['person2'] AS person2
MERGE (p1:Person {id: person1})
MERGE (p2:Person {id: person2})
MERGE (p1)-[:KNOWS]->(p2);