java - 是否可以检索 RDF4J 事务的更新语句?
问题描述
我正在尝试使用 RDF4J 支持 SPARQL 更新查询的“试运行”功能。我想通知用户将插入/删除的语句,最好是通过从当前事务中获取语句。我在想类似的事情:
conn.begin();
Update query = conn.prepareUpdate(queryString);
query.execute();
// Print statements in the transaction
System.out.println("Dry run completed.");
conn.rollback();
System.out.println("Dry run rolled back.");
有没有办法用 RDF4J 做到这一点?
解决方案
(从https://github.com/eclipse/rdf4j/discussions/3163复制)
您可以使用SailConnectionListener
. 不过,访问它的方式有点笨拙。这是一个例子:
Repository rep = new SailRepository(new MemoryStore());
try (SailRepositoryConnection conn = (SailRepositoryConnection) rep.getConnection()) {
NotifyingSailConnection sailConn = (NotifyingSailConnection) conn.getSailConnection();
sailConn.addConnectionListener(new SailConnectionListener() {
@Override
public void statementRemoved(Statement removed) {
System.out.println("removed: " + removed);
}
@Override
public void statementAdded(Statement added) {
System.out.println("added: " + added);
}
});
conn.begin();
conn.add(FOAF.PERSON, RDF.TYPE, RDFS.CLASS);
String update = "DELETE { ?p a rdfs:Class } INSERT { ?p rdfs:label \"Person\" } WHERE { ?p a rdfs:Class }";
conn.prepareUpdate(update).execute();
System.out.println("executed");
conn.rollback();
System.out.println("transaction aborted");
}
如您所见,我们需要将RepositoryConnection
转换为特定类型以检索底层SailConnection
,然后我们还需要将其SailConnection
转换为 aNotifyingSailConnection
以便能够SailConnectionListener
在其上注册 a 。此侦听器将接收各个语句的预提交添加和删除事件。运行上述代码将产生以下控制台输出:
added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class)
removed: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class) [null]
added: (http://xmlns.com/foaf/0.1/Person, http://www.w3.org/2000/01/rdf-schema#label, "Person")
executed
transaction aborted
推荐阅读
- c - rand()%28 只生成 7 的倍数
- r - 使用 RCircos 包绘制基因组数据
- c# - iOS 相当于 Android 吐司
- c# - 如果由包含实体订购,我如何在 Linq Select Distinct 中订购?
- android - 我认为我没有正确实施 ListenerRegistration
- pyomo - 使用持久求解器处理 ConstraintList
- java - Android:如何使用 Calendar 类检查一周是否过去
- html - 如何修复导航栏但仅在上传时
- javascript - JavaScript:Promise.All() 和 Async / Await 和 Map()
- r - R中的蒙特卡洛方法