Alluxio基于内存的分布式文件系统,提供统一的访问方式,可集成多平台的数据存储系统。
废话不多说,直接上代码:
// 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。
接口封装的已经很方便了,但是官方文档写的不够明确,还是花了不少时间来研究的。关键点说明(本来不想写的,贴代码还不够简单明了么 ):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操作,有此需求的童靴们放弃吧。