Jena read from turtle fails

后端 未结 2 1598
半阙折子戏
半阙折子戏 2021-01-12 11:47

I have just imported jena libraries to eclipse to work on rdf-s and it is my first try, but I cannot read a turtle (.ttl) file.

I tried it in the following way:

相关标签:
2条回答
  • 2021-01-12 12:09

    Following Program will read and traverse over the TTL file

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import org.apache.jena.graph.Triple ;
    import org.apache.jena.riot.RDFDataMgr ;
    import org.apache.jena.riot.lang.PipedRDFIterator;
    import org.apache.jena.riot.lang.PipedRDFStream;
    import org.apache.jena.riot.lang.PipedTriplesStream;
    
    public class ReadingTTL
    {
        public static void main(String... argv) {
            final String filename = "yagoTransitiveType2.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() {
                    // Call the parsing process.
                    RDFDataMgr.parse(inputStream, filename);
                }
            };
    
            // Start the parser on another thread
            executor.submit(parser);
    
            // We will consume the input on the main thread here
    
            // We can now iterate over data as it is parsed, parsing only runs as
            // far ahead of our consumption as the buffer size allows
            while (iter.hasNext()) {
                Triple next = iter.next();
                // Do something with each triple
                System.out.println("Subject:  "+next.getSubject());
                System.out.println("Object:  "+next.getObject());
                System.out.println("Predicate:  "+next.getPredicate());
                System.out.println("\n");
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-12 12:22

    The read method you are using assumes that the input format is RDF/XML.

    you need to use one of the other read methods.

    So it would be:

    public static void main(String[] args) throws IOException {
        Model model=ModelFactory.createDefaultModel();
        model.read(new FileInputStream("simpsons.ttl"),null,"TTL");
    }
    
    0 讨论(0)
提交回复
热议问题