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 }

 以上代码,来自网络,忘记出处,如有侵权,立即删!

 

posted @ 2017-11-23 11:45  KevinZhyp  阅读(8193)  评论(0编辑  收藏  举报