简体   繁体   English

使用Java针对DBPedia的SPARQL查询

[英]SPARQL query against DBPedia using Java

I would like to query on DBPedia using Java. 我想使用Java在DBPedia上进行查询。 Below is my code and it does not return corrrect result.I want to get Abstract part from [ http://dbpedia.org/page/Ibuprofen page and label name. 下面是我的代码,它没有返回正确的结果。我想从[ http://dbpedia.org/page/Ibuprofen页面和标签名称中获取摘要部分。 but it returns only http://dbpedia.org/resource/Ibuprofen 11 times. 但它只返回11次http://dbpedia.org/resource/Ibuprofen If possible could you tell me where is the mistake? 如果可能的话,您能告诉我哪里出了问题吗? This is my code: 这是我的代码:

import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.ResourceFactory;

public class JavaDBPediaExample {

    public static void main(String[] args) {
        ParameterizedSparqlString qs = new ParameterizedSparqlString(""
                + "prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n"
                + "PREFIX dbo:     <http://dbpedia.org/ontology/>"
                + "\n"
                + "select ?resource where {\n"
                + "  ?resource rdfs:label ?label.\n"
                + "  ?resource dbo:abstract ?abstract.\n"
                + "}");

        Literal ibuprofen = ResourceFactory.createLangLiteral("Ibuprofen", "en");
        qs.setParam("label", ibuprofen);

        QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());

        ResultSet results = exec.execSelect();

        while (results.hasNext()) {

            System.out.println(results.next().get("resource"));
        }

        ResultSetFormatter.out(results);
    }
}

You have multiple results because there are multiple language variants in DBPedia. 您有多个结果,因为DBPedia中有多种语言变体。 Work out which language you want and change the filter below accordingly. 找出所需的语言,然后相应地更改下面的过滤器。 You can include the label pattern in the query as well instead ok doing it programmatically. 您也可以在查询中包括标签模式,而可以通过编程来完成。 As per ASKW's comment, you also haven't bound the abstract variable to the result. 根据ASKW的评论,您还没有将抽象变量绑定到结果。

Basically your code should looks something like this: 基本上,您的代码应如下所示:

public static void main(String[] args) {
        ParameterizedSparqlString qs = new ParameterizedSparqlString(""
                + "prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n"
                + "PREFIX dbo:     <http://dbpedia.org/ontology/>"
                + "\n"
                + "select distinct ?resource ?abstract where {\n"
                + "  ?resource rdfs:label 'Ibuprofen'@en.\n"
                + "  ?resource dbo:abstract ?abstract.\n"
                + "  FILTER (lang(?abstract) = 'en')}");


        QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());

        ResultSet results = exec.execSelect();

        while (results.hasNext()) {

            System.out.println(results.next().get("abstract").toString());
        }

        ResultSetFormatter.out(results);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM