简体   繁体   中英

RDF4J SPARQL query to JSON

I am trying to move data from a SPARQL endpoint to a JSONObject. Using RDF4J.

RDF4J documentation does not address this directly (some info about using endpoints, less about converting to JSON, and nothing where these two cases meet up).

Sofar I have:

        SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "SPARQL/JSON");
        repo.setAdditionalHttpHeaders(headers);
        try (RepositoryConnection conn = repo.getConnection())
        {
            String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
            GraphQuery query = conn.prepareGraphQuery(queryString);
            debug("Mark 2");
            try (GraphQueryResult result = query.evaluate())

this fails because "Server responded with an unsupported file format: application/sparql-results+json"

I figured a SPARQLGraphQuery should take the place of GraphQuery, but RepositoryConnection does not have a relevant prepare statement.

If I exchange

try (RepositoryConnection conn = repo.getConnection())

with

try (SPARQLConnection conn = (SPARQLConnection)repo.getConnection())

I run into the problem that SPARQLConnection does not generate a SPARQLGraphQuery. The closest I can get is:

SPARQLGraphQuery query = (SPARQLGraphQuery)conn.prepareQuery(QueryLanguage.SPARQL, queryString);

which gives a runtime error as these types cannot be cast to eachother.

I do not know how to proceed from here. Any help or advise much appreciated. Thank you

this fails because "Server responded with an unsupported file format: application/sparql-results+json"

In RDF4J, SPARQL SELECT queries are tuple queries , so named because each result is a set of bindings, which are tuples of the form (name, value). In contrast, CONSTRUCT (and DESCRIBE) queries are graph queries , so called because their result is a graph, that is, a collection of RDF statements.

Furthermore, setting additional headers for the response format as you have done here is not necessary (except in rare circumstances), the RDF4J client handles this for you automatically, based on the registered set of parsers.

So, in short, simplify your code as follows:

SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
try (RepositoryConnection conn = repo.getConnection()) {
        String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
        TupleQuery query = conn.prepareTupleQuery(queryString);
        debug("Mark 2");
        try (TupleQueryResult result = query.evaluate()) {
                ... 
        }
 }

If you want to write the result of the query in JSON format, you could use a TupleQueryResultHandler , for example the SPARQLResultsJSONWriter , as follows:

SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
try (RepositoryConnection conn = repo.getConnection()) {
        String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
        TupleQuery query = conn.prepareTupleQuery(queryString);
        query.evaluate(new SPARQLResultsJSONWriter(System.out));
 }

This will write the result of the query (in this example to standard output) using the SPARQL Query Results JSON format . If you have a non-standard format in mind, you could of course also create your own TupleQueryResultHandler implementation.

For more details on the various ways in which you can process the result (including iterating, streaming, adding to a List, or just directly sending to a result handler), see the documentation on querying a repository . As an aside, the javadoc on the RDF4J APIs is pretty extensive too, so if your Java editing environment has support for displaying that, I'd advise you to make use of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM