Search in sources :

Example 1 with PipedTriplesStream

use of org.apache.jena.riot.lang.PipedTriplesStream 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 PipedTriplesStream

use of org.apache.jena.riot.lang.PipedTriplesStream 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

Triple (org.apache.jena.graph.Triple)2 PipedRDFIterator (org.apache.jena.riot.lang.PipedRDFIterator)2 PipedTriplesStream (org.apache.jena.riot.lang.PipedTriplesStream)2 ExecutorService (java.util.concurrent.ExecutorService)1