Most efficient way to clear a named graph?

只谈情不闲聊 提交于 2021-01-03 05:45:40

问题


I am working with an instance of Ontotext GraphDB and often want to clear a named graph with a large number of triples.

Currently, my technique involves issuing a SPARQL command to the graph server, which searches and matches a triple pattern of every triple in the named graph:

DELETE { GRAPH example:exampleGraph { ?s ?p ?o }} WHERE {?s ?p ?o .}

When there are a lot of triples, this approach often takes quite some time to clear the named graph.

I am wondering whether there is a more efficient way to do this. Even a triplestore-specific solution would be acceptable to me.

I should also note that I am using the RDF4J library to communicate with the graph. I understand that certain solutions may work on the Ontotext web interface, but I am only interested in a solution which I can implement programatically.


回答1:


You can use the SPARQL CLEAR command for this:

CLEAR GRAPH example:exampleGraph

Or alternatively, DROP:

DROP GRAPH example:exampleGraph

The difference between the two is that CLEAR allows triplestores to keep an empty named graph, while the DROP completely removes the named graph. But in the case of GraphDB there's no practical difference as GraphDB never keeps a reference to an empty named graph.

If you don't want to use SPARQL, you can use the RDF4J API to programmatically call the clear() operation:

IRI graph = graphdb.getValueFactory().createIRI("http://example.org/exampleGraph");  
try(RepositoryConnection conn = graphdb.getConnection()) {
   conn.clear(graph);
}

or more succinctly:

IRI graph = graphdb.getValueFactory().createIRI("http://example.org/exampleGraph");  
Repositories.consume(graphdb, conn -> conn.clear(graph));


来源:https://stackoverflow.com/questions/50280986/most-efficient-way-to-clear-a-named-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!