学习用java进行的常用hdfs操作

参考:

[b0002] Hadoop HDFS cmd常用命令练手

 

环境:

hadoop2.6.4

win7 下的eclipse环境调试已经配置好,参考前面的文章

 

代码:

1. 创建文件夹

 1 package hdfs;
 2 
 3 import java.io.IOException;
 4 
 5 import org.apache.hadoop.conf.Configuration;
 6 import org.apache.hadoop.fs.FileSystem;
 7 import org.apache.hadoop.fs.Path;
 8 
 9 /**
10  * 
11  * @author Administrator
12  *  创建文件夹,如果不存在
13  */
14 public class CreateFolder {
15     
16     public static void main(String[] args) throws IOException {
17         Configuration conf =new Configuration();
18         conf.set("fs.defaultFS", "hdfs://ssmaster:9000/");
19         FileSystem fs =  FileSystem.get(conf) ;
20         Path path = new Path("/output");
21         
22         if(! fs.exists(path)){
23             fs.mkdirs(path);
24         }
25     }
26 }

 

以流的方式下载文件

 1 package hdfs;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 import org.apache.commons.compress.utils.IOUtils;
 8 import org.apache.hadoop.conf.Configuration;
 9 import org.apache.hadoop.fs.FSDataInputStream;
10 import org.apache.hadoop.fs.FileSystem;
11 import org.apache.hadoop.fs.Path;
12 
13 /**
14  *  功能:      将 hdfs://ssmaster:9000/data/paper.txt下载到Windows下c:\paper.txt
15  *  调用方式:windows下执行,eclipse中执行
16  */
17 
18 public class Down_Load {
19 
20     public static void main(String[] args) {
21         
22     Configuration conf =new Configuration();
23     conf.set("fs.defaultFS", "hdfs://ssmaster:9000/");
24     
25     FileSystem fs = null;
26     Path src = null;
27     FSDataInputStream in = null;
28     FileOutputStream out = null;
29       
30     src = new Path("hdfs://ssmaster:9000/data/paper.txt" );
31     
32     try {
33         
34       fs = FileSystem.get(conf) ;
35       in = fs.open(src);
36 
37        } catch (IOException e) {
38         e.printStackTrace(); 
39     }
40     
41     try {
42         out = new FileOutputStream ("c:\\paper.txt"); //等效  c:/paper.txt
43     } catch (FileNotFoundException e) {
44         e.printStackTrace();
45     }
46     
47     try {
48         IOUtils.copy(in, out);
49     } catch (IOException e) {
50         e.printStackTrace();
51     }
52 
53 }
54 }
View Code

相关文章:

  • 2021-12-20
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2021-11-12
  • 2022-12-23
  • 2021-07-10
  • 2021-07-12
  • 2021-11-04
  • 2021-11-24
  • 2022-01-14
相关资源
相似解决方案