Java API

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.

Creating an Ozone client

The Ozone client factory creates the ozone client. To get a RPC client we can call

  1. OzoneClient ozClient = OzoneClientFactory.getRpcClient();

If the user want to create a client based on the configuration, then they can call.

  1. OzoneClient ozClient = OzoneClientFactory.getClient();

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

  1. ObjectStore objectStore = ozClient.getObjectStore();

An object store represents an active cluster against which the client is working.

  1. // Let us create a volume to store our game assets.
  2. // This uses default arguments for creating that volume.
  3. objectStore.createVolume("assets");
  4. // Let us verify that the volume got created.
  5. OzoneVolume assets = objectStore.getVolume("assets");

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.

  1. // Let us create a bucket called videos.
  2. assets.createBucket("videos");
  3. OzoneBucket video = assets.getBucket("videos");

At this point we have a usable volume and a bucket. Our volume is called assets and bucket is called videos.

Now we can create a Key.

Reading and Writing a Key

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.

  1. // read data from the file, this is a user provided function.
  2. byte [] videoData = readFile("intro.mp4");
  3. // Create an output stream and write data.
  4. OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
  5. videoStream.write(videoData);
  6. // Close the stream when it is done.
  7. videoStream.close();
  8. // We can use the same bucket to read the file that we just wrote, by creating an input Stream.
  9. // Let us allocate a byte array to hold the video first.
  10. byte[] data = new byte[(int)1048576];
  11. OzoneInputStream introStream = video.readKey("intro.mp4");
  12. // read intro.mp4 into the data buffer
  13. introStream.read(data);
  14. introStream.close();

Here is a complete example of the code that we just wrote. Please note the close functions being called in this program.

  1. // Let us create a client
  2. OzoneClient ozClient = OzoneClientFactory.getClient();
  3. // Get a reference to the ObjectStore using the client
  4. ObjectStore objectStore = ozClient.getObjectStore();
  5. // Let us create a volume to store our game assets.
  6. // This default arguments for creating that volume.
  7. objectStore.createVolume("assets");
  8. // Let us verify that the volume got created.
  9. OzoneVolume assets = objectStore.getVolume("assets");
  10. // Let us create a bucket called videos.
  11. assets.createBucket("videos");
  12. OzoneBucket video = assets.getBucket("videos");
  13. // read data from the file, this is assumed to be a user provided function.
  14. byte [] videoData = readFile("intro.mp4");
  15. // Create an output stream and write data.
  16. OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
  17. videoStream.write(videoData);
  18. // Close the stream when it is done.
  19. videoStream.close();
  20. // We can use the same bucket to read the file that we just wrote, by creating an input Stream.
  21. // Let us allocate a byte array to hold the video first.
  22. byte[] data = new byte[(int)1048576];
  23. OzoneInputStream introStream = video.readKey("intro.mp4");
  24. introStream.read(data);
  25. // Close the stream when it is done.
  26. introStream.close();
  27. // Close the client.
  28. ozClient.close();