This article shares very first step of Graph database tutorial for beginners.
Sample code
https://github.com/maclt/sample-graphdb
Overview
Glossary
Neo4j … open-source graph database management system designed to store, query, and manage data in the form of graphs. Neo4j graph database is composed of 2 concepts.
Node … an entity or object in the graph.
Relationship … connects two nodes and represents the relationship or association between them. 1
Cypher Query Language (CQL)2 … a declarative query language similar to SQL but designed for graph databases.
Prerequisites
Go 1.23.3
Docker or equivalent
Step 1. Prepare Docker image
Download docker image and run the container3
$ docker pull neo4j
$ docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
neo4j
Neo4j has GUI called Neo4j Browser4. Once you started the docker container, you can access to http://localhost:7474/browser/ .
The default username is “neo4j”
.
On the Neo4j browser, you can set up the password for the database. In the sample code, the password is “password”
.
Step 2. Initiate Gin
In this article I do NOT explain about Gin. Please refer the official document by Gin5.
If you are not familiar with Gin, here I share 2 commands to start the server.
$ go mod download // download the packages
$ go run main.go // run the server which exposes REST API
Step 3. Dependency Injection for DB Connection
In mod.go
file, you see a neo4j-go-driver
(link) which enable the Gin server to connect to the Neo4j database.
📄 mod.go
module maclt/graphdb/neo4j
go 1.23.3
require (
...
github.com/neo4j/neo4j-go-driver/v5 v5.27.0 // indirect
...
)
With neo4j-go-driver
, the dependency injection struct is implemented in database/database.go
file.
📄 database/database.go
package database
import (
"context"
"log"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
)
type DatabaseDriver struct {
Driver neo4j.DriverWithContext
}
// Constructor function to create a new database driver instance
func Connect() *DatabaseDriver {
uri := "bolt://localhost:7687" // Bolt protocol
username := "neo4j"
password := "password"
// Create a driver instance
driver, err := neo4j.NewDriverWithContext(uri, neo4j.BasicAuth(username, password, ""))
if err != nil {
log.Fatalf("Failed to create driver: %v", err)
}
// Return the driver instance
return &DatabaseDriver{Driver: driver}
}
// Close function to close the database driver
func (d *DatabaseDriver) Close(c context.Context) {
if err := d.Driver.Close(c); err != nil {
log.Printf("Failed to close Neo4j driver: %v", err)
}
}
Bolt protocol6 is a lightweight, binary, and efficient protocol used for communication between Neo4j clients and servers. It supports streaming result and TLS based secure communication.
Step 4. Create node
This is the CQL to create a User
node.
CREATE (u:User {name: $name}) RETURN u
CREATE
… define the action(u:User {name: $name})
… define the information of the node to create.u
is alias of the user which was created (similar toAS
keyword in SQL).RETURN u
… define the value to return as the result
You can create the node via REST API endpoint POST /users. On the Neo4j browser, you see the result below.
Step 5. Create relationship
This is the CQL to create a relationship to connect 2 User
nodes.
MATCH (a:User {name: $name1}), (b:User {name: $name2})
CREATE (a)-[:MARRY]->(b), (b)-[:MARRY]->(a)
MATCH (a:User {name: $name1}), (b:User {name: $name2})
… find 2User
nodes which matches to the condition searched by thename
attribute.CREATE
… create 2 relationships below(a)-[:MARRY]->(b)
… createMARRY
relationship froma
tob
(b)-[:MARRY]->(a)
... createMARRY
relationship fromb
toa
You can create the relationships via REST API endpoint POST /marriage. On the Neo4j browser, you see the result below.
Main References
In some other graph databases, relationship is called edge.