List all nodes from store #
Problem #
How can I retrieve and print all nodes from the store?
Solution #
Create Test Data: #
package main
import (
"github.com/cayleygraph/quad"
// Add some predefined vocabularies
"github.com/cayleygraph/quad/voc/rdf"
"github.com/tombenke/cayley-cookbook-src/kbase/voc/foaf"
)
func makeQuads() []quad.Quad {
// Generate Blank Nodes to represent the people internally
luke := quad.RandomBlankNode()
leia := quad.IRI("starwars:leia_organa") //quad.RandomBlankNode()
// Create Quads about to export
label := "people"
quads := []quad.Quad{}
quads = append(quads, quad.Make(luke, quad.IRI(rdf.Type), quad.IRI(foaf.Person), label))
quads = append(quads, quad.Make(luke, quad.IRI(foaf.GivenName), "Luke", label))
quads = append(quads, quad.Make(luke, quad.IRI(foaf.FamilyName), "Skywalker", label))
quads = append(quads, quad.Make(luke, quad.IRI(foaf.Age), 23, label))
quads = append(quads, quad.Make(leia, quad.IRI(rdf.Type), quad.IRI(foaf.Person), label))
quads = append(quads, quad.Make(leia, quad.IRI(foaf.Knows), luke, label))
quads = append(quads, quad.Make(leia, quad.IRI(foaf.GivenName), "Leia", label))
quads = append(quads, quad.Make(leia, quad.IRI(foaf.FamilyName), "Organa", label))
return quads
}
Retrieve and list all nodes from the store #
package main
import (
"fmt"
"log"
"reflect"
"context"
"github.com/cayleygraph/cayley"
)
func main() {
// Create a brand new graph
store, err := cayley.NewMemoryGraph()
if err != nil {
log.Fatalln(err)
}
// Create Quads about some people using Blank Nodes as references
quads := makeQuads()
// Add quads to the store
for _, q := range(quads) {
store.AddQuad(q)
}
printAllNodes(store)
}
func printAllNodes(store *cayley.Handle) {
// Get the iterator that enumerates all nodes in the graph.
it := store.NodesAllIterator()
fmt.Printf("%v %v\n", it, reflect.TypeOf(it))
for it.Next(context.Background()) {
ref := it.Result()
key := ref.Key()
value := store.NameOf(ref)
fmt.Printf("Ref: %v %v\n", ref, reflect.TypeOf(ref))
fmt.Printf("Key: %v %v\n", key, reflect.TypeOf(key))
fmt.Printf("Value: %v %v\n", value, reflect.TypeOf(value))
fmt.Println()
}
}
Results:
MemStoreAll *memstore.AllIterator
Ref: 1 memstore.bnode
Key: 1 memstore.bnode
Value: _:n4748188061887615063 quad.BNode
Ref: 2 memstore.bnode
Key: 2 memstore.bnode
Value: <rdf:type> quad.IRI
Ref: 3 memstore.bnode
Key: 3 memstore.bnode
Value: <foaf:Person> quad.IRI
Ref: 4 memstore.bnode
Key: 4 memstore.bnode
Value: "people" quad.String
Ref: 6 memstore.bnode
Key: 6 memstore.bnode
Value: <foaf:givenName> quad.IRI
Ref: 7 memstore.bnode
Key: 7 memstore.bnode
Value: "Luke" quad.String
Ref: 9 memstore.bnode
Key: 9 memstore.bnode
Value: <foaf:familyName> quad.IRI
Ref: 10 memstore.bnode
Key: 10 memstore.bnode
Value: "Skywalker" quad.String
Ref: 12 memstore.bnode
Key: 12 memstore.bnode
Value: <foaf:age> quad.IRI
Ref: 13 memstore.bnode
Key: 13 memstore.bnode
Value: "23"^^<xsd:integer> quad.Int
Ref: 15 memstore.bnode
Key: 15 memstore.bnode
Value: <starwars:leia_organa> quad.IRI
Ref: 17 memstore.bnode
Key: 17 memstore.bnode
Value: <foaf:knows> quad.IRI
Ref: 19 memstore.bnode
Key: 19 memstore.bnode
Value: "Leia" quad.String
Ref: 21 memstore.bnode
Key: 21 memstore.bnode
Value: "Organa" quad.String