Alluxio User Guide
From IIIS-Systems
Revision as of 19:21, 15 November 2017 by Chentianjia (Talk | contribs)
Docker使用Alluxio教程
什么是Alluxio?
Alluxio是一个基于内存的分布式文件系统,它是架构在底层分布式文件系统和上层分布式计算框架之间的一个中间件,主要职责是以文件形式在内存或其它存储设施中提供数据的存取服务。
徐葳老师希望所有Docker虚拟机共享内存中的文件,这样就可以快速读取文件信息。搭出来的Alluxio是这样的。
我们的Alluxio
我们的Alluxio的底层存储系统有两个: Ceph与HDFS。利用Alluxio’s unified namespace有两个优势:
1)程序可以使用相同的命名空间和接口,在不同的底层存储系统中通信。程序和新的存储之间无缝结合。
2)仅需把数据在内存中加载一次,你的程序就能以不同类型的存储系统进行访问。
我们的Docker虚拟机默认连接的是Ceph。需要调用Alluxio的API才能把数据加载到内存中。例子是如何用Java在Ceph中读取/写入数据:
默认路径是/mnt/data,读取/写入数据请从新构造一个文件夹:
写数据
// 获取文件系统客户端FileSystem实例
FileSystem fs = FileSystem.Factory.get();
// 构造Alluxio路径AlluxioURI实例
AlluxioURI path = new AlluxioURI("/myFile");
// 设置一些操作选项
// 设置文件块大小为128M
CreateFileOptions options = CreateFileOptions.defaults().setBlockSize(128 * Constants.MB);
// 创建一个文件并获取它的文件输出流FileOutStream实例
FileOutStream out = fs.createFile(path);
// 调用文件输出流FileOutStream实例的write()方法写入数据
out.write(...);
// 关闭文件输出流FileOutStream实例,结束写文件操作
out.close();
读数据
// 获取文件系统客户端FileSystem实例
FileSystem fs = FileSystem.Factory.get();
// 构造Alluxio路径AlluxioURI实例
AlluxioURI path = new AlluxioURI("/myFile");
// 打开一个文件,获得文件输入流FileInStream(同时获得一个锁以防止文件被删除)
FileInStream in = fs.openFile(path);
// 调用文件输入流FileInStream实例的read()方法读数据
in.read(...);
// 关闭文件输入流FileInStream实例,结束读文件操作(同时释放锁)
in.close();
我想要同时用Ceph跟HDFS
package io; import alluxio.client.file.FileInStream; import alluxio.client.file.FileOutStream; import alluxio.client.file.FileSystem;
// mount HDFS to Alluxio
FileSystem fileSystem = FileSystem.Factory.get();
fileSystem.mount("/mnt/hdfs", "hdfs://10.10.0.1:9000/hdfs/data1");
// 从HDFS读取数据
AlluxioURI inputUri = new AlluxioURI("/mnt/hdfs/input.data");
FileInStream is = fileSystem.openFile(inputUri);
... // read data
is.close();
... // perform computation
// 写数据到HDFS
AlluxioURI outputUri = new AlluxioURI("/mnt/hdfs/output.data");
FileOutStream os = fileSystem.createFile(outputUri);
... // write data
os.close();

