| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| |
| <title>Apache Jena - Reading RDF in Apache Jena</title> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <link href="/css/bootstrap.min.css" rel="stylesheet" media="screen"> |
| <link href="/css/bootstrap-icons.css" rel="stylesheet" media="screen"><link rel="stylesheet" type="text/css" href="https://jena.apache.org/sass/jena.1b17c39a117e22b46db4c66f6395dc27c134a60377d87d2d5745b8600eb69722.css" integrity="sha256-GxfDmhF+IrRttMZvY5XcJ8E0pgN32H0tV0W4YA62lyI="> |
| <link rel="shortcut icon" href="/images/favicon.ico" /> |
| |
| </head> |
| |
| <body> |
| |
| <nav class="navbar navbar-expand-lg bg-body-tertiary" role="navigation"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
| <span class="navbar-toggler-icon"></span> |
| </button> |
| <a class="navbar-brand" href="/index.html"> |
| <img class="logo-menu" src="/images/jena-logo/jena-logo-notext-small.png" alt="jena logo">Apache Jena</a> |
| </div> |
| |
| <div class="collapse navbar-collapse" id="navbarNav"> |
| <ul class="navbar-nav me-auto mb-2 mb-lg-0"> |
| <li id="homepage" class="nav-item"><a class="nav-link" href="/index.html"><span class="bi-house"></span> Home</a></li> |
| <li id="download" class="nav-item"><a class="nav-link" href="/download/index.cgi"><span class="bi-download"></span> Download</a></li> |
| <li class="nav-item dropdown"> |
| <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal"></span> Learn <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li class="dropdown-header">Tutorials</li> |
| <li><a class="dropdown-item" href="/tutorials/index.html">Overview</a></li> |
| <li><a class="dropdown-item" href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li> |
| <li><a class="dropdown-item" href="/documentation/notes/index.html">How-To's</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li> |
| <li><a class="dropdown-item" href="/tutorials/rdf_api.html">RDF core API tutorial</a></li> |
| <li><a class="dropdown-item" href="/tutorials/sparql.html">SPARQL tutorial</a></li> |
| <li><a class="dropdown-item" href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">References</li> |
| <li><a class="dropdown-item" href="/documentation/index.html">Overview</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/index.html">ARQ (SPARQL)</a></li> |
| <li><a class="dropdown-item" href="/documentation/io/">RDF I/O</a></li> |
| <li><a class="dropdown-item" href="/documentation/assembler/index.html">Assembler</a></li> |
| <li><a class="dropdown-item" href="/documentation/tools/index.html">Command-line tools</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfs/">Data with RDFS Inferencing</a></li> |
| <li><a class="dropdown-item" href="/documentation/geosparql/index.html">GeoSPARQL</a></li> |
| <li><a class="dropdown-item" href="/documentation/inference/index.html">Inference API</a></li> |
| <li><a class="dropdown-item" href="/documentation/ontology/">Ontology API</a></li> |
| <li><a class="dropdown-item" href="/documentation/permissions/index.html">Permissions</a></li> |
| <li><a class="dropdown-item" href="/documentation/extras/querybuilder/index.html">Query Builder</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdf/index.html">RDF API</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li> |
| <li><a class="dropdown-item" href="/documentation/rdfstar/index.html">RDF-star</a></li> |
| <li><a class="dropdown-item" href="/documentation/shacl/index.html">SHACL</a></li> |
| <li><a class="dropdown-item" href="/documentation/shex/index.html">ShEx</a></li> |
| <li><a class="dropdown-item" href="/documentation/tdb/index.html">TDB</a></li> |
| <li><a class="dropdown-item" href="/documentation/tdb2/index.html">TDB2</a></li> |
| <li><a class="dropdown-item" href="/documentation/query/text-query.html">Text Search</a></li> |
| </ul> |
| </li> |
| |
| <li class="nav-item dropdown"> |
| <a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal-code"></span> Javadoc <b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a class="dropdown-item" href="/documentation/javadoc.html">All Javadoc</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/arq/">ARQ</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/fuseki2/">Fuseki</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/jena/">Jena Core</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/permissions/">Permissions</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/shacl/">SHACL</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/tdb/">TDB</a></li> |
| <li><a class="dropdown-item" href="/documentation/javadoc/text/">Text Search</a></li> |
| </ul> |
| </li> |
| </ul> |
| <form class="d-flex" role="search" action="/search" method="GET"> |
| <div class="input-group"> |
| <input class="form-control border-end-0 border m-0" type="search" name="q" id="search-query" placeholder="Search...." aria-label="Search" style="width: 10rem;"> |
| <button class="btn btn-outline-secondary border-start-0 border" type="submit"> |
| <i class="bi-search"></i> |
| </button> |
| </div> |
| </form> |
| <ul class="navbar-nav"> |
| <li id="ask" class="nav-item"><a class="nav-link" href="/help_and_support/index.html" title="Ask"><span class="bi-patch-question"></span><span class="text-body d-none d-xxl-inline"> Ask</span></a></li> |
| |
| <li class="nav-item dropdown"> |
| <a href="#" title="Get involved" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-megaphone"></span><span class="text-body d-none d-xxl-inline"> Get involved </span><b class="caret"></b></a> |
| <ul class="dropdown-menu"> |
| <li><a class="dropdown-item" href="/getting_involved/index.html">Contribute</a></li> |
| <li><a class="dropdown-item" href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">Project</li> |
| <li><a class="dropdown-item" href="/about_jena/about.html">About Jena</a></li> |
| <li><a class="dropdown-item" href="/about_jena/architecture.html">Architecture</a></li> |
| <li><a class="dropdown-item" href="/about_jena/citing.html">Citing</a></li> |
| <li><a class="dropdown-item" href="/about_jena/team.html">Project team</a></li> |
| <li><a class="dropdown-item" href="/about_jena/contributions.html">Related projects</a></li> |
| <li><a class="dropdown-item" href="/about_jena/roadmap.html">Roadmap</a></li> |
| <li><a class="dropdown-item" href="/about_jena/security-advisories.html">Security Advisories</a></li> |
| <li class="dropdown-divider"></li> |
| <li class="dropdown-header">ASF</li> |
| <li><a class="dropdown-item" href="https://www.apache.org/">Apache Software Foundation</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/licenses/LICENSE-2.0">License</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/security/">Security</a></li> |
| <li><a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| </ul> |
| </li> |
| |
| |
| |
| |
| <li class="nav-item" id="edit"><a class="nav-link" href="https://github.com/apache/jena-site/edit/main/source/documentation/io/rdf-input.md" title="Edit this page on GitHub"><span class="bi-pencil-square"></span><span class="text-body d-none d-xxl-inline"> Edit this page</span></a></li> |
| </ul> |
| </div> |
| </div> |
| </nav> |
| |
| <div class="container"> |
| <div class="row"> |
| <div class="col-md-12"> |
| |
| <div id="breadcrumbs"> |
|
|
|
|
|
|
|
|
|
|
|
|
| <ol class="breadcrumb mt-4 p-2 bg-body-tertiary">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="breadcrumb-item"><a href='/documentation'>DOCUMENTATION</a></li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="breadcrumb-item"><a href='/documentation/io'>IO</a></li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| <li class="breadcrumb-item active">RDF INPUT</li>
|
|
|
|
|
|
|
|
|
| </ol>
|
|
|
|
|
|
|
| |
| </div> |
| <h1 class="title">Reading RDF in Apache Jena</h1> |
| |
| |
| <main class="d-flex flex-xl-row flex-column"> |
| |
| <aside class="text-muted align-self-start mb-3 p-0 d-xl-none d-block"> |
| <h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2> |
| <nav id="TableOfContents"> |
| <ul> |
| <li><a href="#api">API</a> |
| <ul> |
| <li><a href="#determining-the-rdf-syntax">Determining the RDF syntax</a></li> |
| <li><a href="#using-rdfdatamgr">Example 1 : Using the RDFDataMgr</a></li> |
| <li><a href="#model-usage">Example 2 : Common usage</a></li> |
| <li><a href="#using-rdfparser">Example 3 : Using RDFParser</a></li> |
| </ul> |
| </li> |
| <li><a href="#logging">Logging</a></li> |
| <li><a href="#streammanager-and-locationmapper">StreamManager and LocationMapper</a> |
| <ul> |
| <li><a href="#configuring-a-streammanager">Configuring a <code>StreamManager</code></a></li> |
| <li><a href="#configuring-a-locationmapper">Configuring a <code>LocationMapper</code></a></li> |
| </ul> |
| </li> |
| <li><a href="#advanced-examples">Advanced examples</a> |
| <ul> |
| <li><a href="#iterating-over-parser-output">Iterating over parser output</a></li> |
| <li><a href="#filter-the-output-of-parsing">Filter the output of parsing</a></li> |
| <li><a href="#add-a-new-language">Add a new language</a></li> |
| </ul> |
| </li> |
| </ul> |
| </nav> |
| </aside> |
| <article class="flex-column me-lg-4"> |
| <p>This page details the setup of RDF I/O technology (RIOT) for Apache Jena.</p> |
| <p>See <a href="rdf-output.html">Writing RDF</a> for details of the RIOT Writer system.</p> |
| <ul> |
| <li><a href="#api">API</a> |
| <ul> |
| <li><a href="#determining-the-rdf-syntax">Determining the RDF syntax</a></li> |
| <li><a href="#using-rdfdatamgr">Example 1 : Using the RDFDataMgr</a></li> |
| <li><a href="#model-usage">Example 2 : Model usage</a></li> |
| <li><a href="#using-rdfparser">Example 3 : Using RDFParser</a></li> |
| </ul> |
| </li> |
| <li><a href="#logging">Logging</a></li> |
| <li><a href="#streammanager-and-locationmapper">The StreamManager and LocationMapper</a> |
| <ul> |
| <li><a href="#configuring-a-streammanager">Configuring a <code>StreamManager</code></a></li> |
| <li><a href="#configuring-a-locationmapper">Configuring a <code>LocationMapper</code></a></li> |
| </ul> |
| </li> |
| <li><a href="#advanced-examples">Advanced examples</a> |
| <ul> |
| <li><a href="#iterating-over-parser-output">Iterating over parser output</a></li> |
| <li><a href="#filter-the-output-of-parsing">Filtering the output of parsing</a></li> |
| <li><a href="#add-a-new-language">Add a new language</a></li> |
| </ul> |
| </li> |
| </ul> |
| <p>Full details of operations are given in the javadoc.</p> |
| <h2 id="api">API</h2> |
| <p>Much of the functionality is accessed via the Jena Model API; direct |
| calling of the RIOT subsystem isn’t needed. A resource name |
| with no URI scheme is assumed to be a local file name.</p> |
| <p>Applications typically use at most <code>RDFDataMgr</code> to read RDF datasets.</p> |
| <p>The major classes in the RIOT API are:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Class</th> |
| <th>Comment</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>RDFDataMgr</td> |
| <td>Main set of functions to read and load models and datasets</td> |
| </tr> |
| <tr> |
| <td>StreamRDF</td> |
| <td>Interface for the output of all parsers</td> |
| </tr> |
| <tr> |
| <td>RDFParser</td> |
| <td>Detailed setup of a parser</td> |
| </tr> |
| <tr> |
| <td>StreamManager</td> |
| <td>Handles the opening of typed input streams</td> |
| </tr> |
| <tr> |
| <td>RDFLanguages</td> |
| <td>Registered languages</td> |
| </tr> |
| <tr> |
| <td>RDFParserRegistry</td> |
| <td>Registered parser factories</td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="determining-the-rdf-syntax">Determining the RDF syntax</h3> |
| <p>The syntax of the RDF file is determined by the content type (if an HTTP |
| request), then the file extension if there is no content type. Content type |
| <code>text/plain</code> is ignored; it is assumed to be type returned for an unconfigured |
| http server. The application can also pass in a declared language hint.</p> |
| <p>The string name traditionally used in <code>model.read</code> is mapped to RIOT <code>Lang</code> |
| as:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Jena reader</th> |
| <th>RIOT Lang</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td><code>"TURTLE"</code></td> |
| <td><code>TURTLE</code></td> |
| </tr> |
| <tr> |
| <td><code>"TTL"</code></td> |
| <td><code>TURTLE</code></td> |
| </tr> |
| <tr> |
| <td><code>"Turtle"</code></td> |
| <td><code>TURTLE</code></td> |
| </tr> |
| <tr> |
| <td><code>"N-TRIPLES"</code></td> |
| <td><code>NTRIPLES</code></td> |
| </tr> |
| <tr> |
| <td><code>"N-TRIPLE"</code></td> |
| <td><code>NTRIPLES</code></td> |
| </tr> |
| <tr> |
| <td><code>"NT"</code></td> |
| <td><code>NTRIPLES</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/XML"</code></td> |
| <td><code>RDFXML</code></td> |
| </tr> |
| <tr> |
| <td><code>"N3"</code></td> |
| <td><code>N3</code></td> |
| </tr> |
| <tr> |
| <td><code>"JSON-LD"</code></td> |
| <td><code>JSONLD</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/JSON"</code></td> |
| <td><code>RDFJSON</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/JSON"</code></td> |
| <td><code>RDFJSON</code></td> |
| </tr> |
| </tbody> |
| </table> |
| <p>The following is a suggested Apache httpd .htaccess file:</p> |
| <pre><code>AddType text/turtle .ttl |
| AddType application/rdf+xml .rdf |
| AddType application/n-triples .nt |
| |
| AddType application/ld+json .jsonld |
| |
| AddType text/trig .trig |
| AddType application/n-quads .nq |
| |
| AddType application/trix+xml .trix |
| AddType application/rdf+thrift .rt |
| AddType application/rdf+protobuf .rpb |
| </code></pre> |
| <h3 id="using-rdfdatamgr">Example 1 : Using the RDFDataMgr</h3> |
| <p><code>RDFDataMgr</code> provides operations to load, read and write models and datasets.</p> |
| <p><code>RDFDataMgr</code> “load” operations create an |
| in-memory container (model, or dataset as appropriate); “read” operations |
| add data into an existing model or dataset.</p> |
| <pre><code>// Create a model and read into it from file |
| // "data.ttl" assumed to be Turtle. |
| Model model = RDFDataMgr.loadModel("data.ttl") ; |
| |
| // Create a dataset and read into it from file |
| // "data.trig" assumed to be TriG. |
| Dataset dataset = RDFDataMgr.loadDataset("data.trig") ; |
| |
| // Read into an existing Model |
| RDFDataMgr.read(model, "data2.ttl") ; |
| </code></pre> |
| <h3 id="model-usage">Example 2 : Common usage</h3> |
| <p>The original Jena Model API operation for <code>read</code> and <code>write</code> provide another way to the same machinery:</p> |
| <pre><code>Model model = ModelFactory.createDefaultModel() ; |
| model.read("data.ttl") ; |
| </code></pre> |
| <p>If the syntax is not as the file extension, a language can be declared:</p> |
| <pre><code>model.read("data.foo", "TURTLE") ; |
| </code></pre> |
| <h3 id="using-rdfparser">Example 3 : Using RDFParser</h3> |
| <p>Detailed control over the setup of the parsing process is provided by |
| <code>RDFParser</code> which provides a builder pattern. It has many options - see |
| <a href="/documentation/javadoc/arq/org.apache.jena.arq/org/apache/jena/riot/RDFParser.html">the javadoc for all details</a>.</p> |
| <p>For example, to read Trig data, and set the error handler specially,</p> |
| <pre><code> Dataset dataset; |
| // The parsers will do the necessary character set conversion. |
| try (InputStream in = new FileInputStream("data.some.unusual.extension")) { |
| dataset = |
| RDFParser.create() |
| .source(in) |
| .lang(RDFLanguages.TRIG) |
| .errorHandler(ErrorHandlerFactory.errorHandlerStrict) |
| .base("http://example/base") |
| .toDataset(noWhere); |
| } |
| </code></pre> |
| <h2 id="logging">Logging</h2> |
| <p>The parsers log to a logger called <code>org.apache.jena.riot</code>. To avoid <code>WARN</code> |
| messages, set this to <code>ERROR</code> in the logging system of the application.</p> |
| <h2 id="streammanager-and-locationmapper">StreamManager and LocationMapper</h2> |
| <p>Operations to read RDF data can be redirected to local copies and to other URLs. |
| This is useful to provide local copies of remote resources.</p> |
| <p>By default, the <code>RDFDataMgr</code> uses the global <code>StreamManager</code> to open typed |
| InputStreams. The <code>StreamManager</code> can be set using the <code>RDFParser</code> builder:</p> |
| <pre><code> // Create a copy of the global default StreamManager. |
| StreamManager sm = StreamManager.get().clone(); |
| // Add directory "/tmp" as a place to look for files |
| sm.addLocator(new LocatorFile("/tmp")); |
| |
| RDFParser.create() |
| .streamManager(sm) |
| .source("data.ttl") |
| .parse(...); |
| </code></pre> |
| <p>It can also be set in a <code>Context</code> object given the the RDFParser for the |
| operation, but normally this defaults to the global <code>Context</code> available via |
| <code>Context.get()</code>. The constant <code>SysRIOT.sysStreamManager</code>, which is |
| <code>http://jena.apache.org/riot/streamManager</code>, is used.</p> |
| <p>Specialized StreamManagers can be configured with specific locators for |
| data:</p> |
| <ul> |
| <li>File locator (with own current directory)</li> |
| <li>URL locator</li> |
| <li>Class loader locator</li> |
| <li>Zip file locator</li> |
| </ul> |
| <h3 id="configuring-a-streammanager">Configuring a <code>StreamManager</code></h3> |
| <p>The <code>StreamManager</code> can be reconfigured with different places to look for |
| files. The default configuration used for the global <code>StreamManager</code> is |
| a file access class, where the current directory is that of the java |
| process, a URL accessor for reading from the web, and a |
| class loader-based accessor. Different setups can be built and used |
| either as the global set up, or on a per request basis.</p> |
| <p>There is also a <code>LocationMapper</code> for rewriting file names and URLs before |
| use to allow placing known names in different places (e.g. having local |
| copies of import http resources).</p> |
| <h3 id="configuring-a-locationmapper">Configuring a <code>LocationMapper</code></h3> |
| <p>Location mapping files are RDF, usually written in Turtle although |
| an RDF syntax can be used.</p> |
| <pre><code>PREFIX lm: <http://jena.hpl.hp.com/2004/08/location-mapping#> |
| |
| [] lm:mapping |
| [ lm:name "file:foo.ttl" ; lm:altName "file:etc/foo.ttl" ] , |
| [ lm:prefix "file:etc/" ; lm:altPrefix "file:ETC/" ] , |
| [ lm:name "file:etc/foo.ttl" ; lm:altName "file:DIR/foo.ttl" ] |
| . |
| </code></pre> |
| <p>There are two types of location mapping: exact match renaming and |
| prefix renaming. When trying to find an alternative location, a |
| <code>LocationMapper</code> first tries for an exact match; if none is found, |
| the LocationMapper will search for the longest matching prefix. If |
| two are the same length, there is no guarantee on order tried; |
| there is no implied order in a location mapper configuration file |
| (it sets up two hash tables).</p> |
| <p>In the example above, <code>file:etc/foo.ttl</code> becomes <code>file:DIR/foo.ttl</code> |
| because that is an exact match. The prefix match of file:/etc/ is |
| ignored.</p> |
| <p>All string tests are done case sensitively because the primary use |
| is for URLs.</p> |
| <p>Notes:</p> |
| <ul> |
| <li>Property values are not URIs, but strings. This is a system |
| feature, not an RDF feature. Prefix mapping is name rewriting; |
| alternate names are not treated as equivalent resources in the rest |
| of Jena. While application writers are encouraged to use URIs to |
| identify files, this is not always possible.</li> |
| <li>There is no check to see if the alternative system resource is |
| equivalent to the original.</li> |
| </ul> |
| <p>A LocationMapper finds its configuration file by looking for the |
| following files, in order:</p> |
| <ul> |
| <li><code>file:location-mapping.rdf</code></li> |
| <li><code>file:location-mapping.ttl</code></li> |
| <li><code>file:etc/location-mapping.rdf</code></li> |
| <li><code>file:etc/location-mapping.ttl</code></li> |
| </ul> |
| <p>This is a specified as a path - note the path separator is always |
| the character ‘;’ regardless of operating system because URLs |
| contain ‘:’.</p> |
| <p>Applications can also set mappings programmatically. No |
| configuration file is necessary.</p> |
| <p>The base URI for reading models will be the original URI, not the alternative location.</p> |
| <h2 id="advanced-examples">Advanced examples</h2> |
| <p>Example code may be found in <a href="https://github.com/apache/jena/tree/main/jena-examples/src/main/java/arq/examples/riot/">jena-examples:arq/examples</a>.</p> |
| <h3 id="iterating-over-parser-output">Iterating over parser output</h3> |
| <p>One of the capabilities of the RIOT API is the ability to treat parser output as an iterator, |
| this is useful when you don’t want to go to the trouble of writing a full sink implementation and can easily express your |
| logic in normal iterator style.</p> |
| <p>To do this you use <code>AsyncParser.asyncParseTriples</code> which parses the input on |
| another thread:</p> |
| <pre><code> IteratorCloseable<Triple> iter = AsyncParser.asyncParseTriples(filename); |
| iter.forEachRemaining(triple->{ |
| // Do something with triple |
| }); |
| </code></pre> |
| <p>Calling the iterator’s close method stops parsing and closes the involved resources. |
| For N-Triples and N-Quads, you can use |
| <code>RiotParsers.createIteratorNTriples(input)</code> which parses the input on the |
| calling thread.</p> |
| <p><a href="https://github.com/apache/jena/blob/main/jena-examples/src/main/java/arq/examples/riot/ExRIOT9_AsyncParser.java">RIOT example 9</a>.</p> |
| <p>Additional control over parsing is provided by the <code>AsyncParser.of(...)</code> methods which return <code>AsyncParserBuilder</code> instances. |
| The builder features a fluent API that allows for fine-tuning internal buffer sizes as well as eventually obtaining |
| a standard Java <code>Stream</code>. Calling the stream’s close method stops parsing and closes the involved resources. |
| Therefore, these streams are best used in conjunction with try-with-resources blocks:</p> |
| <pre><code> try (Stream<Triple> stream = AsyncParser.of(filename) |
| .setQueueSize(2).setChunkSize(100).streamTriples().limit(1000)) { |
| // Do something with the stream |
| } |
| </code></pre> |
| <p>The AsyncParser also supports parsing RDF into a stream of <code>EltStreamRDF</code> elements. Each element can hold a triple, quad, prefix, base IRI or exception. |
| For all <code>Stream</code>-based methods there also exist <code>Iterator</code>-based versions:</p> |
| <pre><code> IteratorCloseable<EltStreamRDF> it = AsyncParser.of(filename).asyncParseElements(); |
| try { |
| while (it.hasNext()) { |
| EltStreamRDF elt = it.next(); |
| if (elt.isTriple()) { |
| // Do something with elt.getTriple(); |
| } else if (elt.isPrefix()) { |
| // Do something with elt.getPrefix() and elt.getIri(); |
| } |
| } |
| } finally { |
| Iter.close(it); |
| } |
| </code></pre> |
| <h3 id="filter-the-output-of-parsing">Filter the output of parsing</h3> |
| <p>When working with very large files, it can be useful to |
| process the stream of triples or quads produced |
| by the parser so as to work in a streaming fashion.</p> |
| <p>See <a href="https://github.com/apache/jena/blob/main/jena-examples/src/main/java/arq/examples/riot/ExRIOT4_StreamRDF_Filter.java">RIOT example 4</a></p> |
| <h3 id="add-a-new-language">Add a new language</h3> |
| <p>The set of languages is not fixed. A new language, |
| together with a parser, can be added to RIOT as shown in |
| <a href="https://github.com/apache/jena/blob/main/jena-examples/src/main/java/arq/examples/riot/ExRIOT6_AddNewReader.java">RIOT example 6</a></p> |
| |
| </article> |
| |
| <aside class="text-muted align-self-start mb-3 mb-xl-5 p-0 d-none d-xl-flex flex-column sticky-top"> |
| <h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2> |
| <nav id="TableOfContents"> |
| <ul> |
| <li><a href="#api">API</a> |
| <ul> |
| <li><a href="#determining-the-rdf-syntax">Determining the RDF syntax</a></li> |
| <li><a href="#using-rdfdatamgr">Example 1 : Using the RDFDataMgr</a></li> |
| <li><a href="#model-usage">Example 2 : Common usage</a></li> |
| <li><a href="#using-rdfparser">Example 3 : Using RDFParser</a></li> |
| </ul> |
| </li> |
| <li><a href="#logging">Logging</a></li> |
| <li><a href="#streammanager-and-locationmapper">StreamManager and LocationMapper</a> |
| <ul> |
| <li><a href="#configuring-a-streammanager">Configuring a <code>StreamManager</code></a></li> |
| <li><a href="#configuring-a-locationmapper">Configuring a <code>LocationMapper</code></a></li> |
| </ul> |
| </li> |
| <li><a href="#advanced-examples">Advanced examples</a> |
| <ul> |
| <li><a href="#iterating-over-parser-output">Iterating over parser output</a></li> |
| <li><a href="#filter-the-output-of-parsing">Filter the output of parsing</a></li> |
| <li><a href="#add-a-new-language">Add a new language</a></li> |
| </ul> |
| </li> |
| </ul> |
| </nav> |
| </aside> |
| </main> |
| |
| </div> |
| </div> |
| </div> |
| |
| <footer class="bd-footer py-4 py-md-5 mt-4 mt-lg-5 bg-body-tertiary"> |
| <div class="container" style="font-size:80%" > |
| <p> |
| Copyright © 2011–2024 The Apache Software Foundation, Licensed under the |
| <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>. |
| </p> |
| <p> |
| Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of |
| The Apache Software Foundation. |
| <br/> |
| <a href="https://privacy.apache.org/policies/privacy-policy-public.html" |
| >Apache Software Foundation Privacy Policy</a>. |
| </p> |
| </div> |
| </footer> |
| |
| <script src="/js/popper.min.js.js" type="text/javascript"></script> |
| <script src="/js/bootstrap.min.js" type="text/javascript"></script> |
| <script src="/js/improve.js" type="text/javascript"></script> |
| |
| <script type="text/javascript"> |
| (function() { |
| 'use strict' |
| |
| |
| |
| const links = document.querySelectorAll(`a[href="${window.location.pathname}"]`) |
| if (links !== undefined && links !== null) { |
| for (const link of links) { |
| |
| link.classList.add('active') |
| let parentElement = link.parentElement |
| let count = 0 |
| const levelsLimit = 4 |
| |
| |
| |
| |
| |
| while (['UL', 'LI'].includes(parentElement.tagName) && count <= levelsLimit) { |
| if (parentElement.tagName === 'LI') { |
| |
| |
| |
| parentElement.querySelector('a:first-child').classList.add('active') |
| } |
| parentElement = parentElement.parentElement |
| count++ |
| } |
| } |
| } |
| })() |
| </script> |
| |
| </body> |
| </html> |