Skip to content

Commit

Permalink
add redis seed script example and docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Pires committed Jan 10, 2023
1 parent d63f3ab commit a7b60d4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 82 deletions.
Empty file added Dockerfile
Empty file.
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'

services:
redisgraph:
image: redis/redis-stack-server:latest
ports:
- 6379:6379
83 changes: 1 addition & 82 deletions repositories/redisgraph.go
Original file line number Diff line number Diff line change
@@ -1,82 +1 @@
package main

import (
"fmt"
"os"

"github.com/gomodule/redigo/redis"
rg "github.com/redislabs/redisgraph-go"
)

func main() {
conn, _ := redis.Dial("tcp", "127.0.0.1:6379")
defer conn.Close()

graph := rg.GraphNew("social", conn)

graph.Delete()

john := rg.Node{
Label: "person",
Properties: map[string]interface{}{
"name": "John Doe",
"age": 33,
"gender": "male",
"status": "single",
},
}
graph.AddNode(&john)

japan := rg.Node{
Label: "country",
Properties: map[string]interface{}{
"name": "Japan",
},
}
graph.AddNode(&japan)

edge := rg.Edge{
Source: &john,
Relation: "visited",
Destination: &japan,
}
graph.AddEdge(&edge)

graph.Commit()

query := `MATCH (p:person)-[v:visited]->(c:country)
RETURN p.name, p.age, c.name`

// result is a QueryResult struct containing the query's generated records and statistics.
result, _ := graph.Query(query)

// Pretty-print the full result set as a table.
result.PrettyPrint()

// Iterate over each individual Record in the result.
fmt.Println("Visited countries by person:")
for result.Next() { // Next returns true until the iterator is depleted.
// Get the current Record.
r := result.Record()

// Entries in the Record can be accessed by index or key.
pName := r.GetByIndex(0)
fmt.Printf("\nName: %s\n", pName)
pAge, _ := r.Get("p.age")
fmt.Printf("\nAge: %d\n", pAge)
}

// Path matching example.
query = "MATCH p = (:person)-[:visited]->(:country) RETURN p"
result, err := graph.Query(query)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Pathes of persons visiting countries:")
for result.Next() {
r := result.Record()
p, ok := r.GetByIndex(0).(rg.Path)
fmt.Printf("%s %v\n", p, ok)
}
}
package repositories
82 changes: 82 additions & 0 deletions scripts/redisgraph/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"fmt"
"os"

"github.com/gomodule/redigo/redis"
rg "github.com/redislabs/redisgraph-go"
)

func main() {
conn, _ := redis.Dial("tcp", "127.0.0.1:6379")
defer conn.Close()

graph := rg.GraphNew("social", conn)

graph.Delete()

john := rg.Node{
Label: "person",
Properties: map[string]interface{}{
"name": "John Doe",
"age": 33,
"gender": "male",
"status": "single",
},
}
graph.AddNode(&john)

japan := rg.Node{
Label: "country",
Properties: map[string]interface{}{
"name": "Japan",
},
}
graph.AddNode(&japan)

edge := rg.Edge{
Source: &john,
Relation: "visited",
Destination: &japan,
}
graph.AddEdge(&edge)

graph.Commit()

query := `MATCH (p:person)-[v:visited]->(c:country)
RETURN p.name, p.age, c.name`

// result is a QueryResult struct containing the query's generated records and statistics.
result, _ := graph.Query(query)

// Pretty-print the full result set as a table.
result.PrettyPrint()

// Iterate over each individual Record in the result.
fmt.Println("Visited countries by person:")
for result.Next() { // Next returns true until the iterator is depleted.
// Get the current Record.
r := result.Record()

// Entries in the Record can be accessed by index or key.
pName := r.GetByIndex(0)
fmt.Printf("\nName: %s\n", pName)
pAge, _ := r.Get("p.age")
fmt.Printf("\nAge: %d\n", pAge)
}

// Path matching example.
query = "MATCH p = (:person)-[:visited]->(:country) RETURN p"
result, err := graph.Query(query)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Pathes of persons visiting countries:")
for result.Next() {
r := result.Record()
p, ok := r.GetByIndex(0).(rg.Path)
fmt.Printf("%s %v\n", p, ok)
}
}

0 comments on commit a7b60d4

Please sign in to comment.