Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Neo4j JDBC Driver and 3.1.0 Released

DZone's Guide to

Neo4j JDBC Driver and 3.1.0 Released

Neo4j's latest version of their JDBC driver is now out. Come find out what can be done with this update along with how to use it and some examples.

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

During his opening keynote address, Neo4j CEO Emil Eifrem called GraphConnect Europe “my favorite day of the year.” Without a doubt, this is true for all of the graph-addicted living in Europe, including Neo4j partners like us. At LARUS, we actually call it “THE conference!”

Year after year, more and more IT experts (developers, architects, database administrators, and managers) are attending the conference. Also, the number of talks is growing incredibly. This year, they were organized in four tracks, just confirming how many companies are using graph databases to deliver their solutions with success.

And don’t forget all of the collateral events. The day before, there were a lot of trainings (from Fundamentals to Graph Data Modeling and Neo4j in Production) and the lovely evening hackathon, this time centered around the theme of data journalism (also to celebrate the Pulitzer Prize recently awarded to the ICIJ for the Panama Papers investigation).

But for us partners, this is also an important occasion to join our long-term friends and work on the projects we’re contributing to with the Neo4j core team — finally face-to-face!

Integrate Neo4j With All the Things

As a Neo4j integrator leader, LARUS specializes in building any required connector for most of the products and solutions already adopted by our customers: BI or Reporting tools, other NoSQL databases, libraries, frameworks, ETL tools, etc.

In fact, we love to help customers to embrace Neo4j as easily and happily as possible while at the same time taking care of their existing IT ecosystem and architecture (such as banks, telecom providers, governments and retailers).

At this year’s GraphConnect Europe, Omar Rampado CTO at LARUS, presented a brand new version of our Couchbase-Neo4j Connector, now renamed Doc2Graph since it now works with MongoDB and any generic JSON API as well.

If you’re interested, the Community Edition is available on our GitHub for free.

Besides its own integration projects, LARUS also officially contributes to many Neo4j projects. As coordinated by Michael Hunger (Neo4j Community Caretaker-General and creator of many neo4j-contrib projects), we are currently working on:

The Neo4j JDBC Driver

In particular, LARUS is the official developer and maintainer of the Neo4j JDBC Driver.

Sun Microsystems released JDBC as part of Java Development Kit (JDK) 1.1 on February 19, 1997, and 20 years later, the vast majority of products, solutions, tools, frameworks, and IDEs use a JDBC Driver in order to connect with an underlying database.

That’s why in January 2016, we started implementing a brand new version of the Neo4j JDBC Driver that allows any Java application to run Cypher queries against Neo4j via the standard JDBC API.

Compared with its previous 2.x version, this driver comes with two major benefits:

  1. The Bolt binary protocol: introduced since Neo4j 3.0 to speedup performances
  2. Modularization: HTTP and Bolt protocols are available both as an “all-in” package and as two separated jars (minimizing this way the specific driver size, by skipping unnecessary dependencies for unwanted protocols).

Two New Releases (3.0.3 and 3.1.0) Are Now Available

In London, I luckily met Konstantin Lutovich from the official Java Bolt Driver team, and he patiently explained to me all the details about the new 1.3 version. With his support, we successfully updated the Neo4j JDBC Driver in order to make use of this latest Java Bolt Driver release 1.3 and together with Michael Hunger we finally released two new versions of it:

  1. Neo4j JDBC Driver v. 3.0.3, working with Neo4j 3.0.x.
  2. Neo4j JDBC Driver v. 3.1.0, the first release working with Neo4j 3.1.x.

They both contain the following fixes and updates:

  • Update to Neo4j 3.0.9 (or 3.1.4 respectively)
  • Makes use of the latest neo4j-java-driver version 1.3.0
  • Makes use of the latest http-client version 4.5.3
  • Makes use of the latest jackson version 2.8.8
  • Only shades the neo4j-jdbc-driver project instead of the two neo4j-jdbc-bolt and neo4j-jdbc-http modules (not shaded)
  • Corrected bug that switched to non-autocommit mode when there was an error
  • Improvement on BoltDatabaseMetaData
  • Better alignment of BoltStatement.execute with BoltPreparedStatement.execute
  • BoltDatabaseMetadata.getTables implementation returning Labels list
  • Mybatis example added
  • fix, int and float conversion to string
  • ResultSet.getInt/Long/Float/Double now return 0 for NULL values
  • ResultSet.getBoolean returns false for NULL values

Start Using the JDBC Driver for Neo4j in 5 Minutes

  1. Download the installation package Neo4j JDBC Driver 3.0.3 for Neo4j 3.0.x (here) or Neo4j JDBC Driver 3.1.0 for Neo4j 3.1.x (here).
    • Please notice that these two jars are shaded, which means that all the dependencies are nested inside. In case you have version conflicts with your application’s dependencies then you can use the not-shaded versions: Bolt 3.0.3 (here) or 3.1.0 (here), HTTP 3.0.3 (here), or 3.1.0 (here).
  2. Configure the connection. The JDBC Driver for Neo4j can be used with any Cypher query or reporting software that supports JDBC. The connection information is:
    • JDBC Driver class names (automatically registered):
      • Generic: org.neo4j.jdbc.Driver
        • Bolt: org.neo4j.jdbc.bolt.BoltDriver
        • HTTP: org.neo4j.jdbc.http.HttpDriver
      • URL format:
        • Bolt: jdbc:neo4j:bolt://<serverName>/<databaseName>
        • HTTP: jdbc:neo4j:http://<serverName>/<databaseName>

As part of the connection string, a series of connection arguments can be passed to the driver that will affect its behavior. Please refer to the official documentation for a detailed description of these parameters.

Code Walkthrough With Bolt

//
// Create a new instance of the JDBC Driver for Neo4j and connect to the URL:
//
String url = "jdbc:neo4j:bolt://localhost";
try (Connection con = DriverManager.getConnection(url, "neo4j", "neo4j")) {
  //
  // Create a PreparedStatement and submit a Cypher query:
  //
  String sql = "MATCH (u:User)-[:FRIEND]-(f:User) " +
               "WHERE u.name = {1} " +
               "RETURN f.name as name, f.age as age";

  try (PreparedStatement stmt = con.prepareStatement(query)) {
    stmt.setString(1,"John");
    //
    // Iterate and print out the result set
    //
    try (ResultSet rs = stmt.execute()) {
      while (rs.next()) {  
        System.out.println(
          "Friend: " + rs.getString("name") + " is " + rs.getInt("age"));
      }
    }
  }
}

Please notice that ConnectionStatement/PreparedStatement, and ResultSet are all AutoCloseable and this can better protect from any eventual resource leak.

Just change your URL to jdbc:neo4j:http://localhost in order to make use of the HTTP protocol instead of the Bolt one.

Next Steps

We’re currently testing the Neo4j JDBC Driver with the newly released Neo4j 3.2.0 and adding new examples.

Also, we’d love to take advantage of the bolt+routing protocol in order to make read-only queries to access a Causal Cluster’s Read Replica nodes. The general idea would be to access these instances as soon as the java.sql.Connection is set to read-only:

String url = "jdbc:neo4j:bolt://localhost";
try (Connection con = DriverManager.getConnection(url, "neo4j", "neo4j")) {
  //
  // Switching connection to read only will make all queries access
  // to Read Replica nodes
  //
  con.setReadOnly(true);

  String sql = "MATCH (u:User)-[:FRIEND]-(f:User) " +
               "WHERE u.name = {1} " +
               "RETURN f.name as name, f.age as age";

  try (PreparedStatement stmt = con.prepareStatement(query)) {
    // ...
  }
}

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
database ,neo4j ,jdbc ,jdbc driver

Published at DZone with permission of Lorenzo Speranzoni. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}