A basic nquads parser in Go
N-Quads is a serialisation format for RDF datasets. A dataset consists of a default graph with no name and zero or more named graphs where a graph is a composed of a set of triples. The default graph may be empty.
An N-Quads file is a line-oriented format where each triple or quad statement is terminated by a period .
.
<
and >
"
followed by an optional language tag using @
as a delimiter, or a data type IRI using ^^
as a delimiter_:
and the same label denotes the same blank node throughout the file.A triple in a named graph may be written as a statement using four terms, the last of which is the name of the graph:
<http://example/s> <http://example/p> <http://example/o> <http://example/g> .
A triple in the default graph omits the fourth term:
<http://example/s> <http://example/p> <http://example/o> .
Example of parsing an nquads file and printing out every 5000th quad
package main
import (
"fmt"
"os"
"github.com/iand/nquads"
)
func main() {
nqfile, err := os.Open("myquads.nq")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err.Error())
os.Exit(1)
}
defer nqfile.Close()
count := 0
r := nquads.NewReader(nqfile)
for r.Next()
count++
if count % 5000 == 0{
fmt.Printf("%s\n", r.Quad())
}
}
if r.Err() != nil {
fmt.Printf("Unexpected error encountered: %v\n", r.Err())
}
}
This is free and unencumbered software released into the public domain. For more
information, see http://unlicense.org/ or the accompanying UNLICENSE
file.
The design and logic is inspired by Go's standard csv parsing library