This article shares very first step of Graph database tutorial for beginners.
Sample code
https://github.com/maclt/sample-graphdb
Overview
Glossary
Prerequisites
Open JDK 23.0.2
Maven
Step 1. Prepare Docker image
Download docker image and run the container1
$ docker pull neo4j$ docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
neo4jNeo4j has GUI called Neo4j Browser2. 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 Gin3.
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 APIStep 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 protocol4 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 uCREATE… define the action(u:User {name: $name})… define the information of the node to create.uis alias of the user which was created (similar toASkeyword 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 2Usernodes which matches to the condition searched by thenameattribute.CREATE… create 2 relationships below(a)-[:MARRY]->(b)… createMARRYrelationship fromatob(b)-[:MARRY]->(a)... createMARRYrelationship frombtoa
You can create the relationships via REST API endpoint POST /marriage. On the Neo4j browser, you see the result below.





