[英]Java Jena SPARQL Query not retrieving results
I am trying to use Jena in Java to run a SPARQL query and retrieve the results from a .ttl
file. 我正在尝试在Java中使用Jena来运行SPARQL查询并从
.ttl
文件中检索结果。 For some reason, the desired results are not being retrieved. 由于某些原因,没有检索到所需的结果。 I have looked at my SPARQL query and I am not sure where the error is, but no results are being delivered to my
ResultSet
object. 我已经查看了我的SPARQL查询,但不确定错误在哪里,但是没有结果传递给
ResultSet
对象。 Here is my code below: 这是我的代码如下:
BasicConfigurator.configure();
FileManager.get().addLocatorClassLoader(QuerySparql.class.getClassLoader());
Model model = FileManager.get().loadModel(My_Path//sample.ttl", "TTL");
String queryString =
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/name>\n" +
"PREFIX dbo: <http://dbpedia.org/ontology/>\n" +
"PREFIX db: <http://dbpedia.org/resource/>\n " +
"SELECT ?team WHERE { \n" +
"?simeon foaf:name \"Simeon Ikudabo\"@en .\n" +
"?simeon dbo:team ?team\n" +
"}";
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal team = soln.getLiteral("team");
System.out.println(team);
}
}
catch(Exception e) {
e.printStackTrace(System.out);
}
System.out.println("Done");
And here is the output in the console, but it doesn't actually grab any of the results that I am looking for and skips the while loop altogether because there are no results in the ResultSet
: 这是控制台中的输出,但实际上并没有捕获我要查找的任何结果,并且完全跳过了while循环,因为
ResultSet
中没有结果:
3 [main] DEBUG Jena - Jena initialization
375 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorFile
377 [main] DEBUG org.apache.jena.util.FileManager - Add location: ClassLoaderLocator
384 [main] DEBUG org.apache.jena.util.LocationMapper - Failed to find configuration: file:location-mapping.rdf;file:location-mapping.n3;file:location-mapping.ttl;file:etc/location-mapping.rdf;file:etc/location-mapping.n3;file:etc/location-mapping.ttl
384 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorFile
395 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorURL
395 [main] DEBUG org.apache.jena.util.FileManager - Add location: ClassLoaderLocator
423 [main] DEBUG org.apache.jena.riot.system.stream.JenaIOEnvironment - Failed to find configuration: location-mapping.ttl;location-mapping.rdf;location-mapping.n3;etc/location-mapping.rdf;etc/location-mapping.n3;etc/location-mapping.ttl
1731 [main] DEBUG org.apache.jena.info - System architecture: 64 bit
1862 [main] DEBUG org.apache.jena.info - System architecture: 64 bit
1896 [main] DEBUG org.apache.jena.util.FileManager - loadModel(My_Path//sample.ttl, TTL)
1897 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - Not mapped: My_Path//sample.ttl
1951 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - Found: My_Path//sample.ttl (LocatorFile)
2633 [main] DEBUG org.apache.jena.shared.LockMRSW - Lock : main
2638 [main] DEBUG org.apache.jena.shared.LockMRSW - Lock : main
Done
As you can see, I create the variable ?team
, which is a dbpedia.org/ontology/team
in my local dbpedia turtle file. 如您所见,我在本地dbpedia turtle文件中创建了一个变量
?team
,它是dbpedia.org/ontology/team
。 ?simeon
in this example is the athlete that is being looked up, and Simeon will reflect the foaf:name "Simeon Ikudabo"@en
. 在此示例中,
?simeon
是正在被抬头的运动员,Simeon将反映以下foaf:name "Simeon Ikudabo"@en
。 My last triple statement is ?simeon dbo:team ?team
. 我的最后一个三元声明是
?simeon dbo:team ?team
。 This should find dbpedia ontology team value that corresponds with Simeon Ikudabo. 这应该找到与Simeon Ikudabo对应的dbpedia本体团队值。 For some reason however, the
ResultSet
does not retrieve this value. 但是由于某种原因,
ResultSet
不会检索该值。 I have looked online at what could be wrong with my query, but I am not sure what the issue could be from my research. 我在网上查看了查询可能出了什么问题,但是我不确定我的研究可能出了什么问题。 Here is my
.ttl
file with the data, and it APPEARS to be formatted properly: 这是我的
.ttl
文件,其中包含数据,并且它似乎已正确格式化:
<http://dbpedia.org/resource/Simeon_Ikudabo> <http://dbpedia.org/ontology/team>
<http://dbpedia.org/resource/Indiana_Hoosiers> .
<http://dbpedia.org/resource/Simeon_Ikudabo> <http://www.xmlns.com/foaf/0.1/name> "Simeon Ikudabo"@en .
<http://dbpedia.org/resource/Nick_Mangeri> <http://dbpedia.org/ontology/team> <http://dbpedia.org/resource/Akron_Zips> .
<http://dbepdia.org/resource/Nick_Mangeri> <http://www.xmlns.com/foaf/0.1/name> "Nick Mangeri"@en .
I also accounted for the possibility of an IO issue, but when I simply read the model in Java it seems to be getting the correct IO from the file. 我还考虑了IO问题的可能性,但是当我只是用Java读取模型时,似乎正在从文件中获取正确的IO。 Here is my code for simply reading the model:
这是我的代码,用于简单阅读模型:
Model model = ModelFactory.createDefaultModel();
model.read("My_Path//sample.ttl", "TURTLE");
model.write(System.out);
And here is the output: 这是输出:
0 [main] DEBUG Jena - Jena initialization
417 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorFile
419 [main] DEBUG org.apache.jena.util.FileManager - Add location: ClassLoaderLocator
428 [main] DEBUG org.apache.jena.util.LocationMapper - Failed to find configuration: file:location-mapping.rdf;file:location-mapping.n3;file:location-mapping.ttl;file:etc/location-mapping.rdf;file:etc/location-mapping.n3;file:etc/location-mapping.ttl
428 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorFile
431 [main] DEBUG org.apache.jena.util.FileManager - Add location: LocatorURL
431 [main] DEBUG org.apache.jena.util.FileManager - Add location: ClassLoaderLocator
457 [main] DEBUG org.apache.jena.riot.system.stream.JenaIOEnvironment - Failed to find configuration: location-mapping.ttl;location-mapping.rdf;location-mapping.n3;etc/location-mapping.rdf;etc/location-mapping.n3;etc/location-mapping.ttl
1793 [main] DEBUG org.apache.jena.info - System architecture: 64 bit
1912 [main] DEBUG org.apache.jena.info - System architecture: 64 bit
2254 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - Not mapped: My_Path//sample.ttl
2255 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - open(My_Path//sample.ttl)
2255 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - Not mapped: My_Path//sample.ttl
2288 [main] DEBUG org.apache.jena.riot.system.stream.StreamManager - Found: My_Path//sample.ttl (LocatorFile)
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://dbpedia.org/ontology/"
xmlns:j.1="http://dbpedia.org/ontology/"
xmlns:j.2="http://www.xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://dbpedia.org/resource/Nick_Mangeri">
<j.1:team rdf:resource="http://dbpedia.org/resource/Akron_Zips"/>
</rdf:Description>
<rdf:Description rdf:about="http://dbepdia.org/resource/Nick_Mangeri">
<j.2:name xml:lang="en">Nick Mangeri</j.2:name>
</rdf:Description>
<rdf:Description rdf:about="http://dbpedia.org/resource/Simeon_Ikudabo">
<j.2:name xml:lang="en">Simeon Ikudabo</j.2:name>
<j.0:team rdf:resource="http://dbpedia.org/resource/Indiana_Hoosiers"/>
</rdf:Description>
</rdf:RDF>
So I know that Jena can read the file. 因此,我知道Jena可以读取文件。 I created a SIMILAR query using the Virtuoso SPARQL endpoint for DBpedia and consistently get the proper results.
我使用Virtuoso SPARQL端点为DBpedia创建了一个SIMILAR查询,并始终获得正确的结果。 He is a similar query that I ran as a test from the Virtuoso SPARQL endpoint for DBpedia:
他是我从Virtuoso SPARQL端点针对DBpedia的测试中运行的类似查询:
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix db: <http://dbpedia.org/resource/>
SELECT ?draftTeam WHERE{
?kobe dbpedia-owl:draftTeam ?draftTeam .
?kobe foaf:name "Kobe Bryant"@en .
}
I know that the Virtuoso SPARQL endpoint is slightly different from the Apache Jena SPARQL query, but it is the same general syntax. 我知道Virtuoso SPARQL端点与Apache Jena SPARQL查询略有不同,但是它是相同的常规语法。 I am stumped as to why I cannot execute a query in Jena.
我为无法在耶拿执行查询而感到困惑。
Your data has the triple 您的数据是三重
<http://dbpedia.org/resource/Simeon_Ikudabo> <http://dbepdia.org/ontology/team> <http://dbpedia.org/resource/Indiana_Hoosiers> .
And there's a typo: dbepdia
instead of dbpedia
in the property. 还有一个错字:该属性中是
dbepdia
而不是dbpedia
。 I found this when I noticed in the printed model that there were two namespaces: 我在打印的模型中注意到有两个名称空间时发现了这一点:
xmlns:j.0="http://dbepdia.org/ontology/"
xmlns:j.1="http://dbpedia.org/ontology/"
I was able to resolve the issue. 我能够解决问题。 Here is a sample of the code below:
这是下面的代码示例:
BasicConfigurator.configure();
FileManager.get().addLocatorClassLoader(QuerySparql.class.getClassLoader());
Model model = FileManager.get().loadModel("My_Path//sample.ttl", "TTL");
String queryString =
"PREFIX db: <http://dbpedia.org/resource> \n" +
"SELECT ?meshId WHERE{ \n" +
"?autism <http://xmlns.com/foaf/0.1/name> \"Autism\"@en .\n" +
"?autism <http://dbpedia.org/ontology/meshId> ?meshId \n" +
"}";
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
if(results.hasNext()) {
System.out.println("has results!");
}
else {
System.out.println("No Results!");
}
while(results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal r = soln.getLiteral("meshId");
System.out.println(r);
}
The error appears to have been within the query. 该错误似乎已出现在查询中。 I needed to use the full URL that is in the dbpedia file for my query.
我需要使用dbpedia文件中的完整URL进行查询。 In dbpedia file the url looks like this:
在dbpedia文件中,URL如下所示:
<http://xmlns.com/foaf/0.1/name> "Autism"@en .
Using that URL within the actual query like this: 在实际查询中使用该URL,如下所示:
"?autism <http://xmlns.com/foaf/0.1/name> \"Autism\"@en .\n
ended up solving the issue. 最终解决了这个问题。 The prefix that I used earlier within the query did not directly match the prefix that was in the dbpedia file.
我之前在查询中使用的前缀与dbpedia文件中的前缀不直接匹配。 So the prefix:
因此前缀:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
will not work. 不管用。 Instead, I need to put the URL that directly matches the one found in the dbpedia file.
相反,我需要放置与dbpedia文件中找到的URL直接匹配的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.