




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验1 HDFS Java API编程一、实验目的1、理解HDFSB Hadoop体系结构中的角色;2、熟悉HDFSt作常用的Java API。二、实验平台操作系统:LinuxHadoop版本:或以上版本JDK版本:或以上版本Java IDE : Eclipse三、实验内容利用HDFS勺Java API编程实现以下指定功能:1、文件读写:通过FSDataOutputStream 将文件内容写入 HDFS通过FSDataInputStream从HDF计读取文件内容。Java代码:package .*;public class MyTest1 public static void main(Str
2、ing口 args) throws IOException Configuration conf = new Configuration。;("","");("","");FileSystem fs = (conf);Path filePath = new Path("/user/hadoop/");public class MyTest2 public static boolean test(Configuration conf, String path)throws IOException
3、FileSystem fs = (conf);return (new Path(path);public static void copyFromLocalFile(Configuration conf,String localFilePath, String remoteFilePath) throws IOException FileSystem fs = (conf);Path localPath = new Path(localFilePath);Path remotePath = new Path(remoteFilePath);/*第一个参数表示是否删除源文件,第二个参数表示是否覆
4、盖*/(false, true, localPath, remotePath);();public static void appendToFile(Configuration conf, String localFilePath,String remoteFilePath) throws IOException FileSystem fs = (conf);Path remotePath = new Path(remoteFilePath);/*创建一个文件读入流*/FileInputStream in = new FilelnputStream(localFilePath);/*创建一个文
5、件输出流,输出的内容将追加到文件末尾*/FSDataOutputStream out = (remotePath);/*读写文件内容*/byte口 data = new byte1024;int read = -1;while (read = (data) > 0) (data, 0, read);();();();public static void main(String口 args) Configuration conf = new Configuration。;("","");("","");(&qu
6、ot;", "NEVER");("", "true");String localFilePath = "/home/hadoop/" else + " 不存在.");/*进行处理*/if (!fileExists) 5public class MyTest3 /*下载文件到本地判断本地路径是否已存在,若已存在,则自动进行重命名*/public static void copyToLocal(Configuration conf, String remoteFilePath,String
7、 localFilePath) throws IOException FileSystem fs = (conf);Path remotePath = new Path(remoteFilePath);File f = new File(localFilePath);/*如果文件名存在,自动重命名(在文件名后面加上 _0, _1)*/if () + " 已存在.");Integer i = 0;while (true) f = new File(localFilePath + "_" + ();if (!() localFilePath = localF
8、ilePath + "_" + ();break;i+;"将重新命名为:"+ localFilePath);public class MyTest4 /*读取文件内容*/public static void cat(Configuration conf, String remoteFilePath)throws IOException FileSystem fs = (conf);Path remotePath = new Path(remoteFilePath);FSDataInputStream in = (remotePath);BufferedR
9、eader d = new BufferedReader(new InputStreamReader(in);String line = null;while (line = () != null) ();();();public static void main(String口 args) Configuration conf = new Configuration。;("","");String remoteFilePath = "/user/hadoop/"public class MyTest5 /*删除文件*/public
10、static boolean rm(Configuration conf, String remoteFilePath)throws IOException FileSystem fs = (conf);Path remotePath = new Path(remoteFilePath);boolean result = (remotePath, false);();return result;public static void main(String口 args) Configuration conf = new Configuration。;("","");String remoteFilePath = "/user/hadoop/&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年高中数学 第3章 数系的扩充与复数的引入 3.2 复数代数形式的四则运算 3.2.2 复数代数形式的乘除运算(教师用书)教学实录 新人教A版选修2-2
- 郑州信息科技职业学院单招职业技能测试参考试题(附答案)
- DB14T 3224-2024艺术档案收集与整 理规范
- 小学教科研活动总结
- 电气自动化专业实习报告
- 广告业务年度总结
- 房东房屋租赁合同
- 网络公司网站建设合同
- 五星级酒店改造工程装修合同
- 2025年哈尔滨货运资格证模拟考试
- 批评与自我批评表
- 《公共政策学(第二版)》 课件 杨宏山 第1-6章 导论、政策系统-政策执行
- 【课件】安全教育 防溺水《不做孤泳者安全伴你我》课件
- 期中模拟练习 (试题)-2023-2024学年五年级下册数学北京版
- 教研员培训课件
- 员工主人翁意识培训课件
- 支气管扩张伴咯血的护理查房幻灯片
- 2024无孩无共同财产离婚协议书模板
- DZ∕T 0284-2015 地质灾害排查规范(正式版)
- 胆囊息肉临床路径标准住院流程
- 低氧血症的护理查房
评论
0/150
提交评论