简体   繁体   English

RDF4J是否提供一种以表格格式打印漂亮结果的方法?

[英]Does RDF4J offer a way to pretty print results in tabular format?

I am trying to pretty print the results I get from a SPARQL query in RDF4J. 我正在尝试漂亮地打印从RDF4J中的SPARQL查询获得的结果。 Is there a method or class, inside RDF4J that offers this functionality? RDF4J内是否有提供此功能的方法或类? I am not asking for other 3rd party software or additional source code (there is already a question for that with many good answers). 我不要求其他第三方软件或附加的源代码(已经有一个对这个问题有许多很好的答案)。

There's no real "pretty printer" for query results in Rdf4j but I guess the closest is either the CSV (comma-separated-values) or TSV (tab-separated-values) writers. Rdf4j中没有用于查询结果的真正“漂亮打印机”,但我想最接近的是CSV(逗号分隔值)或TSV(制表符分隔值)编写器。 The Rdf4j Workbench offers these formats as options for exporting a query result. Rdf4j Workbench提供了这些格式,作为导出查询结果的选项。 If you want to use them for programmatic execution of a query, you'd do something along these lines: 如果要使用它们来以编程方式执行查询,则可以按照以下步骤进行操作:

// write TSV result to STDOUT (to write to file, use a FileoutputStream instead)
SPARQLResultsTSVWriter writer = new SPARQLResultsTSVWriter(System.out); 
try (RepositoryConnection conn = rep.getConnection()) {
   String query = "SELECT ... ";
   // execute the query and stream the result to the supplied writer
   conn.prepareTupleQuery(query).evaluate(writer);
}

If you need something prettier than that, you could write your own custom query result handler, and use it in place of the SPARQLResultsTSVWriter . 如果您需要比这漂亮的东西,可以编写自己的自定义查询结果处理程序,并使用它代替SPARQLResultsTSVWriter To do this, create a new class that extends AbstractQueryResultWriter , and override any of the methods in there that need customization (in particular, handleNamespace , handleSolution , startQueryResult and endQueryResult ). 为此,请创建一个扩展AbstractQueryResultWriter的新类,并重写其中需要自定义的任何方法(特别是handleNamespacehandleSolutionstartQueryResultendQueryResult )。 Or you could log a feature request for such a class to be added to Rdf4j (or even better: write it yourself then contribute to the project :)) 或者,您可以记录将要添加到Rdf4j的此类的功能请求(甚至更好:您自己编写然后为项目做贡献:))

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

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