设为首页 收藏本站
查看: 1265|回复: 0

[经验分享] Hadoop HDFS编程 API入门系列之HdfsUtil版本2(七)

[复制链接]

尚未签到

发表于 2017-12-18 11:42:12 | 显示全部楼层 |阅读模式
1 import org.junit.Before;  

  2 import org.junit.Test;  

  3  
  4 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs1;
  
  5
  
  6 import java.io.FileInputStream;
  
  7 import java.io.FileNotFoundException;
  
  8 import java.io.FileOutputStream;
  
  9 import java.io.IOException;
  
10 import java.net.URI;
  
11
  
12 import org.apache.commons.io.IOUtils;
  
13 import org.apache.hadoop.conf.Configuration;
  
14 import org.apache.hadoop.fs.FSDataInputStream;
  
15 import org.apache.hadoop.fs.FSDataOutputStream;
  
16 import org.apache.hadoop.fs.FileStatus;
  
17 import org.apache.hadoop.fs.FileSystem;
  
18 import org.apache.hadoop.fs.LocatedFileStatus;
  
19 import org.apache.hadoop.fs.Path;
  
20 import org.apache.hadoop.fs.RemoteIterator;
  
21 import org.junit.Before;
  
22 import org.junit.Test;
  
23

  
24 public>  
25     
  
26     FileSystem fs = null;
  
27
  
28     
  
29     @Before//@Before是在所拦截单元测试方法执行之前执行一段逻辑,读艾特Before
  
30     public void init() throws Exception{
  
31         
  
32         //读取classpath下的xxx-site.xml 配置文件,并解析其内容,封装到conf对象中
  
33         Configuration conf = new Configuration();
  
34         
  
35         //也可以在代码中对conf中的配置信息进行手动设置,会覆盖掉配置文件中的读取的值
  
36         conf.set("fs.defaultFS", "hdfs://HadoopMaster:9000/");
  
37         
  
38         //根据配置信息,去获取一个具体文件系统的客户端操作实例对象
  
39         fs = FileSystem.get(new URI("hdfs://HadoopMaster:9000/"),conf,"hadoop");
  
40         
  
41         
  
42     }
  
43     
  
44     
  
45     
  
46     /**
  
47      * 上传文件,比较底层的写法
  
48      *
  
49      * @throws Exception
  
50      */
  
51     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
52     public void upload() throws Exception {
  
53
  
54         Configuration conf = new Configuration();
  
55         conf.set("fs.defaultFS", "hdfs://HadoopMaster:9000/");
  
56         
  
57         FileSystem fs = FileSystem.get(conf);
  
58         
  
59         Path dst = new Path("hdfs://HadoopMaster:9000/aa/qingshu.txt");
  
60         
  
61         FSDataOutputStream os = fs.create(dst);
  
62         
  
63         FileInputStream is = new FileInputStream("c:/qingshu.txt");
  
64         
  
65         IOUtils.copy(is, os);
  
66         
  
67
  
68     }
  
69
  
70     /**
  
71      * 上传文件,封装好的写法
  
72      * @throws Exception
  
73      * @throws IOException
  
74      */
  
75     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
76     public void upload2() throws Exception, IOException{
  
77         
  
78         fs.copyFromLocalFile(new Path("c:/qingshu.txt"), new Path("hdfs://HadoopMaster:9000/aaa/bbb/ccc/qingshu2.txt"));
  
79         
  
80     }
  
81     
  
82     
  
83     /**
  
84      * 下载文件
  
85      * @throws Exception
  
86      * @throws IllegalArgumentException
  
87      */
  
88     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
89     public void download() throws Exception {
  
90         
  
91         fs.copyToLocalFile(new Path("hdfs://HadoopMaster:9000/aa/qingshu2.txt"), new Path("c:/qingshu2.txt"));
  
92
  
93     }
  
94
  
95     /**
  
96      * 查看文件信息
  
97      * @throws IOException
  
98      * @throws IllegalArgumentException
  
99      * @throws FileNotFoundException
  
100      *
  
101      */
  
102     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
103     public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
  
104
  
105         // listFiles列出的是文件信息,而且提供递归遍历
  
106         RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
  
107         
  
108         while(files.hasNext()){
  
109            
  
110             LocatedFileStatus file = files.next();
  
111             Path filePath = file.getPath();
  
112             String fileName = filePath.getName();
  
113             System.out.println(fileName);
  
114            
  
115         }
  
116         
  
117         System.out.println("---------------------------------");
  
118         
  
119         //listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
  
120         FileStatus[] listStatus = fs.listStatus(new Path("/"));
  
121         for(FileStatus status: listStatus){
  
122            
  
123             String name = status.getPath().getName();
  
124             System.out.println(name + (status.isDirectory()?" is dir":" is file"));
  
125            
  
126         }
  
127         
  
128     }
  
129
  
130     /**
  
131      * 创建文件夹
  
132      * @throws Exception
  
133      * @throws IllegalArgumentException
  
134      */
  
135     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
136     public void mkdir() throws IllegalArgumentException, Exception {
  
137
  
138         fs.mkdirs(new Path("/aaa/bbb/ccc"));
  
139         
  
140         
  
141     }
  
142
  
143     /**
  
144      * 删除文件或文件夹
  
145      * @throws IOException
  
146      * @throws IllegalArgumentException
  
147      */
  
148     @Test//@Test是测试方法提示符,一般与@Before组合使用
  
149     public void rm() throws IllegalArgumentException, IOException {
  
150
  
151         fs.delete(new Path("/aa"), true);
  
152         
  
153     }
  
154
  
155     
  
156     public static void main(String[] args) throws Exception {
  
157
  
158         Configuration conf = new Configuration();
  
159         conf.set("fs.defaultFS", "hdfs://HadoopMaster:9000/");
  
160         
  
161         FileSystem fs = FileSystem.get(conf);
  
162         
  
163         FSDataInputStream is = fs.open(new Path("/jdk-7u65-linux-i586.tar.gz"));
  
164         
  
165         FileOutputStream os = new FileOutputStream("c:/jdk7.tgz");
  
166         
  
167         IOUtils.copy(is, os);
  
168     }
  
169     
  
170     
  
171     
  
172 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425343-1-1.html 上篇帖子: Hadoop MapReduce编程 API入门系列之wordcount版本1(五) 下篇帖子: Hadoop 三大调度器源码分析及编写自己的调度器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表