Pergunta

Eu não consigo entender como eu posso consultar DBpedia usando Jena. Nos tutoriais como aqui (Listagem 4) modelo é inicializado da seguinte forma:

// Open the bloggers RDF graph from the filesystem
InputStream in = new FileInputStream(new File("bloggers.rdf"));

// Create an empty in-memory model and populate it from the graph
Model model = ModelFactory.createMemModelMaker().createModel();
model.read(in,null); // null base URI, since model URIs are absolute
in.close();

Vamos dizer que eu quero escrever uma consulta que igrejas lista irá em Paris. Em SPARQL será parecido com (retirado esta mensagem lista ):

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" )
 }

Como será este olhar consulta em Java? Particularmente, eu estou interessado em saber como o modelo de objeto é inicializado.

Foi útil?

Solução

Depois de navegar toneladas e toneladas de páginas eu encontrei a resposta. Talvez eu não faça a pergunta de forma suficientemente clara, mas de qualquer maneira a seguir é o código que funcionou para mim.

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();
}

Esta resposta eu encontradas no DBpedia-discussão de www .mail-archive.com página .

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top