1. 介绍
本文介绍下如何通过alluxio读写HDFS。我们在WINDOWS下创建MAVEN工程编写代码。
2. 前提
查看本文的前提是你已经安装好了alluxio并且已经集成了HDFS。如果还没有,请查看:
如果连hadoop还没有安装,可以参考:
如果在WINDOWS下访问hadoop集群还不会可以查看
3. 添加alluxio的jar包
这个如果你下载的是预编译好的alluxio,则在$ALLUXIO_HOME/core/client/target写可以找到alluxio-core-client-1.2.0-jar-with-dependencies.jar
在IDEA当中添加该依赖。因为这个是自己编译出来的额外的JAR,在MAVEN仓库还没有,所以我们手动添加下。
4. 在本地添加配置文件
在本地需要添加core-site.xml和hdfs-site.xml。当然其中也包含了alluxio相关的配置。我的配置如下,大家按照自己的情况可以做修正。
4.1 core-site.xml
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://10.45.10.33:8020</value>
</property>
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
<property>
<name>fs.alluxio.impl</name>
<value>alluxio.hadoop.FileSystem</value>
<description>The Alluxio FileSystem (Hadoop 1.x and 2.x)</description>
</property>
<property>
<name>fs.alluxio-ft.impl</name>
<value>alluxio.hadoop.FaultTolerantFileSystem</value>
<description>The Alluxio FileSystem (Hadoop 1.x and 2.x) with fault tolerant support</description>
</property>
<property>
<name>fs.AbstractFileSystem.alluxio.impl</name>
<value>alluxio.hadoop.AlluxioFileSystem</value>
<description>The Alluxio AbstractFileSystem (Hadoop 2.x)</description>
</property>
</configuration>
4.2 hdfs-site.xml
<configuration>
<!--复制的份数,默认为3份,仅仅在datanode上配置-->
<property>
<name>dfs.replication</name>
<value>3</value>
</property>
<!--datanode在hdfs上的路径,仅仅在datanode上配置-->
<property>
<name>dfs.namenode.data.dir</name>
<value>file:/root/Downloads/hdfs/datanode</value>
</property>
<!-- hue 相关配置 -->
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
<!--alluxio configuration -->
<property>
<name>fs.alluxio.impl</name>
<value>alluxio.hadoop.FileSystem</value>
<description>The Alluxio FileSystem (Hadoop 1.x and 2.x)</description>
</property>
<property>
<name>fs.alluxio-ft.impl</name>
<value>alluxio.hadoop.FaultTolerantFileSystem</value>
<description>The Alluxio FileSystem (Hadoop 1.x and 2.x) with fault tolerant support</description>
</property>
<property>
<name>fs.AbstractFileSystem.alluxio.impl</name>
<value>alluxio.hadoop.AlluxioFileSystem</value>
<description>The Alluxio AbstractFileSystem (Hadoop 2.x)</description>
</property>
</configuration>
5. HDFS访问代码
只要将读写HDFS操作例子里面的代码中的HDFS地址修改成alluxio地址即可。
读取HDFS文件效果: