java-Hadoop fileSystem API实现对文件的操作

我们可以通过hadoop中的fileSystem API进行文件的操作,在获取Hadoop的fileSystem后就可以实现操作方法的封装,现代码及注释如下:

  1 HDFSfile.java
  2 /************************************************************   
  3  Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   
  4  FileName: HDFSfile.java 
  5  Author: Light     
  6  Version : version1.0      
  7  Date: 2018/7/16
  8  Description:以通过hadoop中的fileSystem API进行文件的操作// 模块描述         
  9  Version:         // 版本信息 
 10     实现了对hdfs文件的大部分操作
 11  Function List:     // 主要函数及其功能     
 12  1 创建目录mkdir("/idea/");
 13  2.创建文件create("/idea/haha.txt");
 14  3.查看hdfs文件内容read("/idea/text.txt");
 15  4文件重命名moveFile("/idea/haha.txt","/idea/hello.txt");
 16  5.上传文件putFile("G://text.txt","/idea/");
 17  6.下载文件getFile("/idea/abc.txt","G://");
 18  7.查询目录下的所有文件listStatus("/idea/");
 19  8.删除文件deleteFile("/idea/hello.txt");
 20   History: 
 21    // 历史修改记录 
 22  <author>  <time>   <version >   <desc>       
 23  Light    18/7/16     1.0     build this moudle   
 24  ***********************************************************/
 25 import org.apache.hadoop.conf.Configuration;
 26 import org.apache.hadoop.fs.*;
 27 import org.junit.After;
 28 import org.junit.Before;
 29 import org.junit.Test;
 30 
 31 import java.io.*;
 32 
 33 public class HDFSfile {
 34     Configuration conf;
 35     FileSystem filesystem;
 36     String DEFNAME="fs.defaultFS";
 37     String HDFSURL="hdfs://192.168.72.10:9000";
 38     @Before
 39     public void before() throws IOException {
 40         conf=new Configuration();
 41         conf.set(DEFNAME, HDFSURL);
 42         filesystem=FileSystem.get(conf);
 43 
 44     }
 45 
 46     /**
 47      * junit测试函数
 48      * @throws IOException
 49      */
 50     @Test
 51     public void Text() throws IOException {
 52         //创建目录
 53         //mkdir("/idea/");
 54 
 55         //创建文件
 56         //create("/idea/haha.txt");
 57 
 58         //查看hdfs文件内容
 59         //read("/idea/text.txt");
 60 
 61         //文件重命名
 62         //moveFile("/idea/haha.txt","/idea/hello.txt");
 63 
 64         //上传文件
 65         //putFile("G://text.txt","/idea/");
 66 
 67         //下载文件
 68         //getFile("/idea/abc.txt","G://");
 69 
 70         //查询目录下的所有文件
 71         //listStatus("/idea/");
 72 
 73         //删除文件
 74         //deleteFile("/idea/hello.txt");
 75     }
 76 
 77     /**
 78      * 创建目录
 79      * @param path 创建目录的地址(例:/hadoop/)
 80      * @throws IOException
 81      */
 82     public void mkdir(String path) throws IOException {
 83         //创建hdfs目录
 84         if(filesystem.exists(new Path(path)))
 85         {
 86             System.out.println("目录已存在");
 87         }
 88         else
 89         {
 90             boolean result=filesystem.mkdirs(new Path(path));
 91             System.out.println(result);
 92         }
 93 
 94     }
 95 
 96     /**
 97      * 创建文件
 98      * @param path hdfs文件地址(例:/hadoop/abc.txt)
 99      * @throws IOException
100      */
101     public  void create(String path) throws IOException{
102         //创建文件
103         if(filesystem.exists(new Path(path)))
104         {
105             System.out.println("文件已存在");
106         }
107         else
108         {
109             FSDataOutputStream outputStream=  filesystem.create(new Path(path));
110             System.out.println("文件创建成功");
111         }
112     }
113 
114     /**
115      * 查看文件内容
116      * @param dst hdfs文件地址(例:/hadoop/abc.txt)
117      * @throws IOException
118      */
119     public void read(String dst) throws IOException {
120         if(filesystem.exists(new Path(dst)))
121         {
122             FSDataInputStream inputstream=filesystem.open(new Path(dst));
123             InputStreamReader isr=new InputStreamReader(inputstream);
124             BufferedReader br=new BufferedReader(isr);
125             String str=br.readLine();
126             while(str!=null){
127                 System.out.println(str);
128                 str=br.readLine();
129             }
130             br.close();
131             isr.close();
132             inputstream.close();
133         }
134        else
135         {
136             System.out.println("文件不存在");
137         }
138     }
139 
140     /**
141      * 将dst1重命名为dst2,也可以进行文件的移动
142      * @param oldpath 旧名
143      * @param newpath 新名
144      */
145     public void moveFile(String oldpath, String newpath) {
146         Path path1 = new Path(oldpath);
147         Path path2 = new Path(newpath);
148         try {
149             if (!filesystem.exists(path1)) {
150                 System.out.println(oldpath + " 文件不存在!");
151                 return;
152             }
153             if (filesystem.exists(path2)) {
154                 System.out.println(newpath + "已存在!");
155                 return;
156             }
157             // 将文件进行重命名,可以起到移动文件的作用
158             filesystem.rename(path1, path2);
159             System.out.println("文件已重命名!");
160         } catch (IOException e) {
161             e.printStackTrace();
162         }
163     }
164 
165     /**
166      * 上传文件到hdfs
167      * @param local
168      * @param dst
169      */
170     public void putFile(String local, String dst) {
171         try {
172             // 从本地将文件拷贝到HDFS中,如果目标文件已存在则进行覆盖
173             filesystem.copyFromLocalFile(new Path(local), new Path(dst));
174             System.out.println("上传成功!");
175             // 关闭连接
176         } catch (IOException e) {
177             System.out.println("上传失败!");
178             e.printStackTrace();
179         }
180     }
181 
182     /**
183      * 下载文件到本地
184      * @param dst
185      * @param local
186      */
187     public void getFile(String dst, String local) {
188         try {
189             if (!filesystem.exists(new Path(dst))) {
190                 System.out.println("文件不存在!");
191             } else {
192                 filesystem.copyToLocalFile(new Path(dst), new Path(local));
193                 System.out.println("下载成功!");
194             }
195         } catch (IOException e) {
196             System.out.println("下载失败!");
197             e.printStackTrace();
198         }
199     }
200 
201 
202     /**
203      * 显示目录下所有文件
204      * @param dst
205      */
206     public void listStatus(String dst) {
207         try {
208             if (!filesystem.exists(new Path(dst))) {
209                 System.out.println("目录不存在!");
210                 return;
211             }
212             // 得到文件的状态
213             FileStatus[] status = filesystem.listStatus(new Path(dst));
214             for (FileStatus s : status) {
215                 System.out.println(s.getPath().getName());
216             }
217 
218         } catch (IllegalArgumentException | IOException e) {
219             // TODO Auto-generated catch block
220             e.printStackTrace();
221         }
222     }
223 
224     /**
225      * 删除hdfs中的文件
226      * @param dst
227      */
228     public void deleteFile(String dst) {
229         try {
230             if (!filesystem.exists(new Path(dst))) {
231                 System.out.println("文件不存在!");
232             } else {
233                 filesystem.delete(new Path(dst), true);
234                 System.out.println("删除成功!");
235             }
236         } catch (IOException e) {
237             System.out.println("删除失败!");
238             e.printStackTrace();
239         }
240     }
241 
242 
243     /**
244      * 关闭filesyatem
245      */
246     @After
247     public void destory()
248     {
249         try {
250             filesystem.close();
251         } catch (IOException e) {
252             e.printStackTrace();
253         }
254     }
255 }

猜你喜欢

转载自www.cnblogs.com/suifengye/p/9319386.html