这里列出了基本操作,其它操作都类似:
注意:例子中的操作可能出现没有hadoop操作权限,在网上搜了一下,一种是dfs.permissions为false,另一种执行命令:$ hadoop fs -chmod 777 /user/hadoop,但本人觉得实际应用这几种方式不可取,具体权限问题以后再研究。
废话少说直接上代码:
读取HDFS文件:
1 /** 2 * 读取HDFS文件 3 * 4 * @author: wyf 5 * @version: Jul 10, 2013 3:59:12 PM 6 */ 7 @Test 8 public void readHFDS() { 9 FSDataInputStream open = null;10 try {11 //############################################################################################12 //此处的IP不可指定为locahost,直接指定Master Ip(由于本机为双IP,在单IP情况下没有测试,单IP的朋友可以自行测试)13 //############################################################################################14 String uri = "hdfs://192.168.1.15:9000/hadoopcase/maxtemprature/input/data.txt";15 Configuration conf = new Configuration();16 FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);17 open = fileSystem.open(new Path(uri));18 IOUtils.copyBytes(open, System.out, conf);19 20 } catch (Exception e) {21 e.printStackTrace();22 } finally {23 if(null != open) {24 IOUtils.closeStream(open);25 }26 }27 }
读取HDFS目录:
1 /** 2 * 读取目录 3 * 4 * @author: wyf 5 * @version: Jul 10, 2013 4:00:54 PM 6 */ 7 @Test 8 public void readDirectory() { 9 try {10 11 String uri = "hdfs://192.168.1.15:9000/";12 Configuration conf = new Configuration();13 FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);14 FileStatus[] listStatus = fileSystem.listStatus(new Path(uri));15 loopDirectory(fileSystem, listStatus);16 17 } catch (Exception e) {18 e.printStackTrace();19 }20 }21 22 23 /**24 * 递归获取目录25 * 26 * @author: wyf27 * @version: Jul 10, 2013 4:16:42 PM28 */29 protected void loopDirectory(FileSystem fileSystem, FileStatus[] listStatus) throws IOException {30 for(FileStatus fs : listStatus) {31 Path path = fs.getPath();32 33 //#############################################34 //此步骤可省略,属于打印格式35 //#############################################36 int depth = path.depth();37 StringBuilder sb = new StringBuilder();38 for(int i=0; i< depth; i++) {39 sb.append("--");40 }41 42 43 System.out.println(sb + path.getName());44 if(fs.isDir()) {45 FileStatus[] listStatus2 = fileSystem.listStatus(fs.getPath());46 loopDirectory(fileSystem, listStatus2);47 }48 }49 }
上传文件到HDFS:
1 /** 2 * 上传文件 3 * 4 * @author: wyf 5 * @version: Jul 10, 2013 4:24:13 PM 6 */ 7 @Test 8 public void uploadFile() { 9 try {10 Path srcPath = new Path("/hadoopcase/maxtemprature/input/data.txt");11 String uri = "hdfs://192.168.1.15:9000/hadoopcase/maxtemprature/";12 Path dstPath = new Path(uri);13 Configuration conf = new Configuration();14 FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);15 fileSystem.copyFromLocalFile(srcPath, dstPath);16 } catch (Exception e) {17 e.printStackTrace();18 }19 }
从HDFS中删除文件:
1 /** 2 * 删除文件 3 * 4 * @author: wyf 5 * @version: Jul 10, 2013 4:53:26 PM 6 */ 7 @Test 8 public void deleteFile() { 9 try {10 String uri = "hdfs://192.168.1.15:9000/hadoopcase/maxtemprature/data.txt";11 Configuration conf = new Configuration();12 FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);13 fileSystem.deleteOnExit(new Path(uri));14 } catch (Exception e) {15 e.printStackTrace();16 }17 }