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


1、創建項目:File-->new-->Project;

2、選擇maven,SDK選擇自己的Java安裝路徑;

3、這個隨意了,寫個比較有意義的就行,然后就按照圖片操作。

4、上圖點擊finish后,出現下面的pom.xml,這個就是后續需要mvn依賴的地方。

5、我的hadoop版本是:CDH的2.6.0-cdh5.5.0,所以在mvn下對應的版本,將內容復制出來粘貼進pom.xml;

這個需要特別注意,不同的版本這個也是不同的;

 6、點擊出現的import,下圖是點擊前后點擊后lib中差異,如果導入成功的話就如下圖第二個。

 7、最后將hadoop集群中core-site.xml配置文件復制到項目的resource下;

 

8、最后建包,建類;

 

9、最后,上代碼;

  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      * 1、獲取文件系統
 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      * 2、創建文件目錄
 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      * 3、判斷目錄是否存在
 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      * 4、本地文件上傳至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      * 5、從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      * 6、刪除文件或者文件目錄
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      * 7、讀取文件的內容
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 }

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

 


免責聲明!

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



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