简体   繁体   中英

Getting error while querying rdf file using jena

I have successfully loaded rdf file. Even i tried this query into protege also with same rdf file. Its running properly. But when i am trying it in eclipse its giving me exception.

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class Test {
public static void main(String[] args) {
    // TODO Auto-generated method stub

sparqltest();}

static void sparqltest()
{

FileManager.get().addLocatorClassLoader(Test.class.getClassLoader());
Model model= FileManager.get().loadModel("C:/Users/avg/workspacejena32/Jena/data.rdf");

String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
    "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
    "SELECT ?y ?x" +
    "WHERE " +
    "{?y foaf:z ?x}";


Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);

try {
ResultSet results = qexec.execSelect();while ( results.hasNext()){
    QuerySolution soln = results.nextSolution();
    Literal name = soln.getLiteral("x");
    System.out.println(name);
}
} 
finally {
qexec.close();
}}}

I am getting following exception...

   Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "-" "- "" at line 1, column 130.
 Was expecting one of:
"graph" ...
"optional" ...
"minus" ...
"bind" ...
"service" ...
"filter" ...
"{" ...
"}" ...
";" ...
"," ...
"." ...

at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:87)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse(ParserSPARQL11.java:40)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:132)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:69)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:28)
at Test.sparqltest(Test.java:31)
at Test.main(Test.java:16)

After removing '-' it gives me null result.

null
null
null
null
null
null
null
null
null

If i do some changes in query such as

    String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
    "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
    "SELECT ?y ?x" +
    "WHERE " +
    "{?y foaf:Studies ?x}";

it gives nothing. Blank result.

Hello Sir thank you very much, problem solved. I did changes as per both of your suggestions. And it worked. Actually i was using wrong variables. Actually i am totally unaware about it. So i gone through sparql tutorials again and got it.

String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
               "PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
               "SELECT * WHERE {" +
               "?person foaf:name ?x .}";

i got the results as follows......

Charlie
Mary
John
George V

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