For RDBMS Devs: Learn How to Connect to a Graph Database Using Various Language Drivers for Neo4jUp to this point in this blog series, we’ve covered graph databases in general, only using Neo4j when examples were required. However, when it comes to connecting your application to a graph database, specifics are essential.

In this RDBMS & Graphs blog series, we’ll explore how relational databases compare to their graph counterparts, including data models, query languages, deployment strategies and more. In previous weeks, we’ve explored why RDBMS aren’t always enough, graph basics for the RDBMS developer, relational vs. graph data modeling, SQL vs. Cypher as query languages and three different database deployment strategies.

This week, we’ll discuss the language drivers and APIs specific to Neo4j with plenty of resources for further exploration. At this point, if you are curious about other, non-Neo4j graph databases, we encourage you to explore the available drivers within their respective communities.

As the last post in this RDBMS & Graphs series, we’ll also provide a brief wrap-up and more RDBMS-to-graph resources.

The Neo4j Browser


If you’ve installed and started Neo4j as a server on your system, the good news is that you can already interact with the database via the built-in Neo4j Browser application.

The Neo4j Browser is similar to (but more feature-rich than) SQL*Plus, giving you a familiar and comfortable way to interact directly with your new graph database. The easiest way to connect to the Neo4j Browser is through the localhost:7474 port.

Read this article for more information on the Neo4j Browser.

However, even with the Neo4j Browser, you’ll still want your application to connect to Neo4j through other means – most often through a driver for your programming language of choice. In the rest of this post, we’ll cover the various ways to connect with Neo4j, including the HTTP-API and various language drivers.

Connecting to Neo4j via the REST API


If you want to access Neo4j programmatically, you can do so with the REST API, which allows you to:
    • POST one or more Cypher statements with parameters per request to the server
    • Keep transactions open over multiple requests
    • Choose different result formats
    • Execute management options or introspect the database
These APIs can then be used directly via an HTTP library or through a language driver (more on those far below).

Let’s look at one of the underlying remote API endpoints that Neo4j offers to execute queries. A simple HTTP Cypher request – executable in the Neo4j Browser – would look like this:

:POST /db/data/transaction/commit {"statements":[
      {"statement":"CREATE (p:Person {firstName:{name}}) RETURN p",
       "parameters":{"name":"Daniel"}}
    ]}

Another example: An HTTP request that executes Cypher to create a Person would look like this, here shown with the plain JSON response:

:POST http://localhost:7474/db/data/transaction/commit
  {"statements":[
    {"statement":"CREATE (p:Person {name:{name}}) RETURN p", "parameters":{"name":"Daniel"}}
   ]}
->
{"results":[{"columns":["p"],"data":[{"row":[{"name":"Daniel"}]}]}],"errors":[]}

The language drivers discussed below use the same APIs under the hood, but make them available in a convenient way.

Connecting to Neo4j via Language Drivers


In most cases, you won’t want to connect to Neo4j manually, but with a driver or connector library designed for your stack or programming language.

Thanks to the Neo4j community, there are Neo4j drivers for almost every popular programming language, most of which mimic existing database driver idioms and approaches.

Some of the most popular language drivers for Neo4j include: Below, we’ll take a closer look at two drivers with the greatest familiarity for RDBMS developers: JDBC and Spring Data.

Using Neo4j Server with JDBC


If you’re a Java developer, you’re probably familiar with Java Database Connectivity (JDBC) as a way to work with relational databases, whether directly or via abstractions like Spring’s JDBCTemplate or MyBatis. Many other tools use JDBC drivers to interact with relational databases for business intelligence, data management or ETL (Extract, Transform, Load).

Cypher – like SQL – is a textual and parameterizable query language capable of returning tabular results, so Neo4j supports the JDBC APIs through a Neo4j-JDBC driver.

Let’s look at an example query using the JDBC driver. Working from the organizational data model example from a few weeks ago, here’s what a query for finding John’s department would look like using the Neo4j-JDBC driver:

Connection con = DriverManager.getConnection("jdbc:neo4j://localhost:7474/");

String query =
    "MATCH (:Person {name:{1}})-[:EMPLOYEE]-(d:Department) RETURN d.name as dept";
try (PreparedStatement stmt = con.prepareStatement(QUERY)) {
    stmt.setString(1,"John");
    ResultSet rs = stmt.executeQuery();
    while(rs.next()) {
        String department = rs.getString("dept");
        ....
    }
}

More details on how to use the Neo4j-JDBC driver can be found with our example project for Java-JDBC. There, we implement the backend as a minimal Java web app that leverages Neo4j-JDBC to connect to Neo4j Server.

Using Spring Data Neo4j


Spring Data Neo4j integrates the Neo4j-OGM library to provide fast and comprehensive object graph mapping. Additionally, it provides support for Spring conversions, transaction handling, Spring-Data-Repositories, Spring-Data REST and Spring-Boot.

To get started, add Spring Data Neo4j as a dependency, then configure the necessary beans in your Java config. Then you can use Neo4jTemplate to manage your entities (optionally annotated) and define Spring-Data-Repositories as convenient interfaces to your persistence layer.

Here are more resources on using Spring Data Neo4j: This material gives only a glimpse at the many ways to connect your application with Neo4j. Whether you’re connecting via the Neo4j Browser, via the HTTP-API or via a language driver of your choice, you can find more in-depth and up-to-date resources in the Developer Guides for Neo4j.

Series Conclusion: Never Write Another JOIN


The recent proliferation of NoSQL database technologies is a testament to the fact that relational databases aren’t the right tool for every job.

While relational databases are powerful tools for the right use case and the right architecture, today’s user and business needs are changing. As we’ve seen above, today’s complex, real-world data is increasing in volume, velocity and variety, and the data relationships – which are often more valuable than the data itself – are growing at an even faster rate.

Leveraging those connected data relationships are where graph databases shine. When you make the switch to graph databases – whether as your main or secondary data store – you can capture that rich relationship information in a way your RDBMS never can.

Furthermore, using a graph database for your next application allows you to match changes in your database schema with the speed of business agility. And once your application is in production, a graph database allows you to query connected data at speeds and performance levels that no RDBMS can match.

Because of the natural fit of the graph database model to business domains and processes, Forrester Research estimates that at least 25% of enterprises worldwide will use graph databases by 2017, while Gartner Research reports that graph databases are the fastest-growing category in database management systems, and that “by the end of 2018, 70 percent of leading organizations will have one or more pilot or proof-of-concept efforts underway utilizing graph databases.”

Instead of imposing a connected data model onto a traditional RDBMS – with the accompanying struggle against intensive and frustrating JOINs – it’s time to use a graph database for every use case where data relationships matter most.

In the end, business won’t wait and neither will users. When you choose the right tool for your connected-data application, you never have to write another JOIN. Even better: You reduce query times from minutes to milliseconds.

With so many ways to get started quickly, mastering graph database development is one of the best time investments you can make.

Other Resources: More on Graph Databases for the RDBMS Developer


This blog series has only scratched the surface when it comes to how today’s RDBMS developer can best take advantage of graph databases.

For more insights, tips and tricks on the intersection of relational and graph databases, explore any of the many resources below:

Articles and Presentations: Webinars: Books: Trainings:


Want to learn more on how relational databases compare to their graph counterparts? Download this ebook, The Definitive Guide to Graph Databases for the RDBMS Developer, and discover when and how to use graphs in conjunction with your relational database.

Get the Ebook


Catch up with the rest of the RDBMS & Graphs series:

 

Keywords:  


About the Author

Michael Hunger, Ryan Boyd & William Lyon, Developer Relations Team

Michael Hunger has been passionate about software development for a very long time. For the last few years he has been working with Neo4j on the open source Neo4j graph database filling many roles. As caretaker of the Neo4j community and ecosystem he especially loves to work with graph-related projects, users and contributors. As a developer Michael enjoys many aspects of programming languages, learning new things every day, participating in exciting and ambitious open source projects and contributing and writing software related books and articles.

Ryan Boyd is a speaker, author, developer and developer relations guru with a background in coding, leadership and management.

William Lyon is a Developer Relations Engineer at Neo4j. Prior to joining Neo, William worked as software developer for several startups in the real estate, quantitative finance and predictive API spaces. William holds a Masters degree in Computer Science from the University of Montana.


Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe

Upcoming Event

 

Neo4j GraphTour Register Now

From the CEO

Emil's Blog


Have a Graph Question?

Stackoverflow
Slack
Contact Us

Share your Graph Story?

Email us: content@neotechnology.com


Popular Graph Topics