hadoop官方文档_Alluxio的Hadoop兼容模式写文件

   Alluxio基于内存的分布式文件系统,提供统一的访问方式,可集成多平台的数据存储系统。

998fbf8cc57019230fe159481210aa84.png

废话不多说,直接上代码:

// create a new hadoop configurationorg.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();Configuration conf = new Configuration();// support append acitonconf.setBoolean("dfs.support.append", true);conf.setStrings("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");conf.setBoolean("dfs.client.block.write.replace-datanode-on-failure.enabled", true);// enforce hadoop client to bind alluxio.hadoop.FileSystem for URIs like alluxio://conf.set("fs.alluxio.impl", "alluxio.hadoop.FileSystem");conf.set("fs.AbstractFileSystem.alluxio.impl", "alluxio.hadoop.AlluxioFileSystem");conf.set("alluxio.security.authentication.type", "SIMPLE");conf.set("alluxio.security.login.username", "sysadm");try(org.apache.hadoop.fs.FileSystem fs = org.apache.hadoop.fs.FileSystem.get(URI.create("alluxio://localhost:19998/user/hive/warehouse/table1"), conf,"sysadm")) {// Now alluxio address can be used like any other hadoop-compatible file system URIs  Path file = new Path("alluxio://localhost:19998/user/hive/warehouse/table1/dd.txt");  OutputStream hiveStream =  fs.create(file);  IOUtils.write("test", hiveStream);  hiveStream.close();  fs.close();} catch (IOException | InterruptedException e) {e.printStackTrace();fail("test failure:"+e.getLocalizedMessage());}

以上代码即是使用Hadoop兼容模式写文件到alluxio。

接口封装的已经很方便了,但是官方文档写的不够明确,还是花了不少时间来研究的。关键点说明(本来不想写的,贴代码还不够简单明了么 dcfa3c04e8d0ab7d048c5e47ab4b70dc.png):
  • Hadoop支持append操作

Configuration conf = new Configuration();

conf.setBoolean("dfs.support.append", true);

conf.setStrings("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");

conf.setBoolean("dfs.client.block.write.replace-datanode-on-failure.enabled", true);

加入这个配置是让Hadoop支持append操作,因为要向已存在文件写数据。我使用的Hadoop是2.4版本。

  • Hadoop client操作转向alluxio服务

conf.set("fs.alluxio.impl", "alluxio.hadoop.FileSystem");

conf.set("fs.AbstractFileSystem.alluxio.impl", "alluxio.hadoop.AlluxioFileSystem");

conf.set("alluxio.security.authentication.type", "SIMPLE");

conf.set("alluxio.security.login.username", getUsername());

这段代码是让Hadoop接口向alluxio服务推送数据。

这块需要注意的是用户名需要在configuration中设置,即alluxio.security.login.username属性,alluxio需要这么设置才能识别当前操作用户。

最后需要说明的是当前alluxio(2.3.0)版本不支持append操作,有此需求的童靴们放弃吧。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值