Running rdf4j server programatically

32 views
Skip to first unread message

Ali Haidar

unread,
Mar 30, 2021, 4:32:53 AMMar 30
to RDF4J Users
Hello, Is there a way to launch an rdf4j server on top of a native store and expose this over http as an API ? I could not find any real documentation on this topic on the official website.
Thank you in advance!

Jerven Bolleman

unread,
Mar 30, 2021, 6:46:41 AMMar 30
to rdf4j...@googlegroups.com
Hi Ali,

We are working on making launching just a http sparql endpoint for a repository easier https://github.com/eclipse/rdf4j/pull/2905.

In the meantime you can use the workbench to expose any RDF4J repository as a http/sparql api.

Regards,
Jerven

On Tue, Mar 30, 2021 at 10:32 AM Ali Haidar <ali.hai...@gmail.com> wrote:
Hello, Is there a way to launch an rdf4j server on top of a native store and expose this over http as an API ? I could not find any real documentation on this topic on the official website.
Thank you in advance!

--
You received this message because you are subscribed to the Google Groups "RDF4J Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdf4j-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdf4j-users/7f96230e-e1f1-4679-8795-c3bed59cac98n%40googlegroups.com.


--
Jerven Bolleman
m...@jerven.eu

Ali Haidar

unread,
Mar 30, 2021, 10:59:39 AMMar 30
to rdf4j...@googlegroups.com
Hi Jerven,

Thanks for your reply. I will try to use the workbench.

Regards,
Ali.

You received this message because you are subscribed to a topic in the Google Groups "RDF4J Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rdf4j-users/vCIBp7VZ2Ys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rdf4j-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rdf4j-users/CAHM_hUM7_cxPzc--4EFVRSFUrV9qUoPMk6SnjH1MmLEmJ%2Bd1XA%40mail.gmail.com.

Alessandro Bollini

unread,
Mar 31, 2021, 12:55:29 AMMar 31
to rdf4j...@googlegroups.com


> On 30 Mar 2021, at 10:32, Ali Haidar <ali.hai...@gmail.com> wrote:
>
> Hello, Is there a way to launch an rdf4j server on top of a native store and expose this over http as an API ? I could not find any real documentation on this topic on the official website.

You may also want to have a look at https://github.com/metreeca/link (full disclosure: I’m the lead for the project).

The following would launch a CORS-enabled server with read-only SPARQL 1.1 Query and Graphs endpoints, backed by a native rdf4j store.

If required, authenticated write operations and REST/JSON-LD linked data APIs could be easily added; feel free to ask for assistance.

HTH,
Alessandro

-----------------------------------------------------------------------------------------------------------------------------------------
alessandro bollini ph.d. - tech lead - linkedin.com/in/alessandrobollini



import com.metreeca.jse.JSEServer;
import com.metreeca.rdf4j.assets.Graph;

import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.sail.nativerdf.NativeStore;

import java.io.*;

import static com.metreeca.rdf4j.assets.Graph.graph;
import static com.metreeca.rdf4j.handlers.Graphs.graphs;
import static com.metreeca.rdf4j.handlers.SPARQL.sparql;
import static com.metreeca.rest.Context.asset;
import static com.metreeca.rest.Context.resource;
import static com.metreeca.rest.handlers.Router.router;
import static com.metreeca.rest.wrappers.CORS.cors;
import static com.metreeca.rest.wrappers.Gateway.gateway;
import static com.metreeca.toys.Toys.Base;

public final class RDF4JServer {

public static void main(final String... args) {
new JSEServer()

.delegate(context -> context

.set(graph(), () -> new Graph(new SailRepository(new NativeStore(new File("./data")))))

.exec(() -> asset(graph()).exec(connection -> {
try {

// load repository content here using connection

} catch ( final IOException e ) {
throw new UncheckedIOException(e);
}
}))

.get(() -> gateway()

.with(cors())

.wrap(router()

.path("/sparql", sparql().query())
.path("/graphs", graphs())

)

)
)

.start();
}

}

———

<dependencyManagement>

<dependencies>

<dependency>
<groupId>com.metreeca</groupId>
<artifactId>metreeca-link</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>
<groupId>com.metreeca</groupId>
<artifactId>metreeca-jse</artifactId>
</dependency>

<dependency>
<groupId>com.metreeca</groupId>
<artifactId>metreeca-rdf4j</artifactId>
</dependency>


<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-repository-sail</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-sail-nativerdf</artifactId>
</dependency>

</dependencies>


Alessandro Bollini

unread,
Mar 31, 2021, 3:21:24 AMMar 31
to rdf4j...@googlegroups.com


> On 31 Mar 2021, at 06:55, Alessandro Bollini <alessandr...@metreeca.com> wrote:
>
> The following would launch a CORS-enabled server with read-only SPARQL 1.1 Query and Graphs endpoints, backed by a native rdf4j store.

Fixed minor typos…

-a
.path("/graphs", graphs().query()

)

)
)

.start();
}

}

———

<dependencyManagement>

<dependencies>

<dependency>
<groupId>com.metreeca</groupId>
<artifactId>metreeca-link</artifactId>
<version>0.55.0</version>

Ali Haidar

unread,
Mar 31, 2021, 7:37:15 AMMar 31
to rdf4j...@googlegroups.com
Oh that's great! I tried to run this over my repository however I wasn't able to run update queries. Is it read-only or is there a kind of configuration that has to be given when launching the server? If there is a documentation on this topic using the library you provided it would be nice if you could share this here.

Here's the request I tried:
```

curl -i localhost:8080/sparql --data-urlencode 'update=insert data { <http://www.example.org/X> <http://www.example.org/Y> <http://www.example.org/Q> .}'

HTTP/1.1 401 Unauthorized

Access-control-allow-headers: Origin, Accept, Content-Type, Authorization, X-Requested-With, Access-Control-Allow-Header, Access-Control-Request-Method, Access-Control-Request-Header

Date: Wed, 31 Mar 2021 11:22:22 GMT

Access-control-allow-methods: OPTIONS, GET, HEAD, POST, PUT, DELETE

Transfer-encoding: chunked

Access-control-allow-origin: *

Access-control-allow-credentials: true

```

--
You received this message because you are subscribed to a topic in the Google Groups "RDF4J Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rdf4j-users/vCIBp7VZ2Ys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rdf4j-users...@googlegroups.com.

Alessandro Bollini

unread,
Mar 31, 2021, 10:30:18 AMMar 31
to rdf4j...@googlegroups.com


> On 31 Mar 2021, at 13:37, Ali Haidar <ali.hai...@gmail.com> wrote:
>
> Oh that's great! I tried to run this over my repository however I wasn't able to run update queries. Is it read-only or is there a kind of configuration that has to be given when launching the server?

Endpoints must be explicitly enabled both for read and write operations: the following sample (complete project attached for your convenience) is configured with:

- public access for read operation

- bearer token authorisation for write operations


The write API key is read at server startup from the `UpdateKey` system property (launch as java -DUpdateKey=<your-key>) or generated randomly if not supplied.

After launching the server with the following log:

! Sample update key <474a691a-3410-4be4-9f49-182e886845ce> <— copy key —<
! JSEServer server starting
! JSEServer server listening at <http://localhost:8080/>

you’ll be able to update it including the key in the `Authorization` header, e.g.:

curl --include localhost:8080/sparql \
--header "Authorization: Bearer 474a691a-3410-4be4-9f49-182e886845ce” \
--data-urlencode 'update=insert data { <http://www.example.org/X> <http://www.example.org/Y> <http://www.example.org/Q> .}’

More complex token verification and role assignment logics may be implemented using a custom authoriser (https://javadoc.io/static/com.metreeca/metreeca-rest/0.55.0/com/metreeca/rest/wrappers/Bearer.html#bearer-java.util.function.BiFunction-)


> If there is a documentation on this topic using the library you provided it would be nice if you could share this here.

The framework is fully documented @ https://metreeca.github.io/link/ : see the linked tutorials for a hands-on introduction.
metreeca-link-sample.zip

Alessandro Bollini

unread,
Mar 31, 2021, 11:50:03 AMMar 31
to rdf4j...@googlegroups.com


> On 31 Mar 2021, at 16:30, Alessandro Bollini <alessandr...@metreeca.com> wrote:
>
>> On 31 Mar 2021, at 13:37, Ali Haidar <ali.hai...@gmail.com> wrote:
>>
>> Oh that's great! I tried to run this over my repository however I wasn't able to run update queries. Is it read-only or is there a kind of configuration that has to be given when launching the server?
>
> Endpoints must be explicitly enabled both for read and write operations: the following sample (complete project attached for your convenience) is configured with:

I’d would advise against that, but you could completely disable authentication with:

new JSEServer()

.delegate(context -> context

.set(graph(), () -> new Graph(new SailRepository(new NativeStore(StoragePath))))

.get(() -> gateway()

.with(cors())

.wrap(router()

.path("/sparql", sparql()
.query()
.update()
)

.path("/graphs", graphs()
.query()
.update()
)

)
)
)

.start();

Ali Haidar

unread,
Apr 1, 2021, 4:39:25 AMApr 1
to rdf4j...@googlegroups.com
Ok thanks for your replies I will try to use this!

Regards,
Ali.

--
You received this message because you are subscribed to a topic in the Google Groups "RDF4J Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rdf4j-users/vCIBp7VZ2Ys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rdf4j-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages