简单的 Jena SPARQL 查询不起作用
我在这里做错了什么?
public class SimpleSearchTest {
public static void main(String[] args) throws Exception {
Model model = ModelFactory.createDefaultModel();
model.getGraph().add(new Triple(Node.createURI("a"), Node.createURI("b"), Node.createURI("c")));
String queryString = "SELECT ?p ?o WHERE { <a> ?p ?o }";
Query query = QueryFactory.create(queryString);
QueryExecution qExec = QueryExecutionFactory.create(query, model);
ResultSetFormatter.out(qExec.execSelect());
}
}
我期待着
-------------
| p | o |
=============
| <b> | <c> |
-------------
,但我没有得到任何结果:
---------
| p | o |
=========
---------
我确信这是愚蠢的......
What am I doing wrong here?
public class SimpleSearchTest {
public static void main(String[] args) throws Exception {
Model model = ModelFactory.createDefaultModel();
model.getGraph().add(new Triple(Node.createURI("a"), Node.createURI("b"), Node.createURI("c")));
String queryString = "SELECT ?p ?o WHERE { <a> ?p ?o }";
Query query = QueryFactory.create(queryString);
QueryExecution qExec = QueryExecutionFactory.create(query, model);
ResultSetFormatter.out(qExec.execSelect());
}
}
I am expecting
-------------
| p | o |
=============
| <b> | <c> |
-------------
But instead I am getting no results:
---------
| p | o |
=========
---------
I am sure it is something dumb...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 SPARQL 解析器不喜欢您的
因为它不是合法的 URI(尽管奇怪的是您没有收到警告)。如果您按如下方式更改代码:
您将得到您期望的结果。
另外,通过使用 Node.createURI() 创建测试图,您将使用较低级别的内部图 API,而不是更常用的模型 API。这样做完全没问题,但 Graph API 通常会假设您更了解自己在做什么,并且可能对执行意外操作进行较少的检查。
I think the SPARQL parser isn't liking your
<a>
because it's not a legal URI (though it's odd that you don't get a warning). If you change your code as follows:you get the result you are expecting.
Parenthetically, by creating the test graph with
Node.createURI()
you are using the lower-level internal Graph API, rather than the more normally used Model API. It's perfectly fine to do this, but the Graph API generally assumes you know more what you are doing, and may have fewer checks against doing the unexpected.