Jena Sparql输出为.txt或.xls文件

时间:2013-01-30 10:23:55

标签: 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);
                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();

        }
    }

1 个答案:

答案 0 :(得分:6)

你还没有真正解释你想要的输出格式是什么样的,但无论如何我都会有机会回答。

Jena将生成基于文本的结果序列化,这是ASCII表格样式,您可以通过以下方式获得此结果:

ResultSetFormatter.out(System.out, results, query);

这是您的代码已经执行的操作,所以我会假设这种格式对您不满意吗?

您无法直接生成Excel电子表格,但您可以生成CSV,当然可以将其导入Excel:

ResultSetFormatter.outputAsCSV(System.out, results);

然而,CSV序列化是一个有损的序列化,它不会将URI缩短为缩写形式 - 有关此规范,请参阅SPARQL 1.1 Query Results CSV and TSV

如果您想要写入实际文件,只需切换System.out以进行相应的OutputStream实施,例如FileOutputStream