Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kasei/kineo

A persistent RDF quadstore and SPARQL engine
https://github.com/kasei/kineo

database endpoint graph rdf sparql

Last synced: 3 months ago
JSON representation

A persistent RDF quadstore and SPARQL engine

Awesome Lists containing this project

README

        

# Kineo

## A persistent RDF quadstore and SPARQL engine

### Build

`swift build -c release`

### Swift Package Manager

You can use the [Swift Package Manager](https://swift.org/package-manager/) to add Kineo to a Swift project by adding it as a dependency in `Package.swift`:

```swift
.package(name: "Kineo", url: "https://github.com/kasei/kineo.git", .upToNextMinor(from: "0.0.91")),
```

### Load data

Create a database file (`geo.db`) and load one or more N-Triples or Turtle files:

```
% ./.build/release/kineo -q geo.db -d examples/geo-data/geo.ttl load
```

Specifying `-d FILENAME` will load data from `FILENAME` into the default graph.
Alternatively, data can be loaded into a specific named graph (similarly, a
custom graph name can be used for the query default graph):

```
% ./.build/release/kineo -q geo.db -g http://example.org/dbpedia examples/geo-data/geo.ttl load
```

### Query

Querying of the data can be done using SPARQL:

```
% cat examples/geo-data/geo.rq
PREFIX geo:
SELECT ?s
WHERE {
?s geo:lat ?lat ;
geo:long ?long ;
FILTER(?long < -120)
FILTER(?lat >= 34.0)
FILTER(?lat <= 35.0)
}
ORDER BY ?s

% ./.build/release/kineo -q geo.db query examples/geo-data/geo.rq
Using default graph
1 Result[s: ]
2 Result[s: ]
3 Result[s: ]
4 Result[s: ]
5 Result[s: ]
6 Result[s: ]
7 Result[s: ]
8 Result[s: ]
9 Result[s: ]
10 Result[s: ]
```

### Kineo API

The Kineo API can be used to create an in-memory or persistent quadstore,
load RDF data into it, and evaluate SPARQL queries over the data:

```swift
import Foundation
import SPARQLSyntax
import Kineo

let graph = Term(iri: "http://example.org/default-graph")
let store = MemoryQuadStore()

let url = URL(string: "http://kasei.us/about/foaf.ttl")!
try store.load(url: url, defaultGraph: graph)

let sparql = "PREFIX foaf: SELECT * WHERE { ?person a foaf:Person ; foaf:name ?name }"
let q = try SPARQLParser.parse(query: sparql)
let results = try store.query(q, defaultGraph: graph)
for (i, result) in results.bindings.enumerated() {
print("\(i+1)\t\(result)")
}
```

There is also an API that exposes the RDF data in terms of graph vertices and edge traversals:

```swift
import Foundation
import SPARQLSyntax
import Kineo

let graph = Term(iri: "http://example.org/default-graph")
let store = MemoryQuadStore()

let url = URL(string: "http://kasei.us/about/foaf.ttl")!
try store.load(url: url, defaultGraph: graph)

let graphView = store.graph(graph)
let greg = graphView.vertex(Term(iri: "http://kasei.us/about/#greg"))

let knows = Term(iri: "http://xmlns.com/foaf/0.1/knows")
let name = Term(iri: "http://xmlns.com/foaf/0.1/name")
for v in try greg.outgoing(knows) {
let names = try v.outgoing(name)
if let nameVertex = names.first {
let name = nameVertex.term
print("Greg know \(name)")
}
}
```

### SPARQL Endpoint

Finally, using the companion [kineo-endpoint](https://github.com/kasei/kineo-endpoint) package,
a SPARQL endpoint can be run allowing SPARQL Protocol clients to access the data:

```
% kineo-endpoint -q geo.db &
% curl -H "Accept: application/sparql-results+json" -H "Content-Type: application/sparql-query" --data 'PREFIX geo: SELECT ?s ?lat ?long WHERE { ?s geo:lat ?lat ; geo:long ?long } LIMIT 3' 'http://localhost:8080/sparql'
{
"head": {
"vars": [ "s", "lat", "long" ]
},
"results": {
"bindings": [
{
"s": { "type": "uri", "value": "http://dbpedia.org/resource/'s-Gravendeel" },
"lat": { "type": "literal", "value": "5.17833333333333E1", "datatype": "http://www.w3.org/2001/XMLSchema#float" },
"long": { "type": "literal", "value": "4.61666666666667E0", "datatype": "http://www.w3.org/2001/XMLSchema#float" }
},
{
"s": { "type": "uri", "value": "http://dbpedia.org/resource/'s-Hertogenbosch" },
"lat": { "type": "literal", "value": "5.17833333333333E1", "datatype": "http://www.w3.org/2001/XMLSchema#float" },
"s": { "type": "uri", "value": "http://dbpedia.org/resource/Groesbeek" },
"long": { "type": "literal", "value": "5.93333333333333E0", "datatype": "http://www.w3.org/2001/XMLSchema#float" }
},
{
"s": { "type": "uri", "value": "http://dbpedia.org/resource/'s-Hertogenbosch" },
"lat": { "type": "literal", "value": "5.1729918E1", "datatype": "http://www.w3.org/2001/XMLSchema#float" },
"long": { "type": "literal", "value": "5.306938E0", "datatype": "http://www.w3.org/2001/XMLSchema#float" }
}
]
}
}
```