| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| |
| |
| <title>Apache Jena - Writing 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-output.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 OUTPUT</li>
|
|
|
|
|
|
|
|
|
| </ol>
|
|
|
|
|
|
|
| |
| </div> |
| <h1 class="title">Writing 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></li> |
| <li><a href="#rdfformat"><code>RDFFormat</code></a></li> |
| <li><a href="#jena_model_write_formats"><code>RDFFormat</code>s and Jena syntax names</a></li> |
| <li><a href="#formats">Formats</a> |
| <ul> |
| <li><a href="#normal-printing">Normal Printing</a></li> |
| <li><a href="#pretty-printed-languages">Pretty Printed Languages</a></li> |
| <li><a href="#streamed-block-formats">Streamed Block Formats</a></li> |
| <li><a href="#line-printed-formats">Line printed formats</a></li> |
| <li><a href="#opt-turtle-trig">Turtle and Trig format options</a> |
| <ul> |
| <li><a href="#format-option-usage">Format Option Usage</a></li> |
| </ul> |
| </li> |
| <li><a href="#n-triples-and-n-quads">N-Triples and N-Quads</a></li> |
| <li><a href="#json-ld">JSON-LD</a></li> |
| <li><a href="#rdf-binary">RDF Binary</a></li> |
| <li><a href="#rdfxml">RDF/XML</a></li> |
| </ul> |
| </li> |
| <li><a href="#examples">Examples</a> |
| <ul> |
| <li><a href="#ways-to-write-a-model">Ways to write a model</a></li> |
| <li><a href="#ways-to-write-a-dataset">Ways to write a dataset</a></li> |
| <li><a href="#adding-a-new-output-format">Adding a new output format</a></li> |
| </ul> |
| </li> |
| <li><a href="#notes">Notes</a></li> |
| </ul> |
| </nav> |
| </aside> |
| <article class="flex-column me-lg-4"> |
| <p>This page describes the RIOT (RDF I/O technology) output capabilities.</p> |
| <p>See <a href="rdf-input.html">Reading RDF</a> for details of the RIOT Reader system.</p> |
| <ul> |
| <li><a href="#api">API</a></li> |
| <li><a href="#rdfformat"><code>RDFFormat</code></a></li> |
| <li><a href="#jena_model_write_formats"><code>RDFFormat</code>s and Jena syntax names</a></li> |
| <li><a href="#formats">Formats</a> |
| <ul> |
| <li><a href="#normal-printing">Normal Printing</a></li> |
| <li><a href="#pretty-printed-languages">Pretty Printed Languages</a></li> |
| <li><a href="#streamed-block-formats">Streamed Block Formats</a></li> |
| <li><a href="#line-printed-formats">Line printed formats</a></li> |
| <li><a href="#opt-turtle-trig">Turtle and Trig format options</a></li> |
| <li><a href="#n-triples-and-n-quads">N-Triples and N-Quads</a></li> |
| <li><a href="#json-ld">JSON-LD</a></li> |
| <li><a href="#rdf-binary">RDF Binary</a></li> |
| <li><a href="#rdfxml">RDF/XML</a></li> |
| </ul> |
| </li> |
| <li><a href="#examples">Examples</a></li> |
| <li><a href="#notes">Notes</a></li> |
| </ul> |
| <h2 id="api">API</h2> |
| <p>There are two ways to write RDF data using Apache Jena RIOT, |
| either via the <code>RDFDataMgr</code></p> |
| <pre><code>RDFDataMgr.write(OutputStream, Model, Lang) ; |
| RDFDataMgr.write(OutputStream, Dataset, Lang) ; |
| RDFDataMgr.write(OutputStream, Model, RDFFormat) ; |
| RDFDataMgr.write(OutputStream, Dataset, RDFFormat) ; |
| </code></pre> |
| <p>or the legacy way using the <code>model</code> API, where there is a limited set of <code>"format"</code> names</p> |
| <pre><code>model.write(output, "format") ; |
| </code></pre> |
| <p>The <em><code>format</code></em> names are <a href="#jena_model_write_formats">described below</a>; they include the |
| names Jena has supported before RIOT.</p> |
| <p>Many variations of these methods exist. See the full javadoc for details.</p> |
| <h2 id="rdfformat"><code>RDFFormat</code></h2> |
| <p>Output using RIOT depends on the format, which involves both the language (syntax) |
| being written and the variant of that syntax.</p> |
| <p>The RIOT writer architecture is extensible. The following languages |
| are available as part of the standard setup.</p> |
| <ul> |
| <li>Turtle</li> |
| <li>N-Triples</li> |
| <li>NQuads</li> |
| <li>TriG</li> |
| <li>JSON-LD</li> |
| <li>RDF/XML</li> |
| <li>RDF/JSON</li> |
| <li>TriX</li> |
| <li>RDF Binary</li> |
| </ul> |
| <p>In addition, there are variants of Turtle, TriG for pretty printing, |
| streamed output and flat output. RDF/XML has variants for pretty printing |
| and plain output. Jena RIOT uses <code>org.apache.jena.riot.RDFFormat</code> as a way |
| to identify the language and variant to be written. The class contains constants |
| for the standard supported formats.</p> |
| <p>Note:</p> |
| <ul> |
| <li>RDF/JSON is not JSON-LD. See the <a href="rdf-json.html">description of RDF/JSON</a>.</li> |
| <li>N3 is treated as Turtle for output.</li> |
| </ul> |
| <h2 id="jena_model_write_formats"><code>RDFFormat</code>s and Jena syntax names</h2> |
| <p>The string name traditionally used in <code>model.write</code> is mapped to RIOT <code>RDFFormat</code> |
| as follows:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Jena writer name</th> |
| <th>RIOT RDFFormat</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>"JSON-LD"</code></td> |
| <td><code>JSONLD</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/XML-ABBREV"</code></td> |
| <td><code>RDFXML</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/XML"</code></td> |
| <td><code>RDFXML_PLAIN</code></td> |
| </tr> |
| <tr> |
| <td><code>"N3"</code></td> |
| <td><code>N3</code></td> |
| </tr> |
| <tr> |
| <td><code>"RDF/JSON"</code></td> |
| <td><code>RDFJSON</code></td> |
| </tr> |
| </tbody> |
| </table> |
| <h2 id="formats">Formats</h2> |
| <h3 id="normal-printing">Normal Printing</h3> |
| <p>A <code>Lang</code> can be used for the writer format, in which case it is mapped to |
| an <code>RDFFormat</code> internally. The normal writers are:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat or Lang</th> |
| <th>Default</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>TURTLE</td> |
| <td>Turtle, pretty printed</td> |
| </tr> |
| <tr> |
| <td>TTL</td> |
| <td>Turtle, pretty printed</td> |
| </tr> |
| <tr> |
| <td>NTRIPLES</td> |
| <td>N-Triples, UTF-8</td> |
| </tr> |
| <tr> |
| <td>TRIG</td> |
| <td>TriG, pretty printed</td> |
| </tr> |
| <tr> |
| <td>NQUADS</td> |
| <td>N-Quads, UTF-8</td> |
| </tr> |
| <tr> |
| <td>JSONLD</td> |
| <td>JSON-LD, pretty printed</td> |
| </tr> |
| <tr> |
| <td>RDFXML</td> |
| <td>RDF/XML, pretty printed</td> |
| </tr> |
| <tr> |
| <td>RDFJSON</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>TRIX</td> |
| <td></td> |
| </tr> |
| <tr> |
| <td>RDFTHRFT</td> |
| <td>RDF Binary Thrift</td> |
| </tr> |
| <tr> |
| <td>RDFPROTO</td> |
| <td>RDF Binary Protobuf</td> |
| </tr> |
| </tbody> |
| </table> |
| <p>Pretty printed RDF/XML is also known as RDF/XML-ABBREV.</p> |
| <h3 id="pretty-printed-languages">Pretty Printed Languages</h3> |
| <p>All Turtle and TriG formats use |
| prefix names, and short forms for literals.</p> |
| <p>The pretty printed versions of Turtle and TriG prints |
| data with the same subject in the same graph together. |
| All the properties for a given subject are sorted |
| into a predefined order. RDF lists are printed as |
| <code>(...)</code> and <code>[...]</code> is used for blank nodes where possible.</p> |
| <p>The analysis for determining what can be pretty printed requires |
| temporary datastructures and also a scan of the whole graph before |
| writing begins. Therefore, pretty printed formats are not suitable |
| for writing persistent graphs and datasets.</p> |
| <p>When writing at scale use either a “blocked” version of Turtle or TriG, |
| or write N-triples/N-Quads.</p> |
| <p>Example:</p> |
| <pre><code>PREFIX : <http://example/> |
| PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| |
| :book dc:author ( :a :b ) . |
| |
| :a a foaf:Person ; |
| foaf:knows [ foaf:name "Bob" ] ; |
| foaf:name "Alice" . |
| |
| :b foaf:knows :a . |
| </code></pre> |
| <p>The default pretty printed output (shown above) aligns predicates |
| and objects, which can result in wide lines. For a narrower |
| indentation style, set <code>ttl:indentStyle</code> to <code>long</code>. |
| See <a href="#opt-turtle-trig">Turtle and Trig format options</a>.</p> |
| <p>Pretty printed formats:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| <th>Same as</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>TURTLE_PRETTY</td> |
| <td>TURTLE, TTL</td> |
| </tr> |
| <tr> |
| <td>TRIG_PRETTY</td> |
| <td>TRIG</td> |
| </tr> |
| <tr> |
| <td>RDFXML_PRETTY</td> |
| <td>RDFXML_ABBREV, RDFXML</td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="streamed-block-formats">Streamed Block Formats</h3> |
| <p>Fully pretty printed formats can not be streamed. They require analysis |
| of all of the data to be written in order to choose the short forms. This limits |
| their use in fully scalable applications.</p> |
| <p>Some formats can be written streaming style, where the triples or quads |
| are partially grouped together by adjacent subject or graph/subject |
| in the output stream.</p> |
| <p>The written data is like the pretty printed forms of Turtle or TriG, |
| but without RDF lists being written in the ‘(…)’ form, without |
| using <code>[...]</code> for blank nodes.</p> |
| <p>This gives some degree of readability while not requiring |
| excessive temporary datastructure. Arbitrary amounts of data |
| can be written but blank node labels need to be tracked in order |
| to use the short label form.</p> |
| <p>Example:</p> |
| <pre><code>PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| PREFIX : <http://example/> |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| |
| :book dc:author _:b0 . |
| |
| _:b0 rdf:rest _:b1 ; |
| rdf:first :a . |
| |
| :a foaf:knows _:b2 ; |
| foaf:name "Alice" ; |
| rdf:type foaf:Person . |
| |
| _:b2 foaf:name "Bob" . |
| |
| :b foaf:knows :a . |
| |
| _:b1 rdf:rest rdf:nil ; |
| rdf:first :b . |
| </code></pre> |
| <p>Formats:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>TURTLE_BLOCKS</td> |
| </tr> |
| <tr> |
| <td>TRIG_BLOCKS</td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="line-printed-formats">Line printed formats</h3> |
| <p>There are writers for Turtle and Trig that use the abbreviated formats for |
| prefix names and short forms for literals. They write each triple or quad |
| on a single line.</p> |
| <p>The regularity of the output can be useful for text processing of data.<br> |
| These formats do not offer more scalability than the stream forms.</p> |
| <p>Example:</p> |
| <p>The FLAT writers abbreviates IRIs, literals and blank node labels |
| but always writes one complete triple on one line (no use of <code>;</code>).</p> |
| <pre><code>PREFIX : <http://example/> |
| PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| _:b0 foaf:name "Bob" . |
| :book dc:author _:b1 . |
| _:b2 rdf:rest rdf:nil . |
| _:b2 rdf:first :b . |
| :a foaf:knows _:b0 . |
| :a foaf:name "Alice" . |
| :a rdf:type foaf:Person . |
| _:b1 rdf:rest _:b2 . |
| _:b1 rdf:first :a . |
| :b foaf:knows :a . |
| </code></pre> |
| <p> </p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>TURTLE_FLAT</td> |
| </tr> |
| <tr> |
| <td>TRIG_FLAT</td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="opt-turtle-trig">Turtle and Trig format options</h3> |
| <p>Some context settings affect the output of Turtle and TriG writers. Unless |
| otherwise noted, the setting applies to both Turtle and TriG.</p> |
| <table> |
| <thead> |
| <tr> |
| <th>Context setting</th> |
| <th>Cmd line</th> |
| <th>Values</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>RIOT.symTurtleDirectiveStyle</td> |
| <td>“ttl:directiveStyle”</td> |
| <td>“sparql”, “rdf11”, “at”, “n3”</td> |
| </tr> |
| <tr> |
| <td>RIOT.symTurtleIndentStyle</td> |
| <td>“ttl:indentStyle”</td> |
| <td>“wide”, “long”</td> |
| </tr> |
| <tr> |
| <td>RIOT.symTurtleOmitBase</td> |
| <td>“ttl:omitBase”</td> |
| <td>“true”, “false”</td> |
| </tr> |
| </tbody> |
| </table> |
| <p> </p> |
| <table> |
| <thead> |
| <tr> |
| <th>Directive Style</th> |
| <th>Effect</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>“sparql”, “rdf11”</td> |
| <td>Use <code>PREFIX</code> and <code>BASE</code> in output.</td> |
| </tr> |
| <tr> |
| <td>“at”, “rdf10</td> |
| <td>Use <code>@prefix</code> and <code>@base</code> in output.</td> |
| </tr> |
| <tr> |
| <td>unset</td> |
| <td>Use <code>PREFIX</code> and <code>BASE</code> in output.</td> |
| </tr> |
| </tbody> |
| </table> |
| <p> </b> |
| <h4 id="format-option-usage">Format Option Usage</h4> |
| <h5 id="_setting-directive-style_"><em>Setting directive style</em></h5> |
| <pre tabindex="0"><code> riot --set ttl:directiveStyle=rdf11 --pretty Turtle file1.rdf file2.nt ... |
| </code></pre><p>and in code:</p> |
| <pre tabindex="0"><code> RDFWriter.source(model) |
| .set(RIOT.symTurtleDirectiveStyle, "sparql") |
| .lang(Lang.TTL) |
| .output(System.out); |
| </code></pre><h5 id="_setting-indent-style_"><em>Setting indent style</em></h5> |
| <pre tabindex="0"><code> riot --set ttl:indentStyle=long --formatted=ttl file1.rdf file2.nt ... |
| </code></pre><p>and in code:</p> |
| <pre tabindex="0"><code> RDFWriter.source(model) |
| .format(RDFFormat.TURTLE_LONG) |
| .output(System.out); |
| </code></pre><p>or:</p> |
| <pre tabindex="0"><code> RDFWriter.source(model) |
| .set(RIOT.symTurtleIndentStyle, "long") |
| .lang(Lang.TTL) |
| .output(System.out); |
| </code></pre><h5 id="_base-uri_"><em>Base URI</em></h5> |
| <p>Output can be written with relative URIs and no base. Note: such output is not |
| portable; its meaning depends on the base URI at the time of reading.</p> |
| <p>Turtle and Trig can be written with relative URIs by |
| setting the base URI for writing and switching off output of the base URI.</p> |
| <pre tabindex="0"><code> RDFWriter.create() |
| .base("http://host/someBase") |
| .set(RIOT.symTurtleOmitBase, true) |
| .lang(Lang.TTL) |
| .source(model) |
| .output(System.out); |
| </code></pre><h3 id="n-triples-and-n-quads">N-Triples and N-Quads</h3> |
| <p>These provide the formats that are fastest to write, |
| and data of any size can be output. They do not use any |
| internal state and formats always stream without limitation.</p> |
| <p>They maximise the |
| interoperability with other systems and are useful |
| for database dumps. They are not human readable, |
| even at moderate scale.</p> |
| <p>The files can be large but they compress well with gzip. |
| Compression ratios of x8-x10 can often be obtained.</p> |
| <p>Example:</p> |
| <p>The N-Triples writer makes no attempt to make it’s output readable. |
| It uses internal blank nodes to ensure correct labeling without |
| needing any writer state.</p> |
| <pre><code>_:BX2Dc2b3371X3A13cf8faaf53X3AX2D7fff <http://xmlns.com/foaf/0.1/name> "Bob" . |
| <http://example/book> <http://purl.org/dc/elements/1.1/author> _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffe . |
| _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffd <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . |
| _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffd <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example/b> . |
| <http://example/a> <http://xmlns.com/foaf/0.1/knows> _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7fff . |
| <http://example/a> <http://xmlns.com/foaf/0.1/name> "Alice" . |
| <http://example/a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . |
| _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffe <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffd . |
| _:BX2Dc2b3371X3A13cf8faaf53X3AX2D7ffe <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example/a> . |
| <http://example/b> <http://xmlns.com/foaf/0.1/knows> <http://example/a> . |
| </code></pre> |
| <p> </p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| <th>Other names</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>NTRIPLE</td> |
| <td>NTRIPLE, NT, NTRIPLES_UTF8</td> |
| </tr> |
| <tr> |
| <td>NQUADS</td> |
| <td>NQUADS, NQ, NQUADS_UTF8</td> |
| </tr> |
| </tbody> |
| </table> |
| <p> </p> |
| <p>The main N-Triples and N-Quads writers follow RDF 1.1 and output using UTF-8.<br> |
| For compatibility with old software, writers are provided that output |
| in ASCII (using <code>\u</code> escape sequences for non-ASCI characters where necessary).</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>NTRIPLES_ASCII</td> |
| </tr> |
| <tr> |
| <td>NQUADS_ASCII</td> |
| </tr> |
| </tbody> |
| </table> |
| <h3 id="json-ld">JSON-LD</h3> |
| <blockquote> |
| <p>Caution. This section describes features that may be removed.</p> |
| <p>Jena uses different third party processors for JSON-LD 1.0 and JSON-LD 1.1.</p> |
| <p>This section describes support for passing configuration to the JSON-LD 1.0 |
| processor only. It does not apply to the JSON-LD 1.1 processor.</p> |
| <p>It is planned that support for JSON-LD 1.0 will be removed in Jena5.</p> |
| <p>The project is looking for contributions for passing framing configuration to |
| the JSON-LD 1.1 processor, which is |
| <a href="https://github.com/filip26/titanium-json-ld">titanium-json-ld</a>.</p> |
| </blockquote> |
| <p>JSON-LD output is supported, in its various flavors |
| (“compacted”, “expanded”, “flattened”, “framed”), |
| by using one of the following RDFFormats:</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>JSONLD_EXPAND_PRETTY</td> |
| </tr> |
| <tr> |
| <td>JSONLD_EXPAND_FLAT</td> |
| </tr> |
| <tr> |
| <td>JSONLD_COMPACT_PRETTY</td> |
| </tr> |
| <tr> |
| <td>JSONLD_COMPACT_FLAT</td> |
| </tr> |
| <tr> |
| <td>JSONLD_FLATTEN_PRETTY</td> |
| </tr> |
| <tr> |
| <td>JSONLD_FLATTEN_FLAT</td> |
| </tr> |
| <tr> |
| <td>JSONLD_FRAME_PRETTY</td> |
| </tr> |
| <tr> |
| <td>JSONLD_FRAME_FLAT</td> |
| </tr> |
| </tbody> |
| </table> |
| <p>The default registration for <code>JSONLD</code> is <code>JSONLD_PRETTY</code>. |
| <code>JSONLD_PRETTY</code> is identical to <code>JSONLD_COMPACT_PRETTY</code>.</p> |
| <p>Output can be customized, passing more info to the writer by using the |
| “Context” mechanism provided by Jena. The same mechanism is used to |
| pass the “frame” in the <code>JSONLD_FRAME_PRETTY</code> and <code>JSONLD_FRAME_FLAT</code> |
| cases.</p> |
| <p>What can be done, and how it can be, is explained in the |
| <a href="https://github.com/apache/jena/tree/main/jena-examples/src/main/java/arq/examples/riot/Ex_WriteJsonLD.java">sample code</a>.</p> |
| <h3 id="rdf-binary">RDF Binary</h3> |
| <p><a href="rdf-binary.html">This is a binary encoding</a> using |
| <a href="https://thrift.apache.org/">Apache Thrift</a> or |
| <a href="https://developers.google.com/protocol-buffers">Google Protocol Buffers</a> |
| for RDF Graphs |
| and RDF Datasets, as well as SPARQL Result Sets, and it provides faster parsing |
| compared to the text-based standardised syntax such as N-triples, Turtle or RDF/XML.</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>RDF_THRIFT</td> |
| </tr> |
| <tr> |
| <td>RDF_THRIFT_VALUES</td> |
| </tr> |
| <tr> |
| <td>RDF_PROTO</td> |
| </tr> |
| <tr> |
| <td>RDF_PROTO_VALUES</td> |
| </tr> |
| </tbody> |
| </table> |
| <p><code>RDF_THRIFT_VALUES</code> and <code>RDF_PROTO_VALUES</code> are variants where numeric values are written as values, |
| not as lexical format and datatype. See the |
| <a href="https://rdf-binary.html">description of RDF Binary</a>. |
| for discussion.</p> |
| <h3 id="rdfxml">RDF/XML</h3> |
| <p>RIOT supports output in RDF/XML. RIOT RDFFormats defaults to pretty printed RDF/XML, |
| while the jena writer name defaults to a streaming plain output.</p> |
| <table> |
| <thead> |
| <tr> |
| <th>RDFFormat</th> |
| <th>Other names</th> |
| <th>Jena writer name</th> |
| </tr> |
| </thead> |
| <tbody> |
| <tr> |
| <td>RDFXML</td> |
| <td>RDFXML_PRETTY, RDF_XML_ABBREV</td> |
| <td>“RDF/XML-ABBREV”</td> |
| </tr> |
| <tr> |
| <td>RDFXML_PLAIN</td> |
| <td></td> |
| <td>“RDF/XML”</td> |
| </tr> |
| </tbody> |
| </table> |
| <p>More details <a href="rdfxml-io.html#rdfxml-output">RDF/XML Output</a>.</p> |
| <h2 id="examples">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="ways-to-write-a-model">Ways to write a model</h3> |
| <p>The follow ways are different ways to write a model in Turtle:</p> |
| <pre><code> Model model = ... ; |
| |
| // Write a model in Turtle syntax, default style (pretty printed) |
| RDFDataMgr.write(System.out, model, Lang.TURTLE) ; |
| |
| // Write Turtle to the blocks variant |
| RDFDataMgr.write(System.out, model, RDFFormat.TURTLE_BLOCKS) ; |
| |
| // Write as Turtle via model.write |
| model.write(System.out, "TTL") ; |
| </code></pre> |
| <h3 id="ways-to-write-a-dataset">Ways to write a dataset</h3> |
| <p>The preferred style is to use <code>RDFDataMgr</code>:</p> |
| <pre><code> Dataset ds = .... ; |
| // Write as TriG |
| RDFDataMgr.write(System.out, ds, Lang.TRIG) ; |
| |
| // Write as N-Quads |
| RDFDataMgr.write(System.out, dataset, Lang.NQUADS) ; |
| </code></pre> |
| <p>Additionally, a single model can be written in a dataset format - it becomes |
| the default graph of the dataset.</p> |
| <pre><code>Model m = ... ; |
| RDFDataMgr.write(System.out, m, Lang.TRIG) ; |
| </code></pre> |
| <p>might give:</p> |
| <pre><code>PREFIX : <http://example/> |
| PREFIX dc: <http://purl.org/dc/elements/1.1/> |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/> |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| |
| { |
| :book dc:author ( :a :b ) . |
| |
| :a a foaf:Person ; |
| foaf:knows [ foaf:name "Bob" ] ; |
| foaf:name "Alice" . |
| |
| :b foaf:knows :a . |
| } |
| </code></pre> |
| <h3 id="adding-a-new-output-format">Adding a new output format</h3> |
| <p>A complete example of adding a new output format is given in the example file: |
| <a href="https://github.com/apache/jena/blob/main/jena-examples/src/main/java/arq/examples/riot/ExRIOT7_AddNewWriter.java">RIOT Output example 7</a>.</p> |
| <h2 id="notes">Notes</h2> |
| <p>Using <code>OutputStream</code>s is strongly encouraged. This allows the writers |
| to manage the character encoding using UTF-8. Using <code>java.io.Writer</code> |
| does not allow this; on platforms such as MS Windows, the default |
| configuration of a <code>Writer</code> is not suitable for Turtle because |
| the character set is the platform default, and not UTF-8. |
| The only use of writers that is useful is using <code>java.io.StringWriter</code>.</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></li> |
| <li><a href="#rdfformat"><code>RDFFormat</code></a></li> |
| <li><a href="#jena_model_write_formats"><code>RDFFormat</code>s and Jena syntax names</a></li> |
| <li><a href="#formats">Formats</a> |
| <ul> |
| <li><a href="#normal-printing">Normal Printing</a></li> |
| <li><a href="#pretty-printed-languages">Pretty Printed Languages</a></li> |
| <li><a href="#streamed-block-formats">Streamed Block Formats</a></li> |
| <li><a href="#line-printed-formats">Line printed formats</a></li> |
| <li><a href="#opt-turtle-trig">Turtle and Trig format options</a> |
| <ul> |
| <li><a href="#format-option-usage">Format Option Usage</a></li> |
| </ul> |
| </li> |
| <li><a href="#n-triples-and-n-quads">N-Triples and N-Quads</a></li> |
| <li><a href="#json-ld">JSON-LD</a></li> |
| <li><a href="#rdf-binary">RDF Binary</a></li> |
| <li><a href="#rdfxml">RDF/XML</a></li> |
| </ul> |
| </li> |
| <li><a href="#examples">Examples</a> |
| <ul> |
| <li><a href="#ways-to-write-a-model">Ways to write a model</a></li> |
| <li><a href="#ways-to-write-a-dataset">Ways to write a dataset</a></li> |
| <li><a href="#adding-a-new-output-format">Adding a new output format</a></li> |
| </ul> |
| </li> |
| <li><a href="#notes">Notes</a></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> |