JAVA操作HDFS案例的简单实现

Jar包引入,pom.xml:

[html] view plain copy
  1. <dependency>  
  2.   <groupId>org.apache.hadoop</groupId>  
  3.   <artifactId>hadoop-common</artifactId>  
  4.   <version>2.8.0</version>  
  5. </dependency>  
  6. <dependency>  
  7.   <groupId>org.apache.hadoop</groupId>  
  8.   <artifactId>hadoop-hdfs</artifactId>  
  9.   <version>2.8.0</version>  
  10. </dependency>  

将本地文件上传到hdfs服务器:

[java] view plain copy
  1. /** 
  2.  * 上传文件到hdfs上 
  3.  */  
  4. @Test  
  5. public void upload() throws IOException {  
  6.     Configuration conf = new Configuration();  
  7.     conf.set("fs.defaultFS","hdfs://hzq:9000");  
  8.     FileSystem fs = FileSystem.get(conf);  
  9.     fs.copyFromLocalFile(new Path("/home/hzq/jdk1.8.tar.gz"),new Path("/demo"));  
  10. }  
解析:

      在开发中我没有引入“core-site.xml”配置文件,所以在本地调用时使用conf进行配置“conf.set("fs.defaultFS","hdfs://hzq:9000");“,下面雷同。

将hdfs上文件下载到本地:

[java] view plain copy
  1. /** 
  2.  * 将hdfs上文件下载到本地 
  3.  */  
  4. @Test  
  5. public void download() throws IOException {  
  6.     Configuration conf = new Configuration();  
  7.     conf.set("fs.defaultFS","hdfs://hzq:9000");  
  8.     FileSystem fs = FileSystem.newInstance(conf);  
  9.     fs.copyToLocalFile(new Path("/java/jdk1.8.tar.gz"),new Path("/home/hzq/"));  
  10. }  
删除hdfs上指定文件:

[java] view plain copy
  1. /** 
  2.  * 删除hdfs上的文件 
  3.  * @throws IOException 
  4.  */  
  5. @Test  
  6. public void removeFile() throws IOException {  
  7.     Configuration conf = new Configuration();  
  8.     conf.set("fs.defaultFS","hdfs://hzq:9000");  
  9.     FileSystem fs = FileSystem.newInstance(conf);  
  10.     fs.delete(new Path("/demo/jdk1.8.tar.gz"),true);  
  11. }  
在hdfs上创建文件夹:
[java] view plain copy
  1. /** 
  2.  * 在hdfs更目录下面创建test1文件夹 
  3.  * @throws IOException 
  4.  */  
  5. @Test  
  6. public void mkdir() throws IOException {  
  7.     Configuration conf = new Configuration();  
  8.     conf.set("fs.defaultFS","hdfs://hzq:9000");  
  9.     FileSystem fs = FileSystem.newInstance(conf);  
  10.     fs.mkdirs(new Path("/test1"));  
  11. }  
列出hdfs上所有的文件或文件夹:

[java] view plain copy
  1. @Test  
  2.    public void listFiles() throws IOException {  
  3.        Configuration conf = new Configuration();  
  4.        conf.set("fs.defaultFS","hdfs://hzq:9000");  
  5.        FileSystem fs = FileSystem.newInstance(conf);  
  6.        // true 表示递归查找  false 不进行递归查找  
  7.        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/"), true);  
  8.        while (iterator.hasNext()){  
  9.            LocatedFileStatus next = iterator.next();  
  10.            System.out.println(next.getPath());  
  11.        }  
  12.        System.out.println("----------------------------------------------------------");  
  13.        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));  
  14.        for (int i = 0; i < fileStatuses.length; i++) {  
  15.            FileStatus fileStatus = fileStatuses[i];  
  16.            System.out.println(fileStatus.getPath());  
  17.        }  
  18.    }  
运行结果:

                                     JAVA操作HDFS案例的简单实现

结果分析:

       “listFiles“列出的是hdfs上所有文件的路径,不包括文件夹。根据你的设置,支持递归查找。

       ”listStatus“列出的是所有的文件和文件夹,不支持递归查找。如许递归,需要自己实现。