Cayley
Search…
Quickstart as Library
Currently, Cayley supports being used as a Go library for other projects. To use it in such a way, here's a quick example:
1
package main
2
3
import (
4
"fmt"
5
"log"
6
7
"github.com/cayleygraph/cayley"
8
"github.com/cayleygraph/quad"
9
)
10
11
func main() {
12
// Create a brand new graph
13
store, err := cayley.NewMemoryGraph()
14
if err != nil {
15
log.Fatalln(err)
16
}
17
18
store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello World!", nil))
19
20
// Now we create the path, to get to our data
21
p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course"))
22
23
// Now we iterate over results. Arguments:
24
// 1. Optional context used for cancellation.
25
// 2. Flag to optimize query before execution.
26
// 3. Quad store, but we can omit it because we have already built path with it.
27
err = p.Iterate(nil).EachValue(nil, func(value quad.Value){
28
nativeValue := quad.NativeOf(value) // this converts RDF values to normal Go types
29
fmt.Println(nativeValue)
30
})
31
if err != nil {
32
log.Fatalln(err)
33
}
34
}
Copied!
To use other backends, you can empty-import them, eg
1
import _ "github.com/cayleygraph/cayley/graph/kv/bolt"
Copied!
And use them with a call like
1
import "github.com/cayleygraph/cayley/graph"
2
3
func open() {
4
// Initialize the database
5
graph.InitQuadStore("bolt", path, nil)
6
7
// Open and use the database
8
cayley.NewGraph("bolt", path, nil)
9
}
Copied!
More runnable examples are available in examples folder.
Last modified 2yr ago
Copy link