blob: 242c6e0968d244c1901d6f97cc2aaea56ff501a9 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Hadoop Ozone Documentation">
<title>Documentation for Apache Hadoop Ozone</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/ozonedoc.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Apache Hadoop Ozone/HDDS documentation</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/apache/hadoop">Source</a></li>
<li><a href="https://hadoop.apache.org">Apache Hadoop</a></li>
<li><a href="https://apache.org">ASF</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<img src="ozone-logo.png" style="max-width: 100%;"/>
<ul class="nav nav-sidebar">
<li class="">
<a href="index.html">
<span>Ozone Overview</span>
</a>
</li>
<li class="">
<a href="runningviadocker.html">
<span>Getting Started</span>
</a>
<ul class="nav">
<li class="">
<a href="./runningviadocker.html">Alpha Cluster</a>
</li>
<li class="">
<a href="./settings.html">Configuration</a>
</li>
<li class="">
<a href="./realcluster.html">Starting an Ozone Cluster</a>
</li>
<li class="">
<a href="./runningwithhdfs.html">Running concurrently with HDFS</a>
</li>
<li class="">
<a href="./buildingsources.html">Building from Sources</a>
</li>
</ul>
</li>
<li class="">
<a href="commandshell.html">
<span>Client</span>
</a>
<ul class="nav">
<li class="">
<a href="./commandshell.html">Ozone CLI</a>
</li>
<li class="">
<a href="./volumecommands.html">Volume Commands</a>
</li>
<li class="">
<a href="./bucketcommands.html">Bucket Commands</a>
</li>
<li class="">
<a href="./keycommands.html">Key Commands</a>
</li>
<li class="active">
<a href="./javaapi.html">Java API</a>
</li>
<li class="">
<a href="./ozonefs.html">Ozone File System</a>
</li>
<li class="">
<a href="./rest.html">REST API</a>
</li>
</ul>
</li>
<li class="">
<a href="dozone.html">
<span>Tools</span>
</a>
<ul class="nav">
<li class="">
<a href="./dozone.html">Dozone &amp; Dev Tools</a>
</li>
<li class="">
<a href="./freon.html">Freon</a>
</li>
<li class="">
<a href="./scmcli.html">SCMCLI</a>
</li>
</ul>
</li>
<li class="">
<a href="./concepts.html">
<span>Architecture</span>
</a>
<ul class="nav">
<li class="">
<a href="./hdds.html">Hadoop Distributed Data Store</a>
</li>
<li class="">
<a href="./ozonemanager.html">Ozone Manager</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1>Java API</h1>
<div class="col-md-9">
<!---
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<h2 id="introduction">Introduction</h2>
<p>Ozone ships with it own client library, that supports both RPC(Remote
Procedure call) and REST(Representational State Transfer). This library is
the primary user interface to ozone.</p>
<p>It is trivial to switch from RPC to REST or vice versa, by setting the
property <em>ozone.client.protocol</em> in the configuration or by calling the
appropriate factory method.</p>
<h2 id="creating-an-ozone-client">Creating an Ozone client</h2>
<p>The Ozone client factory creates the ozone client. It allows the user to
specify the protocol of communication. For example, to get an REST client, we
can use</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">OzoneClient ozClient <span style="color:#f92672">=</span> OzoneClientFactory<span style="color:#f92672">.</span><span style="color:#a6e22e">getRestClient</span><span style="color:#f92672">();</span></code></pre></div>
<p>And to get a a RPC client we can call</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">OzoneClient ozClient <span style="color:#f92672">=</span> OzoneClientFactory<span style="color:#f92672">.</span><span style="color:#a6e22e">getRpcClient</span><span style="color:#f92672">();</span></code></pre></div>
<p>If the user want to create a client based on the configuration, then they can
call</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">OzoneClient ozClient <span style="color:#f92672">=</span> OzoneClientFactory<span style="color:#f92672">.</span><span style="color:#a6e22e">getClient</span><span style="color:#f92672">();</span></code></pre></div>
<p>and an appropriate client based on configuration will be returned.</p>
<h2 id="writing-data-using-ozone-client">Writing data using Ozone Client</h2>
<p>The hierarchy of data inside ozone is a volume, bucket and a key. A volume
is a collection of buckets. A bucket is a collection of keys. To write data
to the ozone, you need a volume, bucket and a key.</p>
<h3 id="creating-a-volume">Creating a Volume</h3>
<p>Once we have a client, we need to get a reference to the ObjectStore. This
is done via</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">ObjectStore objectStore <span style="color:#f92672">=</span> ozClient<span style="color:#f92672">.</span><span style="color:#a6e22e">getObjectStore</span><span style="color:#f92672">();</span></code></pre></div>
<p>An object store represents an active cluster against which the client is working.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// Let us create a volume to store our game assets.
</span><span style="color:#75715e">// This uses default arguments for creating that volume.
</span><span style="color:#75715e"></span>objectStore<span style="color:#f92672">.</span><span style="color:#a6e22e">createVolume</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>assets<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
<span style="color:#75715e">// Let us verify that the volume got created.
</span><span style="color:#75715e"></span>OzoneVolume assets <span style="color:#f92672">=</span> objectStore<span style="color:#f92672">.</span><span style="color:#a6e22e">getVolume</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>assets<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span></code></pre></div>
<p>It is possible to pass an array of arguments to the createVolume by creating volume arguments.</p>
<h3 id="creating-a-bucket">Creating a Bucket</h3>
<p>Once you have a volume, you can create buckets inside the volume.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">// Let us create a bucket called videos.
assets.createBucket<span style="color:#f92672">(</span>“videos”<span style="color:#f92672">)</span>;
Ozonebucket video <span style="color:#f92672">=</span> assets.getBucket<span style="color:#f92672">(</span>“videos”<span style="color:#f92672">)</span>;</code></pre></div>
<p>At this point we have a usable volume and a bucket. Our volume is called assets and bucket is called videos.</p>
<p>Now we can create a Key.</p>
<h3 id="reading-and-writing-a-key">Reading and Writing a Key</h3>
<p>With a bucket object the users can now read and write keys. The following code reads a video called intro.mp4 from the local disk and stores in the video bucket that we just created.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">// read data from the file, this is a user provided <span style="color:#66d9ef">function</span>.
byte <span style="color:#f92672">[]</span> vidoeData <span style="color:#f92672">=</span> readFile<span style="color:#f92672">(</span>“into.mp4”<span style="color:#f92672">)</span>;
// Create an output stream and write data.
OzoneOutputStream videoStream <span style="color:#f92672">=</span> video.createKey<span style="color:#f92672">(</span>“intro.mp4”, <span style="color:#ae81ff">1048576</span><span style="color:#f92672">)</span>;
videoStream.write<span style="color:#f92672">(</span>videoData<span style="color:#f92672">)</span>;
// Close the stream when it is <span style="color:#66d9ef">done</span>.
videoStream.close<span style="color:#f92672">()</span>;
// We can use the same bucket to read the file that we just wrote, by creating an input Stream.
// Let us allocate a byte array to hold the video first.
byte<span style="color:#f92672">[]</span> data <span style="color:#f92672">=</span> new byte<span style="color:#f92672">[(</span>int<span style="color:#f92672">)</span><span style="color:#ae81ff">1048576</span><span style="color:#f92672">]</span>;
OzoneInputStream introStream <span style="color:#f92672">=</span> video.readKey<span style="color:#f92672">(</span>“intro.mp4”<span style="color:#f92672">)</span>;
// read intro.mp4 into the data buffer
introStream.read<span style="color:#f92672">(</span>data<span style="color:#f92672">)</span>;
introStream.close<span style="color:#f92672">()</span>;</code></pre></div>
<p>Here is a complete example of the code that we just wrote. Please note the close functions being called in this program.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// Let us create a client
</span><span style="color:#75715e"></span>OzoneClient ozClient <span style="color:#f92672">=</span> OzoneClientFactory<span style="color:#f92672">.</span><span style="color:#a6e22e">getClient</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// Get a reference to the ObjectStore using the client
</span><span style="color:#75715e"></span>ObjectStore objectStore <span style="color:#f92672">=</span> ozClient<span style="color:#f92672">.</span><span style="color:#a6e22e">getObjectStore</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// Let us create a volume to store our game assets.
</span><span style="color:#75715e">// This default arguments for creating that volume.
</span><span style="color:#75715e"></span>objectStore<span style="color:#f92672">.</span><span style="color:#a6e22e">createVolume</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>assets<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
<span style="color:#75715e">// Let us verify that the volume got created.
</span><span style="color:#75715e"></span>OzoneVolume assets <span style="color:#f92672">=</span> objectStore<span style="color:#f92672">.</span><span style="color:#a6e22e">getVolume</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>assets<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
<span style="color:#75715e">// Let us create a bucket called videos.
</span><span style="color:#75715e"></span>assets<span style="color:#f92672">.</span><span style="color:#a6e22e">createBucket</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>videos<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
Ozonebucket video <span style="color:#f92672">=</span> assets<span style="color:#f92672">.</span><span style="color:#a6e22e">getBucket</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>videos<span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
<span style="color:#75715e">// read data from the file, this is assumed to be a user provided function.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">byte</span> <span style="color:#f92672">[]</span> vidoeData <span style="color:#f92672">=</span> readFile<span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>into<span style="color:#f92672">.</span><span style="color:#a6e22e">mp4</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
<span style="color:#75715e">// Create an output stream and write data.
</span><span style="color:#75715e"></span>OzoneOutputStream videoStream <span style="color:#f92672">=</span> video<span style="color:#f92672">.</span><span style="color:#a6e22e">createKey</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>intro<span style="color:#f92672">.</span><span style="color:#a6e22e">mp4</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">,</span> 1048576<span style="color:#f92672">);</span>
videoStream<span style="color:#f92672">.</span><span style="color:#a6e22e">write</span><span style="color:#f92672">(</span>videoData<span style="color:#f92672">);</span>
<span style="color:#75715e">// Close the stream when it is done.
</span><span style="color:#75715e"></span> videoStream<span style="color:#f92672">.</span><span style="color:#a6e22e">close</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// We can use the same bucket to read the file that we just wrote, by creating an input Stream.
</span><span style="color:#75715e">// Let us allocate a byte array to hold the video first.
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">byte</span><span style="color:#f92672">[]</span> data <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#66d9ef">byte</span><span style="color:#f92672">[(</span><span style="color:#66d9ef">int</span><span style="color:#f92672">)</span>1048576<span style="color:#f92672">];</span>
OzoneInputStream introStream <span style="color:#f92672">=</span> video<span style="color:#f92672">.</span><span style="color:#a6e22e">readKey</span><span style="color:#f92672">(</span><span style="color:#960050;background-color:#1e0010"></span>into<span style="color:#f92672">.</span><span style="color:#a6e22e">mp4</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#f92672">);</span>
introStream<span style="color:#f92672">.</span><span style="color:#a6e22e">read</span><span style="color:#f92672">(</span>data<span style="color:#f92672">);</span>
<span style="color:#75715e">// Close the stream when it is done.
</span><span style="color:#75715e"></span>introStream<span style="color:#f92672">.</span><span style="color:#a6e22e">close</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// Close the client.
</span><span style="color:#75715e"></span>ozClient<span style="color:#f92672">.</span><span style="color:#a6e22e">close</span><span style="color:#f92672">();</span></code></pre></div>
</div>
</div>
</div>
</div>
<script src="./js/jquery.min.js"></script>
<script src="./js/ozonedoc.js"></script>
<script src="./js/bootstrap.min.js"></script>
</body>
</html>