Querying DBpedia with SPARQL and Jena

后端 未结 1 1409
孤独总比滥情好
孤独总比滥情好 2021-02-01 10:35

I cannot understand how can I query DBpedia using Jena. In the tutorials like here(Listing 4) model is initialized as follows:



        
1条回答
  •  清酒与你
    2021-02-01 10:51

    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: "+
    "PREFIX dbpedia: "+
    "PREFIX category: "+
    "PREFIX rdfs: "+
    "PREFIX skos: "+
    "PREFIX geo: "+
    
    "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.

    0 讨论(0)
提交回复
热议问题