blob: b83bf670546cec20b7c4a08913abf740501ce18b [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Apache Jena - SDB JavaAPI</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-extension.css" rel="stylesheet" type="text/css">
<link href="/css/jena.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="/images/favicon.ico" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script src="/js/jena-navigation.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/js/improve.js" type="text/javascript"></script>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></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 navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li id="homepage"><a href="/index.html"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li id="download"><a href="/download/index.cgi"><span class="glyphicon glyphicon-download-alt"></span> Download</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Learn <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Tutorials</li>
<li><a href="/tutorials/index.html">Overview</a></li>
<li><a href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li>
<li><a href="/documentation/notes/index.html">How-To's</a></li>
<li><a href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li>
<li><a href="/tutorials/rdf_api.html">RDF core API tutorial</a></li>
<li><a href="/tutorials/sparql.html">SPARQL tutorial</a></li>
<li><a href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li>
<li class="divider"></li>
<li class="dropdown-header">References</li>
<li><a href="/documentation/index.html">Overview</a></li>
<li><a href="/documentation/query/index.html">ARQ (SPARQL)</a></li>
<li><a href="/documentation/assembler/index.html">Assembler</a></li>
<li><a href="/documentation/tools/index.html">Command-line tools</a></li>
<li><a href="/documentation/rdfs/">Data with RDFS Inferencing</a></li>
<li><a href="/documentation/geosparql/index.html">GeoSPARQL</a></li>
<li><a href="/documentation/inference/index.html">Inference API</a></li>
<li><a href="/documentation/javadoc.html">Javadoc</a></li>
<li><a href="/documentation/ontology/">Ontology API</a></li>
<li><a href="/documentation/permissions/index.html">Permissions</a></li>
<li><a href="/documentation/extras/querybuilder/index.html">Query Builder</a></li>
<li><a href="/documentation/rdf/index.html">RDF API</a></li>
<li><a href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li>
<li><a href="/documentation/io/">RDF I/O</a></li>
<li><a href="/documentation/rdfstar/index.html">RDF-star</a></li>
<li><a href="/documentation/shacl/index.html">SHACL</a></li>
<li><a href="/documentation/shex/index.html">ShEx</a></li>
<li><a href="/documentation/jdbc/index.html">SPARQL over JDBC</a></li>
<li><a href="/documentation/tdb/index.html">TDB</a></li>
<li><a href="/documentation/tdb2/index.html">TDB2</a></li>
<li><a href="/documentation/query/text-query.html">Text Search</a></li>
</ul>
</li>
<li class="drop down">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-book"></span> Javadoc <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/documentation/javadoc.html">All Javadoc</a></li>
<li><a href="/documentation/javadoc/arq/">ARQ</a></li>
<li><a href="/documentation/javadoc_elephas.html">Elephas</a></li>
<li><a href="/documentation/javadoc/fuseki2/">Fuseki</a></li>
<li><a href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li>
<li><a href="/documentation/javadoc/jdbc/">JDBC</a></li>
<li><a href="/documentation/javadoc/jena/">Jena Core</a></li>
<li><a href="/documentation/javadoc/permissions/">Permissions</a></li>
<li><a href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li>
<li><a href="/documentation/javadoc/shacl/">SHACL</a></li>
<li><a href="/documentation/javadoc/tdb/">TDB</a></li>
<li><a href="/documentation/javadoc/text/">Text Search</a></li>
</ul>
</li>
<li id="ask"><a href="/help_and_support/index.html"><span class="glyphicon glyphicon-question-sign"></span> Ask</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-bullhorn"></span> Get involved <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/getting_involved/index.html">Contribute</a></li>
<li><a href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li>
<li class="divider"></li>
<li class="dropdown-header">Project</li>
<li><a href="/about_jena/about.html">About Jena</a></li>
<li><a href="/about_jena/architecture.html">Architecture</a></li>
<li><a href="/about_jena/citing.html">Citing</a></li>
<li><a href="/about_jena/team.html">Project team</a></li>
<li><a href="/about_jena/contributions.html">Related projects</a></li>
<li><a href="/about_jena/roadmap.html">Roadmap</a></li>
<li class="divider"></li>
<li class="dropdown-header">ASF</li>
<li><a href="http://www.apache.org/">Apache Software Foundation</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li>
<li><a href="http://www.apache.org/licenses/LICENSE-2.0">License</a></li>
<li><a href="http://www.apache.org/security/">Security</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
</ul>
</li>
<li id="edit"><a href="https://github.com/apache/jena-site/edit/main/source/documentation/archive/sdb/javaapi.md" title="Edit this page on GitHub"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="breadcrumbs">
<ol class="breadcrumb">
<li><a href='/documentation'>DOCUMENTATION</a></li>
<li><a href='/documentation/archive'>ARCHIVE</a></li>
<li><a href='/documentation/archive/sdb'>SDB</a></li>
<li class="active">JAVAAPI</li>
</ol>
</div>
<h1 class="title">SDB JavaAPI</h1>
<p>This page describes how to use SDB from Java.</p>
<p>Code examples are in <code>src-examples/</code> in the SDB distribution.</p>
<h2 id="contents">Contents</h2>
<ul>
<li><a href="#concepts">Concepts</a></li>
<li><a href="#obtaining-the-store">Obtaining the Store</a>
<ul>
<li><a href="#from-a-configuration-file">From a configuration file</a></li>
<li><a href="#in-java-code">In Java code</a></li>
<li><a href="#database-user-and-password">Database User and Password</a></li>
</ul>
</li>
<li><a href="#connection-management">Connection Management</a></li>
<li><a href="#formatting-or-emptying-the-store">Formatting or Emptying the Store</a></li>
<li><a href="#loading-data">Loading data</a></li>
<li><a href="#executing-queries">Executing Queries</a></li>
<li><a href="#using-the-jena-model-api-with-sdb">Using the Jena Model API with SDB</a></li>
</ul>
<h2 id="concepts">Concepts</h2>
<ul>
<li><code>Store</code></li>
<li><code>SDBFactory</code></li>
<li><code>SDBConnection</code></li>
</ul>
<p>SDB loads and queries data based on the unit of a <code>Store</code>. The
<code>Store</code> object has all the information for formatting, loading and
accessing an SDB database. One database or table space is one
Store. <code>Store</code> objects are made via the static method of the
<code>StoreFactory</code> class.</p>
<p><code>SDBConnection</code> wraps the underlying database connection, as well
as providing logging operations.</p>
<ul>
<li><code>StoreDesc</code></li>
</ul>
<p>A store description is the low level mechanism for describing
stores to be created.</p>
<ul>
<li><code>DatasetStore</code></li>
<li><code>GraphSDB</code></li>
</ul>
<p>Two further class are not immediately visible because they are
managed by the <code>SDBFactory</code> which creates the necessary classes,
such as Jena models and graphs.</p>
<p>An object of class <code>DatasetStore</code> represents an
<a href="http://www.w3.org/TR/sparql11-query/#rdfDataset" title="http://www.w3.org/TR/rdf-sparql-query/#rdfDataset">RDF dataset</a>
backed by an SDB store. Objects of this class trigger SPARQL
queries being sent to SDB.</p>
<p>The class <code>GraphSDB</code> provides the adapter between the standard Jena
Java API and an SDB store, either to the default graph or one of
the named graphs. The <code>SDBFactory</code> can also create Jena Models
backed by such a graph.</p>
<h2 id="obtaining-the-store">Obtaining the Store</h2>
<p>A store is build from a description. This can be a description in
file as a
<a href="../assembler">Jena assembler</a>
or the application can build the store description
programmatically.</p>
<h3 id="from-a-configuration-file">From a configuration file</h3>
<p>The stored description is the only point where the specific details
of store are given. This includes connection information, the
database name, and database type. It makes sense to place this
outside the code. That way, the application can be switched between
different databases (e.g. testing and production) by changing a
configuration file, and not the code, which would require
recompilation and a rebuild.</p>
<p>To create a <code>Store</code> from a store assembler</p>
<pre><code> Store store = SDBFactory.connectStore(&quot;sdb.ttl&quot;) ;
</code></pre>
<p>The assembler file has two parts, the connection details and the
store type.</p>
<pre><code> @prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt; .
@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .
@prefix ja: &lt;http://jena.hpl.hp.com/2005/11/Assembler#&gt; .
@prefix sdb: &lt;http://jena.hpl.hp.com/2007/sdb#&gt; .
_:c rdf:type sdb:SDBConnection ;
sdb:sdbType &quot;derby&quot; ;
sdb:sdbName &quot;DB/SDB2&quot; ;
sdb:driver &quot;org.apache.derby.jdbc.EmbeddedDriver&quot; ;
.
[] rdf:type sdb:Store ;
sdb:layout &quot;layout2&quot; ;
sdb:connection _:c ;
.
</code></pre>
<p>See the full details of
<a href="store_description.html" title="SDB/Store Description">store description files</a>
for the options.</p>
<h3 id="in-java-code">In Java code</h3>
<p>The less flexible way to create a store description is to build it
in Java. For example:</p>
<pre><code> StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash,
DatabaseType.Derby) ;
JDBC.loadDriverDerby() ;
String jdbcURL = &quot;jdbc:derby:DB/SDB2&quot;;
SDBConnection conn = new SDBConnection(jdbcURL, null, null) ;
Store store = SDBFactory.connectStore(conn, storeDesc) ;
</code></pre>
<h3 id="database-user-and-password">Database User and Password</h3>
<p>The user and password for the database can be set in explicitly in
the description file but it is usually better to use an environment
variable or Java system property because this avoid writing use and
password in a file.</p>
<p>Environment variable: <code>SDB_USER</code>
Java property: <code>jena.db.user</code></p>
<p>Environment variable: <code>SDB_PASSWORD</code>
Java property: <code>jena.db.password</code></p>
<h2 id="connection-management">Connection Management</h2>
<p>Each store has a JDBC connection associated with it.</p>
<p>In situations where such connections are managed externally, the
store object can be created and used within a single operation.</p>
<p>A <code>Store</code> is lightweight and does not perform any database actions
when created, so creating and releasing them will not impact
performance. Closing a store does not close the JDBC connection.</p>
<p>Similarly, a <code>SDBConnection</code> is lightweight and creation does not
result in any database or JDBC connection actions.</p>
<p>The store description can be read from the same file because any
SDB connection information is ignored when reading to get just the
store description. The store description can be kept across store
creations:</p>
<pre><code> storeDesc = StoreDesc.read(&quot;sdb.ttl&quot;) ;
</code></pre>
<p>then used with an JDBC connection object passed from the connection
container:</p>
<pre><code> public static void query(String queryString,
StoreDesc storeDesc,
Connection jdbcConnection)
{
Query query = QueryFactory.create(queryString) ;
SDBConnection conn = SDBFactory.createConnection(jdbcConnection) ;
Store store = SDBFactory.connectStore(conn, storeDesc) ;
Dataset ds = SDBFactory.connectDataset(store) ;
try(QueryExecution qe = QueryExecutionFactory.create(query, ds)) {
ResultSet rs = qe.execSelect() ;
ResultSetFormatter.out(rs) ;
}
store.close() ;
}
</code></pre>
<h2 id="formatting-or-emptying-the-store">Formatting or Emptying the Store</h2>
<p>SDB stores do not ensure that the database is formatted. You can
check whether the store is already formatted using:</p>
<pre><code>StoreUtils.isFormatted(store);
</code></pre>
<p>This is an expensive operation, and should be used sparingly.</p>
<p>Once you obtain a store for the first time you will need to:</p>
<pre><code>store.getTableFormatter().create();
</code></pre>
<p>This will create the necessary tables and indexes required for a
full SDB store.</p>
<p>You may empty the store completely using:</p>
<pre><code>store.getTableFormatter().truncate();
</code></pre>
<h2 id="loading-data">Loading data</h2>
<p>Data loading uses the standard Jena <code>Model.read</code> operations.
<code>GraphSDB</code>, and models made from a <code>GraphSDB</code>, implement the
standard Jena bulk data interface with backed by an SBD
implementation of that interface.</p>
<h2 id="executing-queries">Executing Queries</h2>
<p>The interface to making queries with SDB is same as that for
<a href="../query/app_api.html">querying with ARQ</a>.
SDB is an ARQ query engine that can handle queries made on an RDF
dataset which is of the SDB class <code>DatasetStore</code>:</p>
<pre><code> Dataset ds = DatasetStore.create(store) ;
</code></pre>
<p>This is then used as normal with ARQ:</p>
<pre><code> Dataset ds = DatasetStore.create(store) ;
try(QueryExecution qe = QueryExecutionFactory.create(query, ds)) {
ResultSet rs = qe.execSelect() ;
ResultSetFormatter.out(rs) ;
}
</code></pre>
<p>When finished, the store should be closed to release any resources
associated with the particular implementation. Closing a store does
<em>not</em> close it&rsquo;s JDBC connection.</p>
<pre><code> store.close() ;
</code></pre>
<p>Closing the SDBConnection <em>does</em> close the JDBC connection:</p>
<pre><code> store.getConnection().close() ;
store.close() ;
</code></pre>
<p>If models or graphs backed by SDB are placed in a general Dataset
then the query is not efficiently executed by SDB.</p>
<h2 id="using-the-jena-model-api-with-sdb">Using the Jena Model API with SDB</h2>
<p>A Jena model can be connected to one graph in the store and used
with all the Jena API operations.</p>
<p>Here, the graph for the model is the default graph:</p>
<pre><code> Store store = SDBFactory.connectStore(&quot;sdb.ttl&quot;) ;
Model model = SDBFactory.connectDefaultModel(store) ;
StmtIterator sIter = model.listStatements() ;
for ( ; sIter.hasNext() ; )
{
Statement stmt = sIter.nextStatement() ;
System.out.println(stmt) ;
}
sIter.close() ;
store.close() ;
</code></pre>
<p>SDB is optimized for SPARQL queries but queries and other Jena API
operations can be mixed. The results from a SPARQL query are Jena
RDFNodes, with the associated model having a graph implemented by
SDB.</p>
</div>
</div>
</div>
<footer class="footer">
<div class="container" style="font-size:80%" >
<p>
Copyright &copy; 2011&ndash;2022 The Apache Software Foundation, Licensed under the
<a href="http://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 type="text/javascript">
var link = $('a[href="' + this.location.pathname + '"]');
if (link != undefined)
link.parents('li,ul').addClass('active');
</script>
</body>
</html>