Giter VIP home page Giter VIP logo

ksparql's Introduction

ksparql

ksparql is a non-blocking sparql xml http client

motivation

i have been writing code which uses rdf4j for talking to a stardog database. while rdf4j is a great library with tons of features it does not integrate well with the ktor/coroutines world. this library aims at bridging that gap by using the aalto-xml async xml parser fed by a ktor ByteReadChannel for processing the sparql result xml.

limitations

this library has been tested with stardog (7.4.4) and Fuseki (3.8.0). theoretically, it should handle all databases with query endpoints that return sparql xml (https://www.w3.org/TR/rdf-sparql-XMLres/). be aware, that not the full XML tag set is supported yet.

usage

setup

get the package with gradle / maven from jCenter

implementation("com.bitkid:ksparql:0.0.2")
<dependency>
    <groupId>com.bitkid</groupId>
    <artifactId>ksparql</artifactId>
    <version>0.0.2</version>
    <type>pom</type>
</dependency>

and create the client

val client = KSparqlClient(ClientConfig(
    databaseHost = "http://localhost",
    databasePort = 5820,
    databaseName = "test",
    user = "admin",
    password = "admin"
))

add data

val model = ModelBuilder().subject("http://someEntity")
    .add(iri("http://prop1"), "bla")
    .add(iri("http://prop2"), 5)
    .build()

val anotherModel = ModelBuilder().subject("http://otherEntity")
    .add(iri("http://prop3"), "bla")
    .add(iri("http://prop4"), 5)
    .build()

runBlocking {
    // atomic add of all statements in the model
    client.add(model)
    
    // using a transaction explicitly
    val transaction = client.begin()
    transaction.add(model)
    transaction.add(anotherModel)
    transaction.commit()
    
    // or the closure
    client.transaction {
        add(model)
        add(anotherModel)
    }
}

query data

assuming you have following triples in your database

<http://bob> <http://likes> <http://alice>
<http://alice> <http://likes> <http://trudy>
<http://trudy> <http://likes> <http://bob>

this is how the client can be used to execute queries

runBlocking {
    client.query("SELECT ?a ?b ?c WHERE { ?a ?b ?c }") { valueFactory ->
        addBinding("a", valueFactory.createIRI("http://bob"))
    }.collect { rdfResult ->
        // do something with the rdfResult
    }

    // returns true
    client.ask("ASK {?a ?b ?c}") { valueFactory ->
        addBinding("a", valueFactory.createIRI("http://bob"))
        addBinding("b", valueFactory.createIRI("http://likes"))
        addBinding("c", valueFactory.createIRI("http://alice"))
    }
}

clear data

runBlocking {
    // delete everything
    client.clear()
    
    // delete named graph
    client.clear(iri("http://my-named-graph"))
}

contribute

i totally accept PRs if i like them. run the tests with

./gradlew test

the stardog tests are set to @Disabled, if you want to run them in your IDE install docker, run

./start_stardog.sh

and create a database called test (i use stardog studio for that)

license

I use modified rdf4j code a lot and it is published under this https://github.com/eclipse/rdf4j/blob/master/LICENSE license.

This library is published with the MIT license though.

MIT

ksparql's People

Contributors

bitkid avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.