Search in sources :

Example 1 with PipedRDFIterator

use of org.apache.jena.riot.lang.PipedRDFIterator in project jena by apache.

the class ExRIOT_6 method main.

public static void main(String... argv) {
    final String filename = "data.ttl";
    // Create a PipedRDFStream to accept input and a PipedRDFIterator to
    // consume it
    // You can optionally supply a buffer size here for the
    // PipedRDFIterator, see the documentation for details about recommended
    // buffer sizes
    PipedRDFIterator<Triple> iter = new PipedRDFIterator<>();
    final PipedRDFStream<Triple> inputStream = new PipedTriplesStream(iter);
    // PipedRDFStream and PipedRDFIterator need to be on different threads
    ExecutorService executor = Executors.newSingleThreadExecutor();
    // Create a runnable for our parser thread
    Runnable parser = new Runnable() {

        @Override
        public void run() {
            RDFParser.source(filename).parse(inputStream);
        }
    };
    // Start the parser on another thread
    executor.submit(parser);
    // far ahead of our consumption as the buffer size allows
    while (iter.hasNext()) {
        Triple next = iter.next();
    // Do something with each triple
    }
}
Also used : Triple(org.apache.jena.graph.Triple) PipedTriplesStream(org.apache.jena.riot.lang.PipedTriplesStream) ExecutorService(java.util.concurrent.ExecutorService) PipedRDFIterator(org.apache.jena.riot.lang.PipedRDFIterator)

Example 2 with PipedRDFIterator

use of org.apache.jena.riot.lang.PipedRDFIterator in project jena by apache.

the class RDFDataMgr method createIteratorQuads.

/**
     * Creates an iterator over parsing of quads
     * @param input Input Stream
     * @param lang Language
     * @param baseIRI Base IRI
     * @return Iterator over the quads
     */
public static Iterator<Quad> createIteratorQuads(InputStream input, Lang lang, String baseIRI) {
    // Special case N-Quads, because the RIOT reader has a pull interface
    if (RDFLanguages.sameLang(RDFLanguages.NQUADS, lang)) {
        return new IteratorResourceClosing<>(RiotParsers.createIteratorNQuads(input, null, RiotLib.dftProfile()), input);
    }
    // Otherwise, we have to spin up a thread to deal with it
    final PipedRDFIterator<Quad> it = new PipedRDFIterator<>();
    final PipedQuadsStream out = new PipedQuadsStream(it);
    Thread t = new Thread(() -> parseFromInputStream(out, input, baseIRI, lang, null));
    t.start();
    return it;
}
Also used : Quad(org.apache.jena.sparql.core.Quad) IteratorResourceClosing(org.apache.jena.atlas.iterator.IteratorResourceClosing) PipedRDFIterator(org.apache.jena.riot.lang.PipedRDFIterator) PipedQuadsStream(org.apache.jena.riot.lang.PipedQuadsStream)

Example 3 with PipedRDFIterator

use of org.apache.jena.riot.lang.PipedRDFIterator in project jena by apache.

the class RDFDataMgr method createIteratorTriples.

/**
     * Create an iterator over parsing of triples
     * @param input Input Stream
     * @param lang Language
     * @param baseIRI Base IRI
     * @return Iterator over the triples
     */
public static Iterator<Triple> createIteratorTriples(InputStream input, Lang lang, String baseIRI) {
    // Special case N-Triples, because the RIOT reader has a pull interface
    if (RDFLanguages.sameLang(RDFLanguages.NTRIPLES, lang))
        return new IteratorResourceClosing<>(RiotParsers.createIteratorNTriples(input, null), input);
    // Otherwise, we have to spin up a thread to deal with it
    PipedRDFIterator<Triple> it = new PipedRDFIterator<>();
    PipedTriplesStream out = new PipedTriplesStream(it);
    Thread t = new Thread(() -> parseFromInputStream(out, input, baseIRI, lang, null));
    t.start();
    return it;
}
Also used : Triple(org.apache.jena.graph.Triple) PipedTriplesStream(org.apache.jena.riot.lang.PipedTriplesStream) PipedRDFIterator(org.apache.jena.riot.lang.PipedRDFIterator)

Aggregations

PipedRDFIterator (org.apache.jena.riot.lang.PipedRDFIterator)3 Triple (org.apache.jena.graph.Triple)2 PipedTriplesStream (org.apache.jena.riot.lang.PipedTriplesStream)2 ExecutorService (java.util.concurrent.ExecutorService)1 IteratorResourceClosing (org.apache.jena.atlas.iterator.IteratorResourceClosing)1 PipedQuadsStream (org.apache.jena.riot.lang.PipedQuadsStream)1 Quad (org.apache.jena.sparql.core.Quad)1