Valentina
Valentina

Reputation: 47

RDF4j and GraphDB repository connection

I've a problem with rdf4j: i want to delete from my GraphDB repository "Feed" all the triples with feed:hashCode as predicate.

The first query verifies if there is a triple with the url parameter as subject, feed:hashCode as predicate, and hash parameter has object, and it works. If this statement doesn't exist in my repository, the second query begins, it should delete all the triples with feed:hashCode as predicate and url as subject, but it doesn't work, what is the problem?

Here is the code:

public static boolean updateFeedQuery(String url, String hash) throws RDFParseException, RepositoryException, IOException{
    Boolean result=false;
    Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed");
    repository.initialize();

    try {
        try (RepositoryConnection conn = repository.getConnection()) {
            BooleanQuery feedUrlQuery = conn.prepareBooleanQuery(
                    // @formatter:off
                    "PREFIX : <http://purl.org/rss/1.0/>\n" +
                    "PREFIX feed: <http://feed.org/>\n" +
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                    "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
                    "ASK{\n" +
                    "<"+url+"> feed:hashCode \""+hash+"\";\n"+
                    "rdf:type :channel.\n" +
                    "}"
                    // @formatter:on
            );

            result = feedUrlQuery.evaluate();

            //the feed is new or updated
            if(result == false) {

                Update removeOldHash = conn.prepareUpdate(
                        // @formatter:off
                        "PREFIX feed: <http://feed.org/>\n" +
                        "DELETE WHERE{\n"+
                        "<"+url+"> feed:hashCode ?s.\n" +
                        "}"
                        // @formatter:on
                        );
                removeOldHash.execute();
            }

        }
    }
    finally {
                 repository.shutDown();
                 return result;
    }

}

The error code is: "Missing parameter: query", and the server response is : "400 Bad Request"

Upvotes: 1

Views: 1250

Answers (1)

Jeen Broekstra
Jeen Broekstra

Reputation: 22052

The problem is in this line:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed");

You are using SPARQLRepository to access your RDF4J/GraphDB triplestore, and you're providing it only with a SPARQL query endpoint. According to the documentation, that means it will use that endpoint for both queries and updates. However, RDF4J Server (and therefore GraphDB) has a separate endpoint for SPARQL updates (see the REST API documentation). Your update fails because SPARQLRepository tries to send it to the query endpoint, instead of the update endpoint.

One way to fix is to explicitly set an update endpoint as well:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed", "http://localhost:7200/repositories/Feed/statements");

However, SPARQLRepository is really intended as a proxy class for accessing a (non-RDF4J) SPARQL endpoint (e.g. DBPedia, or some endpoint outside your own control or running a different triplestore implementation). Since GraphDB is fully RDF4J-compatible, you should really be using the HTTPRepository to access it. HTTPRepository implements the full RDF4J REST API, which extends the basic SPARQL protocol, which will allow your client-server communication to be much more efficient. See the Programming with RDF4J chapter on the Repository API for more details on how effectively accesss a remote RDF4J/GraphDB store.

Upvotes: 5

Related Questions