How to Query Virtuoso from Neo4j using JDBC

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

  1. 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
  2. Open your neo4j.conf file and add openlink.* to your dbms.security.procedures.unrestricted parameter value.
  3. Restart your Neo4j instance.
  4. Test that the custom procedure has successfully installed by running:
CALL dbms.procedures() YIELD name
WHERE name STARTS WITH 'openlink'
RETURN name;
  1. If successful, you can now use openlink.virtuoso_jdbc_connect()

For Developers Who Like to Get Hands-On

  1. Clone the Repository

    • Clone the GitHub repo:
      git clone https://github.com/danielhmills/virtuoso-to-neo4j-handler.git
      
  2. Build the .JAR File

    • Navigate to the project directory and build the .JAR file:
      mvn clean package
      
  3. Install the .JAR and JDBC Driver

    • Copy the .JAR file and the included JDBC Driver to the ‘plugins’ directory of your Neo4j installation.
  4. Configure Neo4j

    • Open your neo4j.conf file.
    • Add openlink.* to the dbms.security.procedures.unrestricted parameter value.
  5. Restart Neo4j

    • Restart your Neo4j instance.
  6. 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().

Running Queries with Your New Procedure

This custom procedure accepts the following parameters:

  • jdbc_url: Your JDBC URL string or variable
  • query: Your SQL or SPARQL-within-SQL query
  • read/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);