客户端接口
Java 客户端 API
登录后可跨设备保存划线和私人笔记登录
Apache Ozone 的 Java 客户端 API 提供了以编程方式访问 Ozone 存储的能力。
如需查看详细的 API 文档,请参阅 OzoneClient Javadoc。Ozone 自带支持 RPC 的客户端库。对于通用使用场景,也可以使用兼容 S3 的 REST 接口来替代 Ozone 客户端。
创建 Ozone 客户端
Ozone 客户端由 Ozone 客户端工厂创建。要获取一个 RPC 客户端,我们可以调用
OzoneClient ozClient = OzoneClientFactory.getRpcClient();如果用户想要基于配置创建客户端,则可以调用
OzoneClient ozClient = OzoneClientFactory.getClient();并根据配置返回相应的客户端。
使用 Ozone Client 写入数据
Ozone 内部的数据层级结构依次为:卷(Volume)、桶(Bucket)和键(Key)。一个卷包含多个桶,一个桶包含多个键。要向 Ozone 写入数据,你需要一个卷、一个桶和一个键。
创建卷
获取客户端之后,我们需要取得 ObjectStore 的引用。这可以通过
ObjectStore objectStore = ozClient.getObjectStore();对象存储(object store)表示客户端正在与其进行交互的活动集群。
// Let us create a volume to store our game assets.
// This uses default arguments for creating that volume.
objectStore.createVolume("assets");
// Let us verify that the volume got created.
OzoneVolume assets = objectStore.getVolume("assets");可以通过创建 volume 参数,向 createVolume 传递参数数组。
创建 Bucket
拥有 volume 之后,就可以在该 volume 内创建 bucket。
// Let us create a bucket called videos.
assets.createBucket("videos");
OzoneBucket video = assets.getBucket("videos");至此,我们已经有了一个可用的卷和一个桶。卷名为 assets,桶名为 videos。
现在可以创建一个 Key 了。
读取与写入 Key
有了桶对象之后,用户就可以读写 Key 了。下面的代码从本地磁盘读取一个名为 intro.mp4 的视频文件,并将其存储到我们刚刚创建的 video 桶中。
// read data from the file, this is a user provided function.
byte [] videoData = readFile("intro.mp4");
// Create an output stream and write data.
OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
videoStream.write(videoData);
// Close the stream when it is done.
videoStream.close();
// 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];
OzoneInputStream introStream = video.readKey("intro.mp4");
// read intro.mp4 into the data buffer
introStream.read(data);
introStream.close();以下是我们刚刚编写的完整代码示例。请注意该程序中被调用的关闭(close)函数。
// Let us create a client
OzoneClient ozClient = OzoneClientFactory.getClient();
// Get a reference to the ObjectStore using the client
ObjectStore objectStore = ozClient.getObjectStore();
// Let us create a volume to store our game assets.
// This default arguments for creating that volume.
objectStore.createVolume("assets");
// Let us verify that the volume got created.
OzoneVolume assets = objectStore.getVolume("assets");
// Let us create a bucket called videos.
assets.createBucket("videos");
OzoneBucket video = assets.getBucket("videos");
// read data from the file, this is assumed to be a user provided function.
byte [] videoData = readFile("intro.mp4");
// Create an output stream and write data.
OzoneOutputStream videoStream = video.createKey("intro.mp4", 1048576);
videoStream.write(videoData);
// Close the stream when it is done.
videoStream.close();
// 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];
OzoneInputStream introStream = video.readKey("intro.mp4");
introStream.read(data);
// Close the stream when it is done.
introStream.close();
// Close the client.
ozClient.close();评论
登录后参与评论
正在加载评论…
KnowForge