学习 Alluxio(四):Java API

栏目: 数据库 · 发布时间: 6年前

内容简介:配置 Alluxio Master 主机地址:配置用户:默认会使用当前用户,如果当前用户没有权限,则会抛出异常

配置

配置 Alluxio Master 主机地址:

Configuration.set(PropertyKey.MASTER_HOSTNAME, "alluxio_master");

配置用户:

Configuration.set(PropertyKey.SECURITY_LOGIN_USERNAME, "alluxio");

默认会使用当前用户,如果当前用户没有权限,则会抛出异常 alluxio.exception.status.PermissionDeniedException: Permission denied

写文件

FileSystem fs = FileSystem.Factory.get();  
try (FileOutStream out = fs.createFile(new AlluxioURI("/tmp/test"))) {  
    out.write("it works!".getBytes("utf-8"));
} catch (AlluxioException | IOException e) {
    // do something
}

在创建文件时,可以对写操作进行配置:

CreateFileOptions createFileOptions = CreateFileOptions.defaults()  
    .setWriteType(WriteType.MUST_CACHE);
try (FileOutStream out = fs.createFile(new AlluxioURI("/tmp/test"), createFileOptions)) {  
} catch (AlluxioException | IOException e) {
    // do something
}

WriteType 选项:

  • CACHE_THROUGH 数据被同步地写入到 Alluxio 的 Worker 和底层存储系统。
  • MUST_CACHE 数据被同步地写入到 Alluxio 的 Worker。但不会被写入到底层存储系统。这是默认写类型。
  • THROUGH 数据被同步地写入到底层存储系统。但不会被写入到 Alluxio 的 Worker。
  • ASYNC_THROUGH 数据被同步地写入到 Alluxio 的 Worker,并异步地写入到底层存储系统。处于实验阶段。

读文件

try (FileInStream in = fs.openFile(new AlluxioURI("/tmp/test"))) {  
    String content = IOUtils.toString(in, "utf-8");
    System.out.println(content);
} catch (AlluxioException | IOException e) {
    // do something
}

在打开文件时,可以对读操作进行配置:

OpenFileOptions openFileOptions = OpenFileOptions.defaults().setReadType(ReadType.CACHE);

try (FileInStream in = fs.openFile(new AlluxioURI("/tmp/test"), openFileOptions)) {  
} catch (AlluxioException | IOException e) {
    // do something
}

ReadType 选项:

  • CACHE_PROMOTE 如果读取的数据在 Worker 上时,该数据被移动到 Worker 的最高层。如果该数据不在本地 Worker 的 Alluxio 存储中,那么就将一个副本添加到本地 Alluxio Worker 中。 如果 alluxio.user.file.cache.partially.read.block 设置为 true,没有完全读取的数据块也会被 全部 存到 Alluxio 内。 相反,一个数据块只有完全被读取时,才能被缓存。
  • CACHE 如果该数据不在本地 Worker 的 Alluxio 存储中,那么就将一个副本添加到本地 Alluxio Worker 中。如果 alluxio.user.file.cache.partially.read.block 设置为 true,没有完全读取的数据块也会被 全部 存到 Alluxio 内。 相反,一个数据块只有完全被读取时,才能被缓存。
  • NO_CACHE 仅读取数据,不在 Alluxio 中存储副本。

参考


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms

Algorithms

Sanjoy Dasgupta、Christos H. Papadimitriou、Umesh Vazirani / McGraw-Hill Education / 2006-10-16 / GBP 30.99

This text, extensively class-tested over a decade at UC Berkeley and UC San Diego, explains the fundamentals of algorithms in a story line that makes the material enjoyable and easy to digest. Emphasi......一起来看看 《Algorithms》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具