0

我需要将 .ttl QuerySolution 结果导出/写入 RDF/XML 文件。

我已经尝试了下面的代码,但我收到以下错误RDFDataMgr.write

The method write(OutputStream, Model, Lang) in the type RDFDataMgr is not applicable for the arguments (OutputStream, QuerySolution, Lang)

Query query = QueryFactory.create(queryString);
QueryExecution qexec= QueryExecutionFactory.create(query, model2);
try {
    ResultSet resultat= qexec.execSelect();
    while (resultat.hasNext()) {
        QuerySolution sol=resultat.nextSolution();

        String outfile = "/auto_home/rdftest/outfile.rdf";
        OutputStream out = new FileOutputStream(outfile);

        RDFDataMgr.write(out, sol, Lang.RDFXML); 
    }
} finally {
    qexec.close(); 
}
4

1 回答 1

1

SPARQL 支持两种主要类型的查询:SELECT 查询和 CONSTRUCT 查询。

SELECT 查询返回一个解决方案表。您正在运行 SELECT 查询。

CONSTRUCT 查询从解决方案创建一个新的 RDF 图。

Turtle 和 RDF/XML 是 RDF 图的格式。它们不是解决方案表的格式。因此,您只能将 CONSTRUCT 查询的结果写入这些格式。

因此,您可以将查询更改为 CONSTRUCT 查询并使用适当的 API 来执行它们(execConstruct() 返回模型而不是 ResultSet),或者使用 ResultSetFormatter 编写整个解决方案表(而不是表中的每一行)正在尝试这样做)到为此目的而存在的一种格式:JSON、CSV、TSV、XML。

于 2019-03-28T06:26:34.927 回答