Java Jena SPARQL查询未检索结果

时间:2018-10-07 16:18:57

标签: java sparql jena dbpedia

我正在尝试在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本体团队值。但是由于某种原因,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查询,并始终获得正确的结果。他是我从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 .
}

我知道Virtuoso SPARQL端点与Apache Jena SPARQL查询略有不同,但是它是相同的常规语法。我为无法在耶拿执行查询而感到困惑。

2 个答案:

答案 0 :(得分:0)

您的数据是三重

<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/"

答案 1 :(得分:0)

我能够解决此问题。这是下面的代码示例:

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直接匹配的URL。