简体   繁体   中英

Getting exception while querying rdf file using jena fuseki server

This is my java code and i am trying to query on my rdf file using jena but its giving me exception about literal. I designed rdf file using protege tool and trying to query it using jena.

Exception in thread "main" java.lang.ClassCastException: com.hp.hpl.jena.rdf.model.impl.ResourceImpl cannot be cast to com.hp.hpl.jena.rdf.model.Literal
at com.hp.hpl.jena.sparql.core.QuerySolutionBase.getLiteral(QuerySolutionBase.java:26)
at emotion.sparqltest(emotion.java:36)
at emotion.main(emotion.java:16)

My java code is as follows...

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 emotion {
public static void main(String[] args) {
    // TODO Auto-generated method stub

sparqltest();}

static void sparqltest()
{

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

String queryString="PREFIX uni:<http://www.semanticweb.org/avg/ontologies/2016/2/untitled-ontology-5#>" +
               "SELECT * WHERE {" +
               "uni:angry uni:says ?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();
    }}}

If i do some changes in my query like this

"uni:angry uni:says ?words"

Then i get the null result as follows

null
null
Literal name = soln.getLiteral("x");

?x is not a literal - it is a URI or a blank node.

When you use uni:angry uni:says ?words , the ?x is not set.

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