Jena Sparql输出为.txt或.xls文件
我想知道如何从Jena SPARQL查询的结果中创建.txt或.xls文件输出。有人能帮忙吗 这里是代码的一部分Jena Sparql输出为.txt或.xls文件,sparql,jena,Sparql,Jena,我想知道如何从Jena SPARQL查询的结果中创建.txt或.xls文件输出。有人能帮忙吗 这里是代码的一部分 InputStream in = new FileInputStream(new File("./src/myfile.owl")); Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, null); m
InputStream in = new FileInputStream(new File("./src/myfile.owl"));
Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, null);
model.read(in, null);
in.close();
String queryString =
"prefix : <http://www.semanticweb.org/thato/ontologies/2012/10/9/thesis_ontology_1try#> " +
"prefix rdfs: <" + RDFS.getURI() + "> " +
"prefix owl: <" + OWL.getURI() + "> " +
"prefix rdf: <" + RDF.getURI() + "> " +
"select ?Model " +
"where {" +
"{?Model rdf:type :ApplicationModel. " +
"?Model :hasDomain ?domain. " +
"?domain :domainCode ?domaincode. " +
"FILTER (?domaincode =" + "'"+ domainElement.getAttribute(attrDomain) + "'"+ ")" +
"?Model :hasPhase ?phase." +
"?phase :name ?phasecode. " +
"FILTER (?phasecode = "+ "'"+ phaseElement.getAttribute(attrPhase)+"'"+")" +
"?Model :hasLevelOfDetail ?lod." +
"?lod :name ?lodcode." +
"FILTER (?lodcode = "+ lodElement.getAttribute(attrLOD) +")" +
"}"
;
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results, query);
qe.close();
}
}
InputStream in=newfileinputstream(新文件(“./src/myfile.owl”);
模型模型=ModelFactory.createOntologyModel(OntModelSpec.OWL\u MEM\u MICRO\u RULE\u INF,null);
model.read(in,null);
in.close();
字符串查询字符串=
“前缀:”+
“前缀rdfs:”+
“前缀owl:”+
“前缀rdf:”+
“选择?模型”+
“其中{”+
“{?模型rdf:类型:应用程序模型。”+
“?型号:hasDomain?域。”+
“?域:域代码?域代码。”+
“过滤器(?domaincode=“+””“+domainElement.getAttribute(attrDomain)+”“+””+
“?型号:hasPhase?阶段。”+
“?阶段:名称?阶段代码。”+
“过滤器(?phasecode=“+””“+phaseElement.getAttribute(attrPhase)+”“+””+
“型号:hasLevelOfDetail?lod”+
“详细等级:名称?详细等级代码。”+
“过滤器(?lodcode=“+lodElement.getAttribute(attrLOD)+””+
"}"
;
Query=QueryFactory.create(queryString);
QueryExecution qe=QueryExecutionFactory.create(查询,模型);
ResultSet results=qe.execSelect();
ResultSetFormatter.out(System.out、结果、查询);
qe.close();
}
}
您还没有真正解释您希望输出格式是什么样的,但无论如何我都会尝试回答
Jena将生成ASCII表格样式的基于文本的结果序列化,您可以通过以下方式获得:
ResultSetFormatter.out(System.out, results, query);
这是你的代码已经做的,所以我假设这种格式不适合你
您不能直接生成Excel电子表格,但可以生成CSV,当然可以将CSV导入Excel:
ResultSetFormatter.outputAsCSV(System.out, results);
但是,CSV序列化是一种有损的序列化,它不会将URI缩短为缩写形式—有关此序列化的规范,请参阅
如果您想写入实际文件,只需切换
System.out
以获得适当的OutputStream
实现,例如FileOutputStream
谢谢…您给了我一些制作方法!!我添加了PrintStream out=newprintstream(newfileoutputstream(“./src/output.txt”);系统放样;输出(输出、结果、查询);您知道可以直接传递文件输出流
,对吗?无需包装在打印流中
并更改标准输出流