Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

92 lines (60 sloc) 4.568 kB

Neo4j JDBC driver

This is a first attempt at creating a JDBC driver for the graph database Neo4j. While Neo4j is a graph database, and JDBC is based on the relational paradigm, this driver provides a way to bridge this gap.

This is done by introducing type nodes in the graph, which are directly related to the root node by the relationship TYPE. Each type node has a property "type" with its name (i.e. "tablename), and HAS_PROPERTY relationships to nodes that represent the properties that the node can have (i.e. "columns"). For each instance of this type (i.e. "row") there is a relationship from the instance to the type node via the IS_A relationship. By using this structure the JDBC driver can mimic a relational database, and provide a means to execute queries against the Neo4j server.

Setup

  • Install a Neo4j server, and start it with the REST API enabled.

  • Add as many TYPE nodes as you want

  • For each type node, add as many HAS_PROPERTY nodes as you want

  • For each property node, set property "name" and "type". "type" should be "String" for now

  • Add as many instance nodes as you want, with the defined properties, and relate them to their type node with the IS_A relationship

Usage

  • Add the JDBC driver jar file to your applications classpath

  • Load the driver with the class org.neo4j.jdbc.Driver

  • Connect with URLs on the form jdbc:neo4j://<host>:<port>/, e.g. jdbc:neo4j://localhost:7474/

  • Execute queries using the Cypher graph query language

To debug the driver, add ?debug=true to the URL. This will log all calls to the driver to the Java Util Logging framework.

Authentication

The driver supports authentication. You can provide the JDBC connection properties "user" and "password", and these will then be used to perform BASIC AUTH authentication while performing the REST API calls.

Quirks modes

Many of the tools below issue SQL statements in order to browse tables. For these tools to work the driver detects whether it is running in any of those environments, and if so starts a "quirks mode", whereby those standard SQL statements are replaced on the fly with the Cypher equivalents.

Using with DbVisualizer

One of the first tests I did with the driver was to connect to Neo4j using the DbVisualizer tool (www.dbvis.com/).

DBViz

Here is how you can try it out:

  • Download and install DbVisualizer

  • Add the JDBC driver jar as a Neo4j driver in the Driver Manager.

  • Add a connection to your database

  • Under "Connection properties", change "SQL Statements" as follows:

    • SELECT ALL: start n=node(0) match (n)-[:TYPE]→(type)←[:IS_A]-(instance) return instance.Firstname . Column names are hardcoded for now, as there is no * selection in Cypher yet

    • SELECT COUNT: start n=node(0) match (n)-[:TYPE]→(type)←[:IS_A]-(instance) return count(instance)

Now you should be able to browse the type nodes as tables and the nodes of those types as rows. Use the "SQL Commander" to issue arbitrary Cypher queries to the Neo4j server.

Using with LibreOffice

LibreOffice

To use the driver with LibreOffice (should be similar with OpenOffice, not tested though), follow these steps:

  • Add the driver jar to the classpath in the Java settings in LibreOffice

  • Create a new Database and select JDBC.

  • Enter the JDBC URL and set org.neo4j.jdbc.Driver as driver

  • Optionally enter authentication settings

  • Click Finish

Now you can browse the "tables" and the data, as well as create Queries that perform Cypher queries, and view the results.

Using with IntelliJ

To use the driver with IntelliJ 11, do as follows:

  • Open up the Data Sources tab

  • Click +, select the JDBC driver jar, and enter the JDBC settings for the connection

  • Switch to the SQL Console, select Properties and go to the Text Mode tab. As "Parameter pattern", enter (\{[\w]*\}). This will allow you to replace Cypher parameters with values before executing a query

Now you can browse "tables" and execute Cypher queries from within IntelliJ.

Using with ODBC on Windows

To use the driver as an ODBC data source in Windows, do as follows:

Now you can browse "tables" and execute Cypher queries from any ODBC-compliant tool.

Jump to Line
Something went wrong with that request. Please try again.