1 packagehdfs;2
3 importorg.apache.commons.lang.StringUtils;4 importorg.apache.hadoop.conf.Configuration;5 importorg.apache.hadoop.fs.FileSystem;6 importorg.apache.hadoop.fs.Path;7 importorg.apache.hadoop.io.IOUtils;8
9 import java.io.*;10 importjava.net.URI;11
12 public classup2hdfs {13
14
15 private static String HDFSUri = "hdfs://IP+:+端口号";16
17 /**
18 * 1、获取文件系统19 *20 * @retrun FileSystem 文件系统21 */
22
23 public staticFileSystem getFileSystem(){24
25 //读取配置文件
26 Configuration conf = newConfiguration();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 = newURI(hdfsUri.trim());42 fs =FileSystem.get(uri,conf);43 } catch(Exception e) {44 e.printStackTrace();45 }46 }47 returnfs ;48 }49
50
51 /**
52 * 2、创建文件目录53 *@parampath 文件路径54 */
55 public static voidmkdir(String path){56
57 try{58 FileSystem fs =getFileSystem();59 System.out.println("FilePath"+path);60 //创建目录
61 fs.mkdirs(newPath(path));62 //释放资源
63 fs.close();64 } catch(IOException e) {65 e.printStackTrace();66 }67 }68
69 /**
70 * 3、判断目录是否存在71 *72 *@paramfilePath 目录路径73 *@paramcreate 若不存在是否创建74 *75 */
76 public static boolean existDir(String filePath,booleancreate){77
78 boolean flag = false;79
80 if(StringUtils.isNotEmpty(filePath)){81 returnflag;82 }83
84 try{85 Path path = newPath(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 returnflag;104
105 }106
107 /**
108 * 4、本地文件上传至HDFS109 *110 *@paramsrcFile 源文件路径111 *@paramdestPath 目的文件路径112 */
113
114 public static void copyFileToHDFS(String srcFile,String destPath) throwsException{115
116 FileInputStream fis = new FileInputStream(new File(srcFile));//读取本地文件
117 Configuration config = newConfiguration();118 FileSystem fs = FileSystem.get(URI.create(HDFSUri+destPath),config);119 OutputStream os = fs.create(newPath(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 *@paramsrcFile 源文件路径131 *@paramdestPath 目的文件路径132 *133 */
134 public static void getFile(String srcFile,String destPath)throwsException{135
136 //HDFS文件地址
137 String file = HDFSUri+srcFile;138 Configuration config = newConfiguration();139 //构建filesystem
140 FileSystem fs =FileSystem.get(URI.create(file),config);141 //读取文件
142 InputStream is = fs.open(newPath(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 *@parampath152 */
153 public static voidrmdir(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 *@paramfilePath179 *@throwsIOException180 */
181 public static void readFile(String filePath)throwsIOException{182
183 Configuration config = newConfiguration();184 String file = HDFSUri+filePath;185 FileSystem fs =FileSystem.get(URI.create(file),config);186 //读取文件
187 InputStream is =fs.open(newPath(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) throwsException {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 }