You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

SPARQL DESCRIBE查询在Java中执行失败求助

Troubleshooting Jena DESCRIBE Query Error with DBpedia SPARQL Service

Let's break down what's causing this error and how to fix it:

First, the error message org.apache.jena.riot.RiotException: [line: 898, col: 11]. Failed to find a prefix name or keyword: –(8211;0x2013) points directly to a problematic character: the en dash (, U+2013) instead of a standard ASCII hyphen (-, U+002D). This typographic dash often creeps into queries when copying from rich-text sources (like formatted web pages or word processors) that auto-replace hyphens with fancier dashes—and Jena's SPARQL parser doesn't recognize it as valid syntax.

Step 1: Clean Up Your SPARQL Query

First, replace all instances of the en dash with a standard hyphen. Here's the corrected query:

PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
DESCRIBE ?person 
WHERE { 
  ?person rdf:type foaf:Person . 
  FILTER EXISTS { ?person owl:sameAs ?sameAs } 
} 
LIMIT 100

Step 2: Build the Query Safely in Jena

When using ParameterizedSparqlString, avoid pasting formatted text that might hide special characters. Instead, build the query incrementally to ensure clean syntax:

import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.rdf.model.Model;

public class DBpediaDescribeExample {
    public static void main(String[] args) {
        ParameterizedSparqlString pss = new ParameterizedSparqlString();
        
        // Add prefixes explicitly to avoid syntax issues
        pss.setNsPrefix("foaf", "http://xmlns.com/foaf/0.1/");
        pss.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        pss.setNsPrefix("owl", "http://www.w3.org/2002/07/owl#");
        
        // Use standard hyphens in the query text
        pss.setCommandText("""
            DESCRIBE ?person 
            WHERE { 
              ?person rdf:type foaf:Person . 
              FILTER EXISTS { ?person owl:sameAs ?sameAs } 
            } 
            LIMIT 100
        """);
        
        // Execute the query
        try (QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", pss.asQuery())) {
            Model result = qe.execDescribe();
            // Process your result model here (e.g., print it)
            result.write(System.out, "TTL");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 3: Verify the Fix

  1. First, test the cleaned-up SPARQL query directly in the DBpedia SPARQL endpoint to confirm it still works as expected.
  2. Run the Java code above—this should resolve the RiotException since we've eliminated the unrecognized en dash character.

The core issue here is strict syntax checking in Jena's parser: SPARQL requires standard ASCII punctuation for keywords and operators, so always double-check for non-ASCII characters when copying queries between environments.

内容的提问来源于stack exchange,提问作者gambit

火山引擎 最新活动