问题描述
我正在尝试创建一个Sparql-Query以获取与讲座有关的所有消息.我是整个语义网"故事的新手,所以如果使用命名空间完全错误,请耐心等待.
I'm trying to create a Sparql-Query to get all messages, that are related to a lecture. I'm new at the whole 'semantic web'-story so be patient if the usage namespace is completely wrong.
我尝试执行的查询是:
PREFIX siocNS: <http://rdfs.org/sioc/ns#>
PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX purlPred: <http://purl.org/dc/terms/>
PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>
select ?post ?title ?content ?time ?creator where
{
?post rdfPred:type siocNS:Post.
?post purlPred:title ?title.
?post purlPred:content ?content.
?post purlPred:created ?time.
?post purlPred:creator ?creator.
?post purlPred:context purlPredLecture:1337-7331.
}
其中1337-7331
是ID.
在Sesame-Workbench上,查询工作正常(因为我得到了2个预期的行).在Java上,使用耶拿(Jena),看来我的QueryExecuion
不正确.
On Sesame-Workbench the query seems working fine (because I get my 2 expected rows). On Java ,using Jena, it seems that my QueryExecuion
isn't correct.
这是我的代码:
String prefix = "PREFIX siocNS: <http://rdfs.org/sioc/ns#>\n"
+ "PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX purlPred: <http://purl.org/dc/terms/>\n"
+ "PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>\n";
String queryString = "select ?post ?title ?content ?time ?creator where\n"
+ "{\n"
+ "?post rdfPred:type siocNS:Post.\n"
+ "?post purlPred:title ?title. \n"
+ "?post purlPred:content ?content. \n"
+ "?post purlPred:created ?time. \n"
+ "?post purlPred:creator ?creator.\n"
+ "?post purlPred:context purlPredLecture:"
+ lectureID + ".\n" + "}";
logger.info("Created Query:" + prefix + queryString);
Query query = QueryFactory.create(prefix
+ queryString, Syntax.syntaxSPARQL);
query.addResultVar("?post");
query.addResultVar("?title");
query.addResultVar("?content");
query.addResultVar("?time");
query.addResultVar("?creator");
Model model = ModelFactory.createDefaultModel();
QueryExecution execution = QueryExecutionFactory
.create(query, model);
ResultSet execSelect = execution.execSelect();
while (execSelect.hasNext()) {
...
DO SOME STUFF
...
}
上面显示的查询是从java-debugger复制的.我在准备Query(Execution)
时错过了什么吗?Sesame-Workbench是否完成了丢失的事情?
The query shown above was copied from the java-debugger. Did I missed something on preparation of the Query(Execution)
and does Sesame-Workbench complete missing things?
谢谢您的帮助!
dpa
推荐答案
所以我针对自己的问题的解决方案;)
So my solution for my own problem ;)
String prefix = "PREFIX siocNS: <http://rdfs.org/sioc/ns#>\n"
+ "PREFIX rdfPred: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX purlPred: <http://purl.org/dc/terms/>\n"
+ "PREFIX purlPredLecture: <http://purl.org/<CENSORED>/lecture/>\n";
String queryString = "select ?post ?title ?content ?time ?creator where\n"
+ "{\n"
+ "?post rdfPred:type siocNS:Post.\n"
+ "?post purlPred:title ?title. \n"
+ "?post purlPred:content ?content. \n"
+ "?post purlPred:created ?time. \n"
+ "?post purlPred:creator ?creator.\n"
+ "?post purlPred:context purlPredLecture:"
+ lectureID + ".\n" + "}";
logger.info("Created Query:" + prefix + queryString);
String completeQuery = prefix + queryString;
logger.info("Created Query:\n" + completeQuery);
RepositoryConnection con = createRepositoryConnection();
if (con == null) {
logger.warn("Error creating repository connection. Returning epty list");
return new ArrayList<UserMessage>();
}
ArrayList<UserMessage> messages = new ArrayList<UserMessage>();
try {
try {
TupleQuery prepareTupleQuery = con
.prepareTupleQuery(
QueryLanguage.SPARQL,
completeQuery);
TupleQueryResult result = prepareTupleQuery
.evaluate();
while (result.hasNext()) {
...
}
for (UserMessage message : messages) {
collectReplysOfMessage(lectureID,
message.getId());
}
} catch (MalformedQueryException e) {
logger.error("Malformed Exception caught",
e);
} catch (QueryEvaluationException e) {
logger.error(
"Error on evaluating the query", e);
} finally {
con.close();
}
} catch (RepositoryException e) {
logger.error(
"Error on creationg repository connection",
e);
}
"createRepositoryConnection()"看起来像
Where 'createRepositoryConnection()' looks like
private static RepositoryConnection createRepositoryConnection() {
if (repoistory == null) {
prepareRepository();
}
RepositoryConnection con = null;
try {
con = repoistory.getConnection();
} catch (RepositoryException e) {
logger.error(
"Error on creating repository connection",
e);
}
return con;
}
这篇关于Sparql Select查询在Sesame-Workbench上有效,但在Java上无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!