You can find the full documentation at: http://neo4j-contrib.github.io/neo4j-jdbc/
This is a JDBC driver for Neo4j supporting Cypher over JDBC.
This driver was mainly developed by Larus BA, Italy, a certified consulting and integration solutions partner for Neo4j. Thank you so much for all your work.
Being a graph database, Neo4j is not serving data in a relational way, nevertheless thanks to this driver it’s possible for projects that are using the classic JDBC connector in the relational paradigm to interact with Neo4j.
This driver supports various types of database transports, through:
-
The
Bolt
protocol usingjdbc:neo4j:bolt://<host>:<port>/
from Neo4j 3.0+ -
The
Bolt+Routing
protocol usingjdbc:neo4j:bolt+routing://<host>:<port>/
from Neo4j 3.1+ to 3.5.x -
The
neo4j
protocol usingjdbc:neo4j:neo4j://<host>:<port>/
from Neo4j 4.0+ -
The
HTTP
protocol usingjdbc:neo4j:http://<host>:<port>/
from Neo4j 2.0+
Going forward there will also be support for:
-
direct file connection
-
embedded connection
Note
|
The previous JDBC driver for Neo4j 2.x was moved to the https://github.com/neo4j-contrib/neo4j-jdbc-2x repository. |
Neo4j-JDBC | Neo4j version | Neo4j Driver | Java | Protocols | Temporal and Spatial |
---|---|---|---|---|---|
3.4.x - 3.5.x |
1.7.5 |
1.8 |
http, bolt, bolt+routing |
yes |
|
3.5.x - 4.x |
4.0.1 |
11 |
http, bolt, bolt+routing |
yes |
For the all-in-one module supporting both Bolt and HTTP, you can simply use:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>{neo4j-jdbc-version}</version>
</dependency>
If you want only one of the protocols, you can depend on its module:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-bolt</artifactId>
<version>{neo4j-jdbc-version}</version>
</dependency>
or
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-http</artifactId>
<version>{neo4j-jdbc-version}</version>
</dependency>
org.neo4j:neo4j-jdbc-driver:4.0.0
// Connecting
try (Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost", 'neo4j', password)) {
// Querying
String query = "MATCH (u:User)-[:FRIEND]-(f:User) WHERE u.name = {1} RETURN f.name, f.age";
try (PreparedStatement stmt = con.prepareStatement(query)) {
stmt.setString(1,"John");
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.println("Friend: "+rs.getString("f.name")+" is "+rs.getInt("f.age"));
}
}
}
}
Please note that the example above uses the try-with-resource blocks that automatically closes resources when the try
block is exited.
-
Install a Neo4j 3.3.x server and start it with the Bolt protocol enabled
-
Connect with URLs in the following 3 forms:
-
jdbc:neo4j:http://<host>:<port>/
(e.g.jdbc:neo4j:http://localhost/
) -
jdbc:neo4j:bolt://<host>:<port>/
(e.g.jdbc:neo4j:bolt://localhost/
) -
jdbc:neo4j:bolt+routing://<host>:<port>/
(e.g.jdbc:neo4j:bolt+routing://localhost?routing:policy=eu
)
-
-
You can also use additional parameters in the URL separated by an
&
character for authentication, debug mode, SSL encryption and flattening e.g.jdbc:neo4j:bolt://localhost/?user=neo4j&password=xxxx&debug=true&nossl&flatten=[-1,100,1000]
Note
|
We’ve deprecated the usage of , as the parameter separator in favour of & to be compliant with the URL parameter syntax.
|
-
Add the JDBC driver dependency or jar file to your project
-
Get a connection from
DriverManager
-
Execute queries and transactions using the Cypher graph query language
Property Name | Supported Values | Description |
---|---|---|
trust.strategy |
TRUST_ALL_CERTIFICATES, TRUST_CUSTOM_CA_SIGNED_CERTIFICATES, TRUST_SYSTEM_CA_SIGNED_CERTIFICATES |
The supported trusted strategies |
trusted.certificate.file |
File Path |
The path of the certificate file |
connection.acquisition.timeout |
Any Long |
The acquisition time |
connection.liveness.check.timeout |
Any Long |
The liveness check timeout |
connection.timeout |
Any Long |
The connection timeout |
encryption |
true/false |
Activate the application encryption |
leaked.sessions.logging |
true/false |
If log leaked session |
max.connection.lifetime |
Any Long |
The connection lifetime |
max.connection.poolsize |
Any Int |
The max pool size |
max.transaction.retry.time |
Any Long |
The retry time for a transaction transient error |
database |
String |
The database name, if not specified connects to the default instance |
readonly |
true/false |
If specified creates a fixed read only connection, any further modification via the |
usebookmarks |
true/false |
If specified disables the bookmarks |
As most JDBC clients and tools don’t support complex objects, the driver can flatten returned nodes and relationships by providing all their properties as individual columns with names like u.name
,r.since
if you just return a node u
or relationship r
.
This is enabled with the JDBC-URL parameter flatten=<rows>
, where <rows>
indicates how many rows are sampled to determine those columns.
With -1
all rows are sampled and with any other value you determine the number of rows being looked at.
When the JDBC driver is configured as a JNDI resource into Tomcat, you must include these two arguments on Resource
configuration:
-
removeAbandonedOnBorrow="true"
-
closeMethod="close"
Here’s an example:
<Resource name="jdbc/neo4j"
auth="Container"
type="javax.sql.DataSource"
username="neo4j"
password="password"
driverClassName="org.neo4j.jdbc.bolt.BoltDriver"
url="jdbc:neo4j:bolt://localhost"
removeAbandonedOnBorrow="true"
closeMethod="close"
/>
First clone the repository.
This project is composed by the following modules:
-
Neo4j JDBC - the core module
-
Neo4j JDBC - Bolt - module supporting the Bolt protocol
-
Neo4j JDBC - HTTP - module supporting the HTTP protocol
-
Neo4j JDBC - Driver packaging - module to package all above modules
mvn clean test
mvn clean test -Pintegration-test
mvn clean test -Pperformance-test
Note
|
To run the performance test, you must have a Neo4j Server 3.3.x running with the Bolt protocol enabled on port 7687 (default). |
Copyright (c) 2017 Neo4j and LARUS Business Automation
The "Neo4j JDBC Driver" is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
Please provide feedback and report bugs as GitHub issues or join the neo4j-users Slack and ask on the #neo4j-jdbc channel.
You might also ask on StackOverflow, please tag your question there with neo4j
and jdbc
.