I am trying to use Jena in Java to run a SPARQL query and retrieve the results from a .ttl
file. 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. 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
:
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. ?simeon
in this example is the athlete that is being looked up, and Simeon will reflect the foaf:name "Simeon Ikudabo"@en
. My last triple statement is ?simeon dbo:team ?team
. This should find dbpedia ontology team value that corresponds with Simeon Ikudabo. For some reason however, the ResultSet
does not retrieve this value. 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:
<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. 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. I created a SIMILAR query using the Virtuoso SPARQL endpoint for DBpedia and consistently get the proper results. He is a similar query that I ran as a test from the Virtuoso SPARQL endpoint for 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. I am stumped as to why I cannot execute a query in Jena.
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. In dbpedia file the url looks like this:
<http://xmlns.com/foaf/0.1/name> "Autism"@en .
Using that URL within the actual query like this:
"?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. 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.