IDEA項目遠程調試hadoop入門(maven項目)

一、建立項目:File-->new-->Project;java

二、選擇maven,SDK選擇本身的Java安裝路徑;apache

三、這個隨意了,寫個比較有意義的就行,而後就按照圖片操做。網絡

四、上圖點擊finish後,出現下面的pom.xml,這個就是後續須要mvn依賴的地方。maven

五、個人hadoop版本是:CDH的2.6.0-cdh5.5.0,因此在mvn下對應的版本,將內容複製出來粘貼進pom.xml;oop

這個須要特別注意,不一樣的版本這個也是不一樣的;測試

 六、點擊出現的import,下圖是點擊先後點擊後lib中差別,若是導入成功的話就以下圖第二個。spa

 七、最後將hadoop集羣中core-site.xml配置文件複製到項目的resource下;.net

 

八、最後建包,建類;3d

 

九、最後,上代碼;code

  1 package hdfs;
  2 
  3 import org.apache.commons.lang.StringUtils;
  4 import org.apache.hadoop.conf.Configuration;
  5 import org.apache.hadoop.fs.FileSystem;
  6 import org.apache.hadoop.fs.Path;
  7 import org.apache.hadoop.io.IOUtils;
  8 
  9 import java.io.*;
 10 import java.net.URI;
 11 
 12 public class up2hdfs {
 13 
 14 
 15     private static String HDFSUri = "hdfs://IP+:+端口號";
 16 
 17     /**
 18      * 一、獲取文件系統
 19      *
 20      * @retrun FileSystem 文件系統
 21      */
 22 
 23     public static FileSystem getFileSystem(){
 24 
 25         //讀取配置文件
 26         Configuration conf = new Configuration();
 27 
 28         //文件系統
 29         FileSystem fs = null;
 30         String hdfsUri = HDFSUri;
 31         if (StringUtils.isBlank(hdfsUri)){
 32             //返回默認文件系統,若是在hadoop集羣下運行,使用此種方法可直接獲取默認文件系統;
 33             try{
 34                 fs = FileSystem.get(conf);
 35             }catch(IOException e){
 36                 e.printStackTrace();
 37             }
 38         }else{
 39             //返回指定的文件系統,若是在本地測試,須要此種方法獲取文件系統;
 40             try{
 41                 URI uri = new URI(hdfsUri.trim());
 42                 fs = FileSystem.get(uri,conf);
 43             } catch (Exception e) {
 44                 e.printStackTrace();
 45             }
 46         }
 47         return fs ;
 48     }
 49 
 50 
 51     /**
 52      * 二、建立文件目錄
 53      * @param path 文件路徑
 54      */
 55     public static void mkdir(String path){
 56 
 57         try {
 58             FileSystem fs = getFileSystem();
 59             System.out.println("FilePath"+path);
 60             //建立目錄
 61             fs.mkdirs(new Path(path));
 62             //釋放資源
 63             fs.close();
 64         } catch (IOException e) {
 65             e.printStackTrace();
 66         }
 67     }
 68 
 69     /**
 70      * 三、判斷目錄是否存在
 71      *
 72      * @param filePath 目錄路徑
 73      * @param create 若不存在是否建立
 74      *
 75      */
 76     public static boolean existDir(String filePath,boolean create){
 77 
 78         boolean flag = false;
 79 
 80         if (StringUtils.isNotEmpty(filePath)){
 81             return flag;
 82         }
 83 
 84         try{
 85             Path path = new Path(filePath);
 86             //FileSystem對象
 87             FileSystem fs = getFileSystem();
 88             if (create){
 89                 if (!fs.exists(path)){
 90                     fs.mkdirs(path);
 91                 }
 92             }
 93 
 94             if (fs.isDirectory(path)){
 95                 flag = true;
 96             }
 97 
 98         }catch (Exception e){
 99             e.printStackTrace();
100 
101         }
102 
103         return flag;
104 
105     }
106 
107     /**
108      * 四、本地文件上傳至HDFS
109      *
110      * @param srcFile 源文件路徑
111      * @param destPath 目的文件路徑
112      */
113 
114     public static void copyFileToHDFS(String srcFile,String destPath) throws Exception{
115 
116         FileInputStream fis = new FileInputStream(new File(srcFile));//讀取本地文件
117         Configuration config = new Configuration();
118         FileSystem fs = FileSystem.get(URI.create(HDFSUri+destPath),config);
119         OutputStream os = fs.create(new Path(destPath));
120         //cpoy
121         IOUtils.copyBytes(fis,os,4096,true);
122 
123         System.out.println("copy 完成 ......");
124         fs.close();
125     }
126 
127     /**
128      * 五、從HDFS下載文件到本地
129      *
130      * @param srcFile 源文件路徑
131      * @param destPath 目的文件路徑
132      *
133      */
134     public static void getFile(String srcFile,String destPath)throws Exception{
135 
136         //HDFS文件地址
137         String file = HDFSUri+srcFile;
138         Configuration config = new Configuration();
139         //構建filesystem
140         FileSystem fs = FileSystem.get(URI.create(file),config);
141         //讀取文件
142         InputStream is = fs.open(new Path(file));
143         IOUtils.copyBytes(is,new FileOutputStream(new File(destPath)),2048,true);
144         System.out.println("下載完成......");
145         fs.close();
146     }
147 
148     /**
149      * 六、刪除文件或者文件目錄
150      *
151      * @param path
152      */
153     public static void rmdir(String path){
154 
155         try {
156             //返回FileSystem對象
157             FileSystem fs = getFileSystem();
158 
159             String hdfsUri = HDFSUri;
160             if (StringUtils.isNotBlank(hdfsUri)){
161 
162                 path = hdfsUri+path;
163             }
164             System.out.println("path"+path);
165             //刪除文件或者文件目錄 delete(Path f)此方法已經棄用
166             System.out.println(fs.delete(new Path(path),true));
167 
168             fs.close();
169         } catch (Exception e) {
170             e.printStackTrace();
171         }
172 
173     }
174 
175     /**
176      * 七、讀取文件的內容
177      *
178      * @param filePath
179      * @throws IOException
180      */
181     public static void readFile(String filePath)throws IOException{
182 
183         Configuration config = new Configuration();
184         String file = HDFSUri+filePath;
185         FileSystem fs = FileSystem.get(URI.create(file),config);
186         //讀取文件
187         InputStream is =fs.open(new Path(file));
188         //讀取文件
189         IOUtils.copyBytes(is, System.out, 2048, false); //複製到標準輸出流
190         fs.close();
191     }
192 
193 
194     /**
195      * 主方法測試
196      */
197     public static void main(String[] args) throws Exception {200         //鏈接fs
201         FileSystem fs = getFileSystem();
202         System.out.println(fs.getUsed());
203         //建立路徑
204         mkdir("/dit2");
205         //驗證是否存在
206         System.out.println(existDir("/dit2",false));
207         //上傳文件到HDFS
208         copyFileToHDFS("G:\\testFile\\HDFSTest.txt","/dit/HDFSTest.txt");
209         //下載文件到本地
210         getFile("/dit/HDFSTest.txt","G:\\HDFSTest.txt");
211         // getFile(HDFSFile,localFile);
212         //刪除文件
213         rmdir("/dit2");
214         //讀取文件
215         readFile("/dit/HDFSTest.txt");
216     }
217 
218 }

 以上代碼,來自網絡,忘記出處,若有侵權,當即刪!

相關文章
相關標籤/搜索