Pantelis Natsiavas
Pantelis Natsiavas

Reputation: 5369

SPARUL query to drop most graphs, using Jena API

I am trying to clear most of the graphs contained in my local Virtuoso triple store, using Apache Jena, as part of my clean up process before and after my unit tests. I think that something like this should be done. First, I retrieve the graph URIs to be deleted; then I execute a SPARUL Drop operation.

String sparqlEndpointUsername = ...;
String sparqlEndpointPassword = ...;
String sparqlQueryString = ...; // Returns the URIs of the graphs to be deleted

HttpAuthenticator authenticator = new SimpleAuthenticator(sparqlEndpointUsername,
            sparqlEndpointPassword.toCharArray());
ResultSet resultSetToReturn = null;
try (QueryEngineHTTP queryEngine = new QueryEngineHTTP(sparqlEndpoint, sparqlQueryString, authenticator)) {
    resultSetToReturn = queryEngine.execSelect();
    resultSetToReturn = ResultSetFactory.copyResults(resultSetToReturn);

    while(resultSetToReturn.hasNext()){
        String graphURI = resultSetToReturn.next().getResource("?g").getURI();

        UpdateRequest request = UpdateFactory.create() ;
        request.add("DROP GRAPH <"+graphURI+">");

        Dataset dataset = ...; // how can I create a default dataset pointing to my local virtuoso installation?

        // And perform the operations.
        UpdateAction.execute(request, dataset) ;

    }
}
;

Questions:

  1. As shown in this example, ARQ needs a dataset to operate on. How would I create this dataset pointing to my local Virtuoso installation for an update operation?
  2. Is there perhaps an alternative to my approach? Would using another approach (apart from jena) be a better idea?

Please note that I am not trying to delete all graphs. I am deleting only the graphs whose names are returned through the SPARQL query defined in the beginning (3rd line).

Upvotes: 0

Views: 809

Answers (2)

AndyS
AndyS

Reputation: 16630

You can build a single SPARQL Update request:

DROP GRAPH <g1> ;
DROP GRAPH <g2> ;
DROP GRAPH <g3> ;
... ;

because in SPARQL Update one HTTP requests can be several update operations, separated by ;.

Upvotes: 1

TallTed
TallTed

Reputation: 9434

Your question appears to be specific to Virtuoso, and meant to remove all RDF data, so you could use Virtuoso's built-in RDF_GLOBAL_RESET() function.

This is not a SPARQL/SPARUL query; it is usually issued through an SQL connection -- which could be JDBC, ODBC, ADO.NET, OLE DB, iSQL, etc.

That said, as you are connecting through a SPARUL-privileged connection, you should be able to use Virtuoso's (limited) SQL-in-SPARQL support, a la --

SELECT
  ( bif:RDF_GLOBAL_RESET()  AS reset )
WHERE
  {  ?s  ?p  ?o  }
LIMIT 1

(Executing this through an unprivileged connection like the default SPARQL endpoint will result in an error like Virtuoso 37000 Error SP031: SPARQL compiler: Function bif:RDF_GLOBAL_RESET() can not be used in text of SPARQL query due to security restrictions.)

(ObDisclaimer: OpenLink Software produces Virtuoso, and employs me.)

Upvotes: 1

Related Questions