简体   繁体   English

Java Apache Jena SparQL查询在查询有效时返回词法错误

[英]Java Apache Jena SparQL query returns Lexical error when query is valid

I have the following SparQL query: 我有以下SparQL查询:

SELECT ?b ?c WHERE {
ont:http\:\/\/test.com\/test\#com.test.test.test.2.3.4 ?b ?c}

which returns the successfully returns the correct results in OpenRDF Workbench 2.6.5 . 成功返回的结果在OpenRDF Workbench 2.6.5中返回正确的结果。 However in my Java which is using Jena 2.6.4 when I run the query using the following code 但是在我使用Jena 2.6.4的 Java中,当我使用以下代码运行查询时

private static ResultSet getQueryResults(String stringQuery, String service) {
    Query query = QueryFactory.create(stringQuery);
    QueryExecution qexec = QueryExecutionFactory.sparqlService(
            service, query);
    ResultSet results = qexec.execSelect(); 
    //ResultSetFormatter.out(System.out, results, query);
    return results;
}

public static ResultSet getDetails(String ID) {
    //we define our sparql query
    String sanatizedID = "ont:" +  sanitizeString(ID);
    String stringQuery = " SELECT ?b ?c WHERE" +
            " { " + sanatizedID + 
            //"{?a"+
            " ?b" +
            " ?c}";
    System.out.println(stringQuery);
    //we define our service
    String service = "http://test.test.com:8181/test-sesame/repositories/test";

    //We write the results of our query into a results set
    ResultSet results = getQueryResults(stringQuery, service);
    return results;
}

private static String sanitizeString(String s) {
    s = s.replace("/", "\\/");
    s = s.replace("#", "\\#");
    s = s.replace(":", "\\:");
    s = s.replace("\"", "\\\"");

    return s;
}

I get the following error 我收到以下错误

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Lexical error at line 1, column 871.  Encountered: ":" (58), after : "\\" 

on line Query query = QueryFactory.create(stringQuery); 在线查询查询= QueryFactory.create(stringQuery);

I've tried changing Jena version and no luck, can't understand why the query is valid in OpenRDF Workbench 2.6.5 and not in my Java query (I think it may do it's own sanatization) :/ 我尝试过更改Jena版本,但没有运气,无法理解为什么查询在OpenRDF Workbench 2.6.5中有效,而在我的Java查询中无效(我认为这可能是由于自己的原因):/

Help would be really appreciated! 帮助将不胜感激!

"ont:" + sanitizeString(ID);

您需要在查询中为ont:声明前缀,或使用完整格式和<...>

You are using a version of Sesame that supports a slightly older version of the SPARQL 1.1 specification, your prefixed name is not actually valid: 您正在使用的芝麻版本支持稍早一些的SPARQL 1.1规范版本,您的前缀名称实际上无效:

ont:http\:\/\/test.com\/test\#com.test.test.test.2.3.4

With the latest versions of the SPARQL specification it now permitted to use additional : characters directly in a prefixed name and there is no need to escape this with a \\ . 使用最新版本的SPARQL规范,现在允许在前缀名称中直接使用附加的:字符,而无需使用\\对此字符进行转义。 So to make this query work with Jena you simply need to remove the \\ before your second : 因此,要使此查询与Jena一起使用,您只需要在第二秒之前删除\\ :

I'm not sure where Sesame is with its alignment with the latest SPARQL 1.1 specifications but if you try this query on a more recent version of Sesame you will likely see behaviour that aligns with Jena. 我不确定Sesame与最新的SPARQL 1.1规范的一致性在哪里,但是如果您在较新版本的Sesame上尝试此查询,则可能会看到符合Jena的行为。

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

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