在Java代码中调用SPARQL查询(在DBPedia上)时出现HttpException错误 [英] HttpException error when I call SPARQL query (on DBPedia) in Java Code

查看:182
本文介绍了在Java代码中调用SPARQL查询(在DBPedia上)时出现HttpException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Java代码时遇到SPARQL端点问题。

I have a problem with SPARQL endpoint using Java Code.

特别是,我有这个Java类:

In particular, I have this Java Class:

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

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

I would like to retrieve the objects of the resource "http://dbpedia.org/resource/Fred_Guy" whose predicate "RDF:type". But I have this error that I don't understand:

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)

为什么我收到此错误?

I我试图在


http://dbpedia.org/sparql

不写strstarts我收到此错误:

without to write strstarts and I get this error:

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中编写这段代码:

I tried to write this code in 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结果

如何用Jena代码编写它?

How I can write it in Jena code?

推荐答案

你这里有两个疑问。在问题的最后,您使用的查询没有过滤器,但是与您的代码中嵌入的查询不同。如果您使用DBpedia端点上的代码中嵌入的查询,您会收到一条非常明确的错误消息:

You've got two queries here. At the end of your question, you use a query that has no filter, but that's different from the query that's embedded in your code. If you use the query embedded in your code on DBpedia's endpoint, you get a very clear error message:

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参数

The SPARQL 1.1 function STRSTARTS() needs a string value as 2d argument

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

You need to write dbpediaont: with str() since it's an IRI, not a string:

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

这篇关于在Java代码中调用SPARQL查询(在DBPedia上)时出现HttpException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆