2018-09-17 17:46:28 +00:00
---
title: "Java API"
date: "2017-09-14"
2019-06-28 17:51:30 +00:00
weight: 1
summary: Ozone has a set of Native RPC based APIs. This is the lowest level API's on which all other protocols are built. This is the most performant and feature-full of all Ozone protocols.
2018-09-17 17:46:28 +00:00
---
2018-09-17 23:21:10 +00:00
<!-- -
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.
-->
2018-09-17 17:46:28 +00:00
2019-06-28 17:51:30 +00:00
Ozone ships with its own client library that supports RPC. For generic use cases the S3
compatible REST interface also can be used instead of the Ozone client.
2018-09-17 17:46:28 +00:00
## Creating an Ozone client
2019-03-01 01:09:05 +00:00
The Ozone client factory creates the ozone client. To get a RPC client we can call
2018-09-17 17:46:28 +00:00
{{< highlight java > }}
OzoneClient ozClient = OzoneClientFactory.getRpcClient();
{{< / highlight > }}
If the user want to create a client based on the configuration, then they can
2019-03-01 01:09:05 +00:00
call.
2018-09-17 17:46:28 +00:00
{{< highlight java > }}
OzoneClient ozClient = OzoneClientFactory.getClient();
{{< / highlight > }}
and an appropriate client based on configuration will be returned.
## Writing data using Ozone Client
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.
### Creating a Volume
Once we have a client, we need to get a reference to the ObjectStore. This
is done via
{{< highlight java > }}
ObjectStore objectStore = ozClient.getObjectStore();
{{< / highlight > }}
An object store represents an active cluster against which the client is working.
{{< highlight java > }}
// Let us create a volume to store our game assets.
// This uses default arguments for creating that volume.
2019-01-25 06:47:36 +00:00
objectStore.createVolume("assets");
2018-09-17 17:46:28 +00:00
// Let us verify that the volume got created.
2019-01-25 06:47:36 +00:00
OzoneVolume assets = objectStore.getVolume("assets");
2018-09-17 17:46:28 +00:00
{{< / highlight > }}
It is possible to pass an array of arguments to the createVolume by creating volume arguments.
### Creating a Bucket
Once you have a volume, you can create buckets inside the volume.
2019-08-21 17:17:41 +00:00
{{< highlight java > }}
2018-09-17 17:46:28 +00:00
// Let us create a bucket called videos.
2019-01-25 06:47:36 +00:00
assets.createBucket("videos");
OzoneBucket video = assets.getBucket("videos");
2018-09-17 17:46:28 +00:00
{{< / highlight > }}
2019-08-21 17:17:41 +00:00
At this point we have a usable volume and a bucket. Our volume is called _assets_ and bucket is called _videos_ .
2018-09-17 17:46:28 +00:00
Now we can create a Key.
### Reading and Writing a Key
2019-08-21 17:17:41 +00:00
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.
2018-09-17 17:46:28 +00:00
2019-08-21 17:17:41 +00:00
{{< highlight java > }}
2018-09-17 17:46:28 +00:00
// read data from the file, this is a user provided function.
2019-01-25 06:47:36 +00:00
byte [] videoData = readFile("intro.mp4");
2018-09-17 17:46:28 +00:00
// Create an output stream and write data.
2019-01-25 06:47:36 +00:00
OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
2018-09-17 17:46:28 +00:00
videoStream.write(videoData);
// Close the stream when it is done.
2019-01-25 06:47:36 +00:00
videoStream.close();
2018-09-17 17:46:28 +00:00
// 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[] data = new byte[(int)1048576];
2019-01-25 06:47:36 +00:00
OzoneInputStream introStream = video.readKey("intro.mp4");
2018-09-17 17:46:28 +00:00
// read intro.mp4 into the data buffer
introStream.read(data);
introStream.close();
{{< / highlight > }}
Here is a complete example of the code that we just wrote. Please note the close functions being called in this program.
{{< highlight java > }}
2018-09-18 17:23:36 +00:00
// Let us create a client
2018-09-17 17:46:28 +00:00
OzoneClient ozClient = OzoneClientFactory.getClient();
2018-09-18 17:23:36 +00:00
// Get a reference to the ObjectStore using the client
ObjectStore objectStore = ozClient.getObjectStore();
2018-09-17 17:46:28 +00:00
// Let us create a volume to store our game assets.
// This default arguments for creating that volume.
2019-01-25 06:47:36 +00:00
objectStore.createVolume("assets");
2018-09-17 17:46:28 +00:00
// Let us verify that the volume got created.
2019-01-25 06:47:36 +00:00
OzoneVolume assets = objectStore.getVolume("assets");
2018-09-17 17:46:28 +00:00
// Let us create a bucket called videos.
2019-01-25 06:47:36 +00:00
assets.createBucket("videos");
OzoneBucket video = assets.getBucket("videos");
2018-09-17 17:46:28 +00:00
// read data from the file, this is assumed to be a user provided function.
2019-01-25 06:47:36 +00:00
byte [] videoData = readFile("intro.mp4");
2018-09-17 17:46:28 +00:00
// Create an output stream and write data.
2019-01-25 06:47:36 +00:00
OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
2018-09-17 17:46:28 +00:00
videoStream.write(videoData);
// Close the stream when it is done.
2019-01-25 06:47:36 +00:00
videoStream.close();
2018-09-17 17:46:28 +00:00
// 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[] data = new byte[(int)1048576];
2019-01-25 06:47:36 +00:00
OzoneInputStream introStream = video.readKey("intro.mp4");
2018-09-17 17:46:28 +00:00
introStream.read(data);
// Close the stream when it is done.
introStream.close();
// Close the client.
ozClient.close();
{{< / highlight > }}