因为HDFS不同于一般的文件系统,所以Hadoop提供了强大的FileSystem API来操作HDFS.
核心类是FSDataInputStream和FSDataOutputStream
读操作:
我们用FSDataInputStream来读取HDFS中的指定文件(第一个实验),另外我们还演示了这个类的定位文件位置的能力,然后从指定位置开始读取文件(第二个实验)。
代码如下:
-
-
- package com.charles.hadoop.fs;
-
-
- import java.net.URI;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
-
-
-
-
-
-
-
-
-
-
- public class ReadFromHadoopFileSystem {
-
-
-
-
- public static void main(String[] args) throws Exception{
-
-
-
- String uri = args[0];
-
- Configuration conf = new Configuration();
- conf.set("hadoop.job.ugi", "hadoop-user,hadoop-user");
-
-
- FileSystem fs = FileSystem.get(URI.create(uri),conf);
- FSDataInputStream in = null;
- try{
-
- System.out.println("实验一:输出全部文件内容");
-
- in = fs.open( new Path(uri) );
-
- IOUtils.copyBytes(in, System.out,50,false);
- System.out.println();
-
-
-
- System.out.println("实验二:展示FSDataInputStream文件输入流的流定位能力,用seek进行定位");
-
-
-
- for (int i=1;i<=3;i++){
- in.seek(0+20*(i-1));
- System.out.println("流定位第 "+i+" 次:" );
- IOUtils.copyBytes(in, System.out,4096,false);
- }
- }finally{
- IOUtils.closeStream(in);
- }
-
- }
-
- }
我们传入的命令行参数为我们要读的HDFS文件系统中某文件的URI:
- hdfs://192.168.129.35:9000/user/hadoop-user/textfile.txt
最终输出结果为:
- 实验一:输出全部文件内容
- This is a text file edited by charles to test the hadoop distributed file system's features.
-
- 实验二:展示FSDataInputStream文件输入流的流定位能力,用seek进行定位
- 流定位第 1 次:
- This is a text file edited by charles to test the hadoop distributed file system's features.
- 流定位第 2 次:
- edited by charles to test the hadoop distributed file system's features.
- 流定位第 3 次:
写操作:
我们用FSDataOutputStream来写文件到HDFS系统中,或者说从本地文件系统中复制文件到HDFS文件系统中。其中这个本地文件系统是相对于运行这段java代码的宿主系统。
代码如下:
-
-
- package com.charles.hadoop.fs;
-
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URI;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IOUtils;
- import org.apache.hadoop.util.Progressable;
-
-
-
-
-
-
-
-
-
- public class WriteToHadoopFileSystem {
-
-
-
-
- public static void main(String[] args)throws Exception{
-
-
-
-
-
- String localSrc = args[0];
- String dst= args[1];
-
-
- InputStream in = new BufferedInputStream( new FileInputStream(localSrc));
-
-
- Configuration conf = new Configuration();
- conf.set("hadoop.job.ugi", "hadoop-user,hadoop-user");
-
-
-
-
- FileSystem fs = FileSystem.get(URI.create(dst), conf);
-
- OutputStream out = fs.create(new Path(dst) );
-
- IOUtils.copyBytes(in, out, 4096,true);
-
- System.out.println("复制完成");
-
- }
-
- }
我们传入2个命令行参数,一个是本地文件系统中被复制的文件路径,第二个要复制到的HDFS文件系统中的目标文件路径:
- copyMe.txt
- hdfs://192.168.129.35:9000/user/hadoop-user/copyMe.txt
我们去文件系统中去检查文件,果然文件被复制上去了:
![]()
打开这个目标文件,果然内容与预期一样:
![]()
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/878921,如需转载请自行联系原作者