1 HDFSfile.java2 /************************************************************3 Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.4 FileName: HDFSfile.java5 Author: Light6 Version : version1.07 Date: 2018/7/168 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 23 Light 18/7/16 1.0 build this moudle24 ***********************************************************/
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 classHDFSfile {34 Configuration conf;35 FileSystem filesystem;36 String DEFNAME="fs.defaultFS";37 String HDFSURL="hdfs://192.168.72.10:9000";38 @Before39 public voidbefore() throws IOException {40 conf=newConfiguration();41 conf.set(DEFNAME, HDFSURL);42 filesystem=FileSystem.get(conf);43
44 }45
46 /**47 * junit测试函数48 * @throws IOException49 */
50 @Test51 public voidText() 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 IOException81 */
82 public voidmkdir(String path) throws IOException {83 //创建hdfs目录
84 if(filesystem.exists(newPath(path)))85 {86 System.out.println("目录已存在");87 }88 else
89 {90 boolean result=filesystem.mkdirs(newPath(path));91 System.out.println(result);92 }93
94 }95
96 /**97 * 创建文件98 * @param path hdfs文件地址(例:/hadoop/abc.txt)99 * @throws IOException100 */
101 public voidcreate(String path) throws IOException{102 //创建文件
103 if(filesystem.exists(newPath(path)))104 {105 System.out.println("文件已存在");106 }107 else
108 {109 FSDataOutputStream outputStream= filesystem.create(newPath(path));110 System.out.println("文件创建成功");111 }112 }113
114 /**115 * 查看文件内容116 * @param dst hdfs文件地址(例:/hadoop/abc.txt)117 * @throws IOException118 */
119 public voidread(String dst) throws IOException {120 if(filesystem.exists(newPath(dst)))121 {122 FSDataInputStream inputstream=filesystem.open(newPath(dst));123 InputStreamReader isr=newInputStreamReader(inputstream);124 BufferedReader br=newBufferedReader(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 voidmoveFile(String oldpath, String newpath) {146 Path path1 = newPath(oldpath);147 Path path2 = newPath(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 * 上传文件到hdfs167 * @param local168 * @param dst169 */
170 public voidputFile(String local, String dst) {171 try{172 //从本地将文件拷贝到HDFS中,如果目标文件已存在则进行覆盖
173 filesystem.copyFromLocalFile(new Path(local), newPath(dst));174 System.out.println("上传成功!");175 //关闭连接
176 } catch(IOException e) {177 System.out.println("上传失败!");178 e.printStackTrace();179 }180 }181
182 /**183 * 下载文件到本地184 * @param dst185 * @param local186 */
187 public voidgetFile(String dst, String local) {188 try{189 if (!filesystem.exists(newPath(dst))) {190 System.out.println("文件不存在!");191 } else{192 filesystem.copyToLocalFile(new Path(dst), newPath(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 dst205 */
206 public voidlistStatus(String dst) {207 try{208 if (!filesystem.exists(newPath(dst))) {209 System.out.println("目录不存在!");210 return;211 }212 //得到文件的状态
213 FileStatus[] status = filesystem.listStatus(newPath(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 dst227 */
228 public voiddeleteFile(String dst) {229 try{230 if (!filesystem.exists(newPath(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 * 关闭filesyatem245 */
246 @After247 public voiddestory()248 {249 try{250 filesystem.close();251 } catch(IOException e) {252 e.printStackTrace();253 }254 }255 }