博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDFS API 操作
阅读量:6968 次
发布时间:2019-06-27

本文共 4106 字,大约阅读时间需要 13 分钟。

这里列出了基本操作,其它操作都类似:

  注意:例子中的操作可能出现没有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     }

 

 

转载于:https://www.cnblogs.com/geopanda/p/Java_API_%e6%93%8d%e4%bd%9c_HDFS.html

你可能感兴趣的文章
CPU 虚拟化
查看>>
circRNA 在人和小鼠脑组织中的表达
查看>>
新人替代旧人
查看>>
2步安装1个hive docker运行环境[centos7]
查看>>
Android Keystore 对称-非对称加密
查看>>
工作总结 获取html 标签 自定义属性值 根据html 自定义属性 获取 到标签...
查看>>
window.external的使用
查看>>
wait/waitpid函数与僵尸进程、fork 2 times
查看>>
iOS中Storyboard使用要点记录
查看>>
payload和formData有什么不同?
查看>>
【文件监控】之一:理解 ReadDirectoryChangesW part1
查看>>
Objective-C
查看>>
PyCharm搭建pyqt5开发环境
查看>>
微信小程序实战–集阅读与电影于一体的小程序项目(七)
查看>>
摄像机、投影、3D旋转、缩放
查看>>
给大家分享两款正在使用的ref“.NET研究”lector插件
查看>>
关于presentModalViewController的一点儿思考
查看>>
【128】Word中的VBA
查看>>
PowerCollections
查看>>
禁用gridview,listview回弹或下拉悬停
查看>>