Search in sources :

Example 11 with ResultSetStream

use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.

the class TableBase method toResultSet.

@Override
public ResultSet toResultSet() {
    QueryIterator qIter = iterator(null);
    ResultSet rs = new ResultSetStream(getVarNames(), null, qIter);
    rs = ResultSetFactory.makeRewindable(rs);
    qIter.close();
    return rs;
}
Also used : QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 12 with ResultSetStream

use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.

the class CSVInput method fromCSV.

public static ResultSet fromCSV(InputStream in) {
    CSVParser parser = CSVParser.create(in);
    final List<Var> vars = vars(parser);
    List<String> varNames = Var.varNames(vars);
    Function<List<String>, Binding> transform = new Function<List<String>, Binding>() {

        private int count = 1;

        @Override
        public Binding apply(List<String> row) {
            if (row.size() != vars.size())
                FmtLog.warn(log, "Row %d: Length=%d: expected=%d", count, row.size(), vars.size());
            BindingMap binding = BindingFactory.create();
            // Check.
            for (int i = 0; i < vars.size(); i++) {
                Var v = vars.get(i);
                String field = (i < row.size()) ? row.get(i) : "";
                Node n = NodeFactory.createLiteral(field);
                binding.add(v, n);
            }
            count++;
            return binding;
        }
    };
    Iterator<Binding> bindings = Iter.map(parser.iterator(), transform);
    //This will parse actual result rows as needed thus minimising memory usage
    return new ResultSetStream(varNames, null, bindings);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) Function(java.util.function.Function) CSVParser(org.apache.jena.atlas.csv.CSVParser) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with ResultSetStream

use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.

the class JSONInput method process.

// TODO Streaming version of JSON Result set processing
private SPARQLResult process(InputStream in, Model model) {
    parse(in);
    if (model == null)
        model = GraphFactory.makeJenaDefaultModel();
    if (rows != null) {
        QueryIterator qIter = new QueryIterPlainWrapper(rows.iterator());
        ResultSet rs = new ResultSetStream(Var.varNames(vars), model, qIter);
        super.set(rs);
    } else
        super.set(booleanResult);
    return this;
}
Also used : QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Example 14 with ResultSetStream

use of org.apache.jena.sparql.engine.ResultSetStream in project jena by apache.

the class BIOInput method fromBIO.

public static ResultSet fromBIO(InputStream input) {
    // Scan the stream for VARS and accumulate the total variables. 
    // Trade off of guessing first line is all the VARS and coping with
    // anything possible.  -> Cope with anything possible.
    BindingInputStream bin = new BindingInputStream(input);
    List<Binding> bindings = new ArrayList<>();
    List<Var> vars = new ArrayList<>();
    while (bin.hasNext()) {
        Binding b = bin.next();
        bindings.add(b);
        for (Var v : bin.vars()) {
            if (!vars.contains(v))
                vars.add(v);
        }
    }
    QueryIterator qIter = new QueryIterPlainWrapper(bindings.iterator());
    return new ResultSetStream(Var.varNames(vars), m, qIter);
}
Also used : BindingInputStream(org.apache.jena.sparql.engine.binding.BindingInputStream) Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) Var(org.apache.jena.sparql.core.Var) ArrayList(java.util.ArrayList) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream)

Aggregations

ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)14 ArrayList (java.util.ArrayList)10 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)10 ResultSet (org.apache.jena.query.ResultSet)8 Var (org.apache.jena.sparql.core.Var)7 Binding (org.apache.jena.sparql.engine.binding.Binding)7 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)7 Node (org.apache.jena.graph.Node)4 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)4 IOException (java.io.IOException)2 Op (org.apache.jena.sparql.algebra.Op)2 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)2 BufferedReader (java.io.BufferedReader)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Function (java.util.function.Function)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 Triple (org.apache.jena.graph.Triple)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 RiotException (org.apache.jena.riot.RiotException)1