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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Keep Calm and Column Wise
  • SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete
  • Navigating the Divide: Distinctions Between Time Series Data and Relational Data
  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years

Trending

  • MLOps: Practical Lessons from Bridging the Gap Between ML Development and Production
  • Operationalizing Data Quality in Cloud ETL Workflows: Automated Validation and Anomaly Detection
  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Cognitive Architecture: How LLMs Are Changing the Way We Build Software
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j With Scala: An Introduction

Neo4j With Scala: An Introduction

Are you a Scala user who's interested in getting started with Neo4j? Look no further!

By 
Anurag Srivastava user avatar
Anurag Srivastava
·
Aug. 02, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
12.1K Views

Join the DZone community and get the full member experience.

Join For Free

When we use a relational database for storing data, we store data in predefined tables and then define foreign keys for references between tables or rows. But when it comes to graph databases, we store data in nodes and relationships. Graph databases provide us with flexibility to arrange data in an easy way.

When transforming a data model from a Relational Database Management System (RDBMS) to a graph database, here are some transformations we'll need to make:

  • Tables are represented by labels on nodes.
  • Rows in an entity table become nodes.
  • Columns on those tables become node properties.
  • Remove technical primary keys and keep business primary keys.
  • Add unique constraints for business primary keys and add indexes for frequent lookup attributes.
  • Replace foreign keys with relationships to the other table and remove them afterwards.
  • Remove data with default values — no need to store those.
  • Data in tables that are denormalized and duplicated might have to be pulled out into separate nodes to get a cleaner model.
  • Indexed column names might indicate an array property.
  • JOIN tables are transformed into relationships and columns on those tables become relationship properties.

When we want to transform our data model from a relational database, it is very important that we know about these terms and the graph data model.

We used SQL statements there for interacting with the database, and here we used Cypher statement for the same.

SQL Statement:


SELECT c.customer_id , c.customer_name FROM customer AS c WHERE c.customer_city = 'Delhi';


Cypher Statement:


Match (c: customer)
WHERE c.customer_city = 'Delhi'
RETURN c.customer_id , c.customer_name ;


Same and boring, we can write Cypher like this:


Match (c: customer{customer_city : 'Delhi'})
RETURN c.customer_id , c.customer_name ;


Now we see how we can use Scala with Neo4j. To use Neo4j, we can use neo4j-java-driver for creating Driver and Session:


libraryDependencies += "org.neo4j.driver" % "neo4j-java-driver" % "1.0.4"


Create Driver and Session:


val driver = GraphDatabase.driver("bolt://localhost/7687", AuthTokens.basic("neo4j", "neo4j"))
val session = driver.session


In Neo4j, we use the Bolt protocol. It is based on PackStream serialization and supports protocol versioning, authentication, and TLS via certificates.

Now we can create a case class where we can define its value. Here, we have a case class:


case class User(name: String, last_name: String, age: Int, city: String)


Now for the CRUD operation:


Create a Node :

val script = s"CREATE (user:Users {name:'${user.name}',last_name:'${user.last_name}',age:${user.age},city:'${user.city}'})"
val result = session.run(script)

Retrieve all Node :

val script = s"MATCH (user:Users) RETURN user.name AS name, user.last_name AS last_name, user.age AS age, user.city AS city"
val result = session.run(script)

Update a Node :

val script = s"MATCH (user:Users) where user.name ='$name' SET user.name = '$newName' RETURN user.name AS name, user.last_name AS last_name, user.age AS age, user.city AS city"
val result = session.run(script)

Delete a Node :

val script = s"MATCH (user:Users) where user.name ='$name' Delete user"
val result = session.run(script)


Now, we can create relationships between the nodes. We implement a method that takes a user's name and uses a list of those users' names with whom we want to create relation and relation_type (i.e. Friend, Family). We modify the list in a comma-separated ( “\”, \””) string and pass that in a script.


val nameOfFriends = "\"" + userList.mkString("\", \"") + "\""
val script = s"MATCH (user:Users {name: '${user_name}'}) FOREACH (name in [${nameOfFriends}] | CREATE (user)-[:$relation_name]->(:Users {name:name}))"
session.run(script)


Here, we send a user's name "Anurag," a list of Friends ("Sandy," "Manish," "Shivansh"), and set the relation between them as "Friends."Screenshot from 2016-07-22 12:25:29


Now, we create two more nodes as friends of "Sandy."

newFOF


And now we want to know the Friends of Friends. Here is the Cypher:


val script = s"MATCH (user:Users)-[:$relation_name]-(friend:Users)-[:$relation_name]-(foaf:Users) WHERE user.name = '$user_name' AND NOT (user)-[:$relation_name]-(foaf) RETURN foaf"
session.run(script)


result FOF


For deleting all relations' records, we can use this Cypher:


val script = s"MATCH (n)-[relation:$relation_name]->(r) DELETE relation"
session.run(script)


I hope this will help you to start with graph databases (Neo4j).

You can get the above working example from the GitHub repo.

Database Relational database Neo4j sql Scala (programming language) Data (computing)

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

Opinions expressed by DZone contributors are their own.

Related

  • Keep Calm and Column Wise
  • SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete
  • Navigating the Divide: Distinctions Between Time Series Data and Relational Data
  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: