使用自定义词汇表将ResultSet作为RDF流式传输

时间:2015-10-14 22:44:56

标签: java stream rdf resultset jena

我需要将从查询中获取的Jena com.hp.hpl.jena.query.ResultSet流式传输到转换为RDF输出格式的远程端点。 我知道Jena为此提供ResultSetFormatter.toModel设施,但是我有以下限制:

  • 我想使用不同的表示/词汇而不是Jena提供的表达/词汇
  • 我不想在内存中加载数据。换句话说,我不想创建一个Model并用ResultSet填充它,但是在我迭代时流出三元组来控制内存消耗。
  • 我仍然希望受益于Jena序列化工具

我见过StreamRDF界面,但我不清楚如何有效地使用它。 在这种情况下,什么是正确的方法?

1 个答案:

答案 0 :(得分:0)

我实施的解决方案对我有用,所以我将其作为答案发布。 下面的代码段应该足够提供信息,因为它涵盖了问题中的要求。值得注意的是,我创建了两个类,一个ResultSetTripleIterator和一个QuerySolutionToTripleAdapter接口。第一个是负责向作者传输三元组,而第二个是从每个QuerySolution构建一个三元组的迭代器。

public class ResultSetTripleIterator implements Iterator<Triple> {
   private ResultSet rs;
   private QuerySolutionToTripleAdapter ad;
   private Iterator<Triple> it = null;
   public ResultSetTripleIterator(ResultSet resultSet, QuerySolutionToTripleAdapter adapter) {
      this.rs = resultSet;
      this.ad = adapter;
   }
   @Override
   public boolean hasNext() {
      if(it != null && it.hasNext()){
         return true;
      }
      it = null;
      return rs.hasNext();
   }
   @Override
   public Triple next() {
       if(it == null){
           it = ad.adapt(rs.next());
       }
       return it.next();
   }
}
public interface QuerySolutionToTripleAdapter {
    public Iterator<Triple> adapt(QuerySolution qs);
}

以下是应用程序示例:

// Can be any OutputStream
OutputStream os = new ByteArrayOutputStream();
StreamRDF stream = StreamRDFWriter.getWriterStream(os, Lang.TRIG);
QueryExecution qe = QueryExecutionFactory.sparqlService(
        "http://data.open.ac.uk/sparql", "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?A ?B ?C WHERE {?A a ?B . ?A rdf:type ?C} LIMIT 100");

Iterator<Triple> iter = new ResultSetTripleIterator(qe.execSelect(), new QuerySolutionToTripleAdapter() {
    Integer rowIndex = 0;

    @Override
    public Iterator<Triple> adapt(QuerySolution qs) {
        rowIndex++;
        String ns = "http://www.example.org/test/row#";
        String pns = "http://www.example.org/test/col#";
        Resource subject = ResourceFactory.createResource(ns + Integer.toString(rowIndex));
        Property property;
        List<Triple> list = new ArrayList<Triple>();
        Iterator<String> cn = qs.varNames();
        while (cn.hasNext()) {
            String c = cn.next();
            property = ResourceFactory.createProperty(pns + c);
            list.add(new Triple(subject.asNode(), property.asNode(), qs.get(c).asNode()));
        }
        return list.iterator();
    }
});
stream.start();
StreamOps.sendTriplesToStream(iter, stream);
stream.finish();

但是,Jena似乎不支持某些用于流式传输的RDF序列化,即XML和JSON格式,例如导致org.apache.jena.riot.RiotException: No serialization for language Lang:rdf/null

修改

感谢Jena用户邮件列表中的反馈,可以使用Jena的实用程序类来操作迭代器和执行转换来压缩上面的代码。

该片段可以按如下方式重写:

OutputStream os = new ByteArrayOutputStream();
StreamRDF stream = StreamRDFWriter.getWriterStream(os, Lang.RDFTHRIFT);
QueryExecution qe = QueryExecutionFactory.sparqlService(
        "http://data.open.ac.uk/sparql", "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT ?A ?B ?C WHERE {?A a ?B . ?A rdf:type ?C} LIMIT 100");

Transform<QuerySolution, Iterator<Triple>> m = new Transform<QuerySolution, Iterator<Triple>>() {
    Integer rowIndex = 0;

    @Override
    public Iterator<Triple> convert(QuerySolution qs) {
        rowIndex++;
        String ns = "http://www.example.org/test/row#";
        String pns = "http://www.example.org/test/col#";
        Resource subject = ResourceFactory.createResource(ns + Integer.toString(rowIndex));
        Property property;
        List<Triple> list = new ArrayList<Triple>();
        Iterator<String> cn = qs.varNames();
        while (cn.hasNext()) {
            String c = cn.next();
            property = ResourceFactory.createProperty(pns + c);
            list.add(new Triple(subject.asNode(), property.asNode(), qs.get(c).asNode()));
        }
        return list.iterator();
    }
};
Iterator<Triple> iter = WrappedIterator.createIteratorIterator( Iter.map( qe.execSelect(), m ));
stream.start();
StreamOps.sendTriplesToStream(iter, stream);
stream.finish();