简体   繁体   中英

Java - Jena QueryParseException

I'm new to Jena and SPARQL. I'm trying to run Jena in Eclipse with following request and code. I getting QueryParseException , I know that other people had the same problem with an undefined rdfs prefix but here it's different.

Exception:

Exception in thread "main" org.apache.jena.query.QueryParseException: Line 1, column 134: Unresolved prefixed name: http:

Query:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject ?fsn
WHERE {
  ?subject rdfs:label ?fsn.
  ?subject rdfs:subClassOf+ http://snomed.info/id/410607006
}

Code:

import java.util.HashMap;
import org.apache.jena.query.*;

public class SnomedQuery {
  private String serviceURI;
  private String query;

  //Constructor with SPARQL endpoint and query
  public SnomedQuery(String URI, String serviceURI){
    this.serviceURI = serviceURI;
    this.query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
              " SELECT ?subject ?fsn WHERE {?subject rdfs:label ?fsn. ?subject rdfs:subClassOf+ "+URI+"}";
  }

  //return SPARQL endpoint
  public String getServiceURI() {
    return this.serviceURI;
  }

  //return query
  public String getQuery() {
    return this.query;
  }

  /*
  * purpose: This function is used to retrieve all child of a concept and the concept itself
  * @param
  * @return
  *   Hashmap with URI as key and corresponding term as value
  */
  public HashMap<String, String> getFSNChildPlus(){
    HashMap<String, String> output = new HashMap<String, String>();

    QueryExecution q = QueryExecutionFactory.sparqlService(getServiceURI(), getQuery());
    ResultSet results = q.execSelect();

    while (results.hasNext()) {
        QuerySolution answer = results.nextSolution();
        String subject = answer.get("subject").toString();
        String fsn = answer.get("fsn").toString();

        output.put(subject, fsn);
    }
    return null;
  }
}

Thank you for your help

In SPARQL, URIs need to be marked up.

So http://snomed.info/id/410607006 is wrong but <http://snomed.info/id/410607006> is ok.

Here is your query :

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject ?fsn
WHERE {
  ?subject rdfs:label ?fsn.
  ?subject rdfs:subClassOf+ <http://snomed.info/id/410607006>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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