Dbpedia Jena Query Returning Null

DBpedia Jena Query returning null

that is so embarassing, there is space problem in the query:

String service = "http://dbpedia.org/sparql";
String queryString = "";
queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?label " +
"WHERE {" +
"<http://dbpedia.org/resource/Quatre_Bornes> <http://dbpedia.org/ontology/country> ?y ."+
"?y rdfs:label ?label ."+
"FILTER (LANG(?label) = 'en')"+
"}";

SPARQL query returns null

I am going to focus on the query, not the rest of the code:

  • there is no skos:subject, you need dct:subject
  • instead of p:abstract, you need dbo:abstract

The corrected query:

PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.georss.org/georss/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?m ?n ?p ?d WHERE {
?m rdfs:label ?n.
?m dct:subject ?c.
?c skos:broader category:Churches_in_Paris.
?m dbo:abstract ?d.
?m geo:point ?p
}

Some other points:

  • consider filtering abstracts and labels to just those in the language that's interesting for you (probably English)
  • consider making geo:point OPTIONAL as not all results have it
  • consider sub-sub-categories of the parent category, not just sub-categories
  • consider using variable names that are more descriptive than just a single seemingly random letter

Querying DBpedia with SPARQL and Jena

After browsing tons and tons of pages I found the answer. Perhaps I didn't ask the question clearly enough, but anyway below is the code that worked for me.

String queryString=
"PREFIX p: <http://dbpedia.org/property/>"+
"PREFIX dbpedia: <http://dbpedia.org/resource/>"+
"PREFIX category: <http://dbpedia.org/resource/Category:>"+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"+
"PREFIX geo: <http://www.georss.org/georss/>"+

"SELECT DISTINCT ?m ?n ?p ?d"+
"WHERE {"+
" ?m rdfs:label ?n."+
" ?m skos:subject ?c."+
" ?c skos:broader category:Churches_in_Paris."+
" ?m p:abstract ?d."+
" ?m geo:point ?p"+
" FILTER ( lang(?n) = "fr" )"+
" FILTER ( lang(?d) = "fr" )"+
" }"

// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {

// Result processing is done here.
}
}
finally {
qexec.close();
}

This answer I found on dbpedia-discussion of www.mail-archive.com page.

Jena sparql (Dbpedia) query OPTIONAL filter gives no results but (http://dbpedia.org/snorql/) same query works

ok i figured it out after logging several things... First ive loged the resultset size, with the optional filter it gave me more results than if i put no optional filter... 2383 vs 2103(without optional).. then i noticied that if

a.setPictureURL(row.get("thumb2").toString()); is empty or null the loop dont continue... it stoped after 14 results and gave me no return for this method :

private LinkedList<Entity> collectAuthors(ResultSet results) {
LinkedList<Entity> temp = new LinkedList<>();
while (results.hasNext()) {
Entity a = new Entity();
QuerySolution row = results.next();
String fullName = row.get("name").toString().substring(0, row.get("name").toString().indexOf("@"));
String biography = row.get("desc").toString().substring(0, row.get("desc").toString().indexOf("@"));
a.setTitle(fullName);
a.setWikiID(Integer.parseInt(row.get("wiki").toString().substring(0, row.get("wiki").toString().indexOf("^"))));
if (!row.get("thumb").toString().isEmpty())
a.setPictureURL(row.get("thumb").toString());

a.setBiography(biography);
temp.add(a);
System.out.println("FAAT" + a.getTitle());
}
return temp;
}

so i just checked with if(row.getResource("thumb2")!=null) like that

    if(row.getResource("thumb")!=null)
a.setPictureURL(row.get("thumb").toString());

and then the loop continued ---> problem solved

Thanks everyone for your hints and be carefull of empty reslutset columns with Jena

SPARQL queries don't work with Jena

Did you try the query at dbpedia.org?

When I run it there, through the web form interface, I get zero rows.

{ dbpedia:The_Beatles mo:member ?member } has zero matches. There are no http://purl.org/ontology/mo/ triples.

Try { dbpedia:The_Beatles ?pred ?obj} and look at the details.

Sparql query does not return uri/object

There is a typo in your query, a missing space. Note that the output should be "object", not "objectWHERE". Also, there is no need for your ?url variable, since you are not using it in your query. Replace this fragment in your code and it will work:

 // Create a new query
String queryString =
"SELECT ?property ?object " +
"WHERE {" +
" <"+resourceURI+"> ?property ?object ." +
"}";

Query query = QueryFactory.create(queryString);

Also, I don't get why you first read the resource locally and then issue the sparql query against the local graph. Why not doing it directly against the dbpedia endpoint?
Something like this:

String resourceURI = "http://dbpedia.org/resource/Roger_Federer";
String queryString =
"SELECT ?property ?object " +
"WHERE {" +
" <"+resourceURI+"> ?property ?object ." +
"}";
Query query = QueryFactory.create(queryString);
//System.out.println(queryIn);
QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results, query);
qe.close();

I think it is cleaner.



Related Topics



Leave a reply



Submit