【问题标题】:HttpException error when I call SPARQL query (on DBPedia) in Java Code当我在 Java 代码中调用 SPARQL 查询(在 DBPedia 上)时出现 HttpException 错误
【发布时间】:2014-08-27 16:42:37
【问题描述】:

我对使用 Java 代码的 SPARQL 端点有疑问。

特别是,我有这个 Java 类:

public class example {

    public static void main(String[] args) {

        String value = "http://dbpedia.org/resource/Fred_Guy";

        example exam = example();
        QueryExecution qe = exam.query(value);
        ResultSet results = ResultSetFactory.copyResults( qe.execSelect() );

    }


    public QueryExecution query(String stringa){

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), dbpediaont:)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

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

        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

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

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;
    }
}

我想检索谓词“RDF:type”的资源“http://dbpedia.org/resource/Fred_Guy”的对象。但是我有这个我不明白的错误:

Exception in thread "main" HttpException: 500
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:340)
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276)
    at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:345)
    at MyPackage.example.main(example.java:19)

为什么会出现这个错误?

我正在尝试在

上执行此查询

http://dbpedia.org/sparql

不写 strstarts 我得到这个错误:

Virtuoso 37000 Error SP031: SPARQL compiler: No one quad map pattern is suitable for GRAPH <http://dbpedia.org> { "http://dbpedia.org/resource/Fred_Guy" <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?resource } triple at line 7

SPARQL query:
define sql:big-data-const 0 
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> prefix dbpediaont: <http://dbpedia.org/ontology/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?resource where {
"http://dbpedia.org/resource/Fred_Guy" rdf:type ?resource
}

我在这里做错了什么?

我尝试在 Virtuoso 中编写此代码:

prefix dbpediaont: <http://dbpedia.org/ontology/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?resource where {
dbpedia:Fred_Guy rdf:type ?resource
}

SPARQL results

如何用 Jena 代码编写它?

【问题讨论】:

    标签: java sparql jena dbpedia httpexception


    【解决方案1】:

    这里有两个查询。在您的问题结束时,您使用没有过滤器的查询,但这与嵌入在您的代码中的查询不同。如果您使用嵌入在 DBpedia 端点上的代码中的查询,您会收到一条非常清晰的错误消息:

    Virtuoso 22023 Error SL001: The SPARQL 1.1 function STRSTARTS() needs a string value as 2d argument
    
    SPARQL query:
    define sql:big-data-const 0 
    #output-format:text/html
    define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> prefix dbpediaont: <http://dbpedia.org/ontology/>
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    
    select ?resource where {
    ?mat rdf:type ?resource
    filter strstarts(str(?resource), dbpediaont:)
    }
    

    关键是

    SPARQL 1.1 函数 STRSTARTS() 需要一个字符串值作为 2d 参数

    您需要用str() 编写dbpediaont:,因为它是IRI,而不是字符串:

    filter strstarts(str(?resource), str(dbpediaont:))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 2010-11-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多