DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j With Scala: REST Endpoints with Akka-HTTP

Neo4j With Scala: REST Endpoints with Akka-HTTP

By using Neo4j and Scala with Akka-HTTP, you are able to create REST endpoints and try to connect with the server with them.

Anurag Srivastava user avatar by
Anurag Srivastava
·
Mar. 01, 17 · Tutorial
Like (3)
Save
Tweet
Share
4.91K Views

Join the DZone community and get the full member experience.

Join For Free

Welcome back to our series of Neo4j with Scala. Let’s start our journey again. Check out the other parts of the series before reading this one:

  1. Getting Started Neo4j with Scala: An Introduction
  2. Neo4j with Scala: Defining User-Defined Procedures and APOC
  3. Neo4j with Scala: Migrate Data From Other Database to Neo4j
  4. Neo4j with Scala: Awesome Experience With Spark
  5. Neo4j vs.ElasticSearch and Full-Text Search

Today, we will discuss Neo4j and Scala with Akka-HTTP. We will create REST endpoints and try to connect with the server with them.

The basic idea behind using Akka-HTTP for creating REST endpoints is that modules implement a full server and client-side HTTP stack on top of akka-actor and akka-stream. We can customize these routes according to our use case. It’s not a web framework but rather a more general toolkit for providing and consuming HTTP-based services.

We will talk step-by-step about how to development this REST API. You will able to run this API on your server and start hitting REST endpoints.

Now we start coding:

1. build.sbt

We will start our implementation with the build.sbt file. We will keep all require dependencies in this file.

2. Factories

This is the part where we interact with the Neo4j database. This code shows how to do CRUD operations and create relationships between the nodes. We will define our ciphers here and execute them. Example:

 def retrieveRecord(email: String): Option[User] = {
    val driver = GraphDatabase.driver(neo4jUrl, AuthTokens.basic(userName, userPassword))
    val session = driver.session
    val script = s"MATCH (a:Users) WHERE a.name = '$email' RETURN a.name AS name, a.email AS " +
                 s"email, a.age AS age, a.city AS city"
    val result = session.run(script)
    val record_data: Option[User] = if (result.hasNext()) {
      val record = result.next()
      val results: User = new User(record.get("name").asString(),
        record.get("email").asString(),
        record.get("age").asInt(),
        record.get("city").asString())
      Some(results)
    } else {
      None
    }
    session.close()
    driver.close()
    record_data
  }

3. Routes

This is the part where we develop routes to provide REST endpoints to the client so that they can interact with the server and perform the operations that are mentioned in factories using REST endpoints. Example:

path("get" / "email" / Segment) { (email: String) =>
      get {
        complete {
          try {
            val idAsRDD: Option[User] = retrieveRecord(email)
            idAsRDD match {
              case Some(data) =>
                HttpResponse(StatusCodes.OK, entity = data.name)
              case None => HttpResponse(StatusCodes.InternalServerError,
                entity = s"Data is not fetched and something went wrong")
            }
          } catch {
            case ex: Throwable =>
              logger.error(ex, ex.getMessage)
              HttpResponse(StatusCodes.InternalServerError,
                entity = s"Error found for email : $email")
          }
        }
      }
    }

4. application.conf

This is the part where we define our configuration like the HTTP's interface, the port, and Neo4j’s URL, userName, and userPassword. We define this file in the resources folder.

5. boot.scala

This is the file from where we will start the server and create connections with the server.

This is the basic idea for developing REST endpoints with Akka-http. If you want to see code, you can find info here or you can just follow the README.md file.

This is a basic implementation of how to perform CRUD operation and create relation on Neo4j using the Akka-HTTP.

Here is an example URL:

0.0.0.0:8080/get/email/anurag@knoldus.com

This will return the user’s name if the user is present in the Neo4j. Otherwise, it will return data that is not fetched.

You can find the activator for the Neo4j with Scala and Akka-http here.

Reference

  • Akka HTTP documentation
REST Web Protocols Neo4j Scala (programming language)

Published at DZone with permission of Anurag Srivastava. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SAST: How Code Analysis Tools Look for Security Flaws
  • Cloud Native London Meetup: 3 Pitfalls Everyone Should Avoid With Cloud Data
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • The Data Leakage Nightmare in AI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: