我想将我的sparql查询结果存储到数组中。我该如何存放?这是我的java代码。是否可以在java中使用datareader?
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.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
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");
//System.out.println("Connected...");
String queryString="PREFIX xsd: <http://www.semanticweb.org/avg/ontologies/2016/2/untitled-ontology-5#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"SELECT * " +
"WHERE" +
"{ xsd:angry xsd:says ?x}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);
try {
ResultSet rs = qexec.execSelect();
for ( ; rs.hasNext() ; )
{
QuerySolution soln = rs.nextSolution() ;
RDFNode a = soln.get("x") ;
// avg = a.asNode().getLocalName();
System.out.println(a.asNode().getLocalName());
}
}
finally {
qexec.close();
}}}
我得到如下结果,我希望将其存储在数组或其他内容中,我可以逐个调用它以供将来使用。
selfish
mad
答案 0 :(得分:2)
这样的事情应该这样做。
List<RDFNode> result = new ArrayList<RDFNode>();
ResultSet rs = qexec.execSelect();
for ( ; rs.hasNext() ; )
{
QuerySolution soln = rs.nextSolution() ;
RDFNode a = soln.get("x") ;
// avg = a.asNode().getLocalName();
System.out.println(a.asNode().getLocalName());
result.add(a);
}