繁体   English   中英

使用Jena Fuseki服务器查询RDF文件时获取异常

[英]Getting exception while querying rdf file using jena fuseki server

这是我的Java代码,我正在尝试使用耶拿(Jena)查询rdf文件,但它给了我有关文字的异常。 我使用protege工具设计了rdf文件,并尝试使用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)

我的Java代码如下...

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();
    }}}

如果我像这样在查询中做一些更改

"uni:angry uni:says ?words"

然后我得到空结果如下

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

?x不是文字,而是URI或空白节点。

当您使用uni:angry uni:says ?words ,未设置?x。

暂无
暂无

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

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