Java Jena SPARQL 查询未检索到结果
Java Jena SPARQL Query not retrieving results
我正在尝试在 Java 中使用 Jena 来 运行 SPARQL 查询并从 .ttl
文件中检索结果。由于某种原因,未检索到所需的结果。我查看了我的 SPARQL 查询,我不确定错误在哪里,但没有结果被传送到我的 ResultSet
对象。下面是我的代码:
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");
这是控制台中的输出,但它实际上并没有获取我正在寻找的任何结果,而是完全跳过了 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
如您所见,我在本地 dbpedia turtle 文件中创建了变量 ?team
,它是一个 dbpedia.org/ontology/team
。本例中的 ?simeon
是正在查找的运动员,而 Simeon 将反映 foaf:name "Simeon Ikudabo"@en
。我的最后一个三重声明是 ?simeon dbo:team ?team
。这应该找到与 Simeon Ikudabo 相对应的 dbpedia ontology 团队值。然而,出于某种原因,ResultSet
不会检索此值。我在网上查看了我的查询可能有什么问题,但我不确定我的研究可能是什么问题。这是我的包含数据的 .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 .
我也考虑到了 IO 问题的可能性,但是当我只是阅读 Java 中的模型时,它似乎从文件中获取了正确的 IO。这是我简单阅读模型的代码:
Model model = ModelFactory.createDefaultModel();
model.read("My_Path//sample.ttl", "TURTLE");
model.write(System.out);
这是输出:
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>
所以我知道 Jena 可以读取该文件。我使用 Virtuoso SPARQL 端点为 DBpedia 创建了一个 SIMILAR 查询,并始终获得正确的结果。他是一个类似的查询,我 运行 作为来自 DBpedia 的 Virtuoso SPARQL 端点的测试:
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 .
}
我知道 Virtuoso SPARQL 端点与 Apache Jena SPARQL 查询略有不同,但通用语法相同。我很困惑为什么我不能在耶拿执行查询。
你的数据有三倍
<http://dbpedia.org/resource/Simeon_Ikudabo> <http://dbepdia.org/ontology/team> <http://dbpedia.org/resource/Indiana_Hoosiers> .
还有一个错字:dbepdia
而不是 属性 中的 dbpedia
。当我在打印模型中注意到有两个名称空间时,我发现了这一点:
xmlns:j.0="http://dbepdia.org/ontology/"
xmlns:j.1="http://dbpedia.org/ontology/"
我能够解决问题。这是下面的代码示例:
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);
}
错误似乎在查询中。我需要使用 dbpedia 文件中的完整 URL 来进行查询。在 dbpedia 文件中,url 看起来像这样:
<http://xmlns.com/foaf/0.1/name> "Autism"@en .
在实际查询中使用 URL,如下所示:
"?autism <http://xmlns.com/foaf/0.1/name> \"Autism\"@en .\n
最终解决了问题。我之前在查询中使用的前缀与 dbpedia 文件中的前缀不直接匹配。所以前缀:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
将不起作用。相反,我需要放置与 dbpedia 文件中找到的直接匹配的 URL。
我正在尝试在 Java 中使用 Jena 来 运行 SPARQL 查询并从 .ttl
文件中检索结果。由于某种原因,未检索到所需的结果。我查看了我的 SPARQL 查询,我不确定错误在哪里,但没有结果被传送到我的 ResultSet
对象。下面是我的代码:
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");
这是控制台中的输出,但它实际上并没有获取我正在寻找的任何结果,而是完全跳过了 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
如您所见,我在本地 dbpedia turtle 文件中创建了变量 ?team
,它是一个 dbpedia.org/ontology/team
。本例中的 ?simeon
是正在查找的运动员,而 Simeon 将反映 foaf:name "Simeon Ikudabo"@en
。我的最后一个三重声明是 ?simeon dbo:team ?team
。这应该找到与 Simeon Ikudabo 相对应的 dbpedia ontology 团队值。然而,出于某种原因,ResultSet
不会检索此值。我在网上查看了我的查询可能有什么问题,但我不确定我的研究可能是什么问题。这是我的包含数据的 .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 .
我也考虑到了 IO 问题的可能性,但是当我只是阅读 Java 中的模型时,它似乎从文件中获取了正确的 IO。这是我简单阅读模型的代码:
Model model = ModelFactory.createDefaultModel();
model.read("My_Path//sample.ttl", "TURTLE");
model.write(System.out);
这是输出:
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>
所以我知道 Jena 可以读取该文件。我使用 Virtuoso SPARQL 端点为 DBpedia 创建了一个 SIMILAR 查询,并始终获得正确的结果。他是一个类似的查询,我 运行 作为来自 DBpedia 的 Virtuoso SPARQL 端点的测试:
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 .
}
我知道 Virtuoso SPARQL 端点与 Apache Jena SPARQL 查询略有不同,但通用语法相同。我很困惑为什么我不能在耶拿执行查询。
你的数据有三倍
<http://dbpedia.org/resource/Simeon_Ikudabo> <http://dbepdia.org/ontology/team> <http://dbpedia.org/resource/Indiana_Hoosiers> .
还有一个错字:dbepdia
而不是 属性 中的 dbpedia
。当我在打印模型中注意到有两个名称空间时,我发现了这一点:
xmlns:j.0="http://dbepdia.org/ontology/"
xmlns:j.1="http://dbpedia.org/ontology/"
我能够解决问题。这是下面的代码示例:
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);
}
错误似乎在查询中。我需要使用 dbpedia 文件中的完整 URL 来进行查询。在 dbpedia 文件中,url 看起来像这样:
<http://xmlns.com/foaf/0.1/name> "Autism"@en .
在实际查询中使用 URL,如下所示:
"?autism <http://xmlns.com/foaf/0.1/name> \"Autism\"@en .\n
最终解决了问题。我之前在查询中使用的前缀与 dbpedia 文件中的前缀不直接匹配。所以前缀:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
将不起作用。相反,我需要放置与 dbpedia 文件中找到的直接匹配的 URL。