java-HDFS 利用 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM