github.com/RedisGraph/redisgraph-go

module github.com/RedisGraph/redisgraph-go

v2.0.1+incompatible
Latest Go to latest
Published: Oct 20, 2019 | License: BSD-3-Clause

README

license CircleCI GitHub issues Codecov Go Report Card GoDoc

Mailing List Gitter

redisgraph-go

redisgraph-go is a Golang client for the RedisGraph module. It relies on redigo for Redis connection management and provides support for RedisGraph’s QUERY, EXPLAIN, and DELETE commands.

Installation

Simply do:

$ go get github.com/redislabs/redisgraph-go

Usage

package main

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

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

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

    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`
    rs, _ := graph.Query(query)

    rs.PrettyPrint()

    // Access individual result record.
    // As long as there are records to consume.
    for rs.Next() {
        // Get current record.
        r := rs.Record()

        p_name := r.GetByIndex(0).(string)
        p_age := r.GetByIndex(1).(int)
        c_name := r.GetByIndex(2).(string)
        fmt.Printf("p_name: %s p_age: %d c_name: %s\n", p_name, p_age, c_name)
    }
}

Running the above should output:

$ go run main.go
+----------+-----------+--------+
|  p.name  |   p.age   | c.name |
+----------+-----------+--------+
| John Doe | 33.000000 | Japan  |
+----------+-----------+--------+

Running tests

A simple test suite is provided, and can be run with:

$ go test

The tests expect a Redis server with the RedisGraph module loaded to be available at localhost:6379

License

redisgraph-go is distributed under the BSD3 license - see LICENSE

Source: github.com/RedisGraph/redisgraph-go@v2.0.1+incompatible/README.md