-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redis seed script example and docker compose
- Loading branch information
Fabio Pires
committed
Jan 10, 2023
1 parent
d63f3ab
commit a7b60d4
Showing
4 changed files
with
90 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |