版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验1 HDFS Java API编程一、实验目的1、理解HDFS在Hadoop体系结构中的角色;2、熟悉HDFS操作常用的Java API。二、实验平台操作系统:LinuxHadoop版本:2.6.0或以上版本JDK版本:1.6或以上版本Java IDE:Eclipse三、实验内容利用HDFS的Java API编程实现以下指定功能:1、文件读写:通过FSDataOutputStream将文件内容写入HDFS,通过FSDataInputStream从HDFS中读取文件内容。Java代码:package com.hut.test;import org.apache.hadoop.conf.Con
2、figuration;import org.apache.hadoop.fs.*;import java.io.*;public class MyTest1 public static void main(String args) throws IOException Configuration conf = new Configuration();conf.set(, hdfs:/localhost:9000);conf.set(fs.hdfs.impl, org.apache.hadoop.hdfs.DistributedFileSystem);FileSys
3、tem fs = FileSystem.get(conf);Path filePath = new Path(/user/hadoop/text.txt);/ 写入数据FSDataOutputStream outStream = fs.create(filePath);outStream.writeUTF(hello world ntest 123);outStream.close();/ 读取数据FSDataInputStream inStream = fs.open(filePath);String data = inStream.readUTF();System.out.println(
4、data);inStream.close();fs.close();2、文件上传:向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件。Java代码:package com.hut.test;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import java.io.*;public class MyTest2 public static boolean test(Configuration conf, String path)th
5、rows IOException FileSystem fs = FileSystem.get(conf);return fs.exists(new Path(path);public static void copyFromLocalFile(Configuration conf,String localFilePath, String remoteFilePath) throws IOException FileSystem fs = FileSystem.get(conf);Path localPath = new Path(localFilePath);Path remotePath
6、= new Path(remoteFilePath);/* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */fs.copyFromLocalFile(false, true, localPath, remotePath);fs.close();public static void appendToFile(Configuration conf, String localFilePath,String remoteFilePath) throws IOException FileSystem fs = FileSystem.get(conf);
7、Path remotePath = new Path(remoteFilePath);/* 创建一个文件读入流 */FileInputStream in = new FileInputStream(localFilePath);/* 创建一个文件输出流,输出的内容将追加到文件末尾 */FSDataOutputStream out = fs.append(remotePath);/* 读写文件内容 */byte data = new byte1024;int read = -1;while (read = in.read(data) 0) out.write(data, 0, read);out
8、.close();in.close();fs.close();public static void main(String args) Configuration conf = new Configuration();conf.set(, hdfs:/localhost:9000);conf.set(fs.hdfs.impl, org.apache.hadoop.hdfs.DistributedFileSystem);conf.set(dfs.client.block.write.replace-datanode-on-failure.policy, NEVER)
9、;conf.set(dfs.client.block.write.replace-datanode-on-failure.enable, true);String localFilePath = /home/hadoop/text.txt; / 本地路径String remoteFilePath = /user/hadoop/text.txt; / HDFS路径String choice = append; / 若文件存在则追加到文件末尾/String choice = overwrite; / 若文件存在则覆盖try /* 判断文件是否存在 */boolean fileExists = fa
10、lse;if (MyTest2.test(conf, remoteFilePath) fileExists = true;System.out.println(remoteFilePath + 已存在.); else System.out.println(remoteFilePath + 不存在.);/* 进行处理 */if (!fileExists) / 文件不存在,则上传MyTest2.copyFromLocalFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath + 已上传至 + remote
11、FilePath); else if (choice.equals(overwrite) / 选择覆盖MyTest2.copyFromLocalFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath + 已覆盖 + remoteFilePath); else if (choice.equals(append) / 选择追加MyTest2.appendToFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath
12、+ 已追加至 + remoteFilePath); catch (Exception e) e.printStackTrace();3、文件下载:从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。Java代码:package com.hut.test;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import java.io.*;public class MyTest3 /* * 下载文件到本地 判断本地路径是否已存在,若已存在,则自动进行重命名 */pu
13、blic static void copyToLocal(Configuration conf, String remoteFilePath,String localFilePath) throws IOException FileSystem fs = FileSystem.get(conf);Path remotePath = new Path(remoteFilePath);File f = new File(localFilePath);/* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 .) */if (f.exists() System.out.println(loc
14、alFilePath + 已存在.);Integer i = 0;while (true) f = new File(localFilePath + _ + i.toString();if (!f.exists() localFilePath = localFilePath + _ + i.toString();break;i+;System.out.println(将重新命名为: + localFilePath);/ 下载文件到本地Path localPath = new Path(localFilePath);fs.copyToLocalFile(remotePath, localPath
15、);fs.close();public static void main(String args) Configuration conf = new Configuration();conf.set(, hdfs:/localhost:9000);String localFilePath = /home/hadoop/text.txt; / 本地路径String remoteFilePath = /user/hadoop/text.txt; / HDFS路径try MyTest3.copyToLocal(conf, remoteFilePath, localFil
16、ePath);System.out.println(下载完成); catch (Exception e) e.printStackTrace();4、使用字符流读取HDFS中指定文件的内容并输出到终端中Java代码:package com.hut.test;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import java.io.*;public class MyTest4 /* * 读取文件内容 */public static void cat(Configuration conf, St
17、ring remoteFilePath)throws IOException FileSystem fs = FileSystem.get(conf);Path remotePath = new Path(remoteFilePath);FSDataInputStream in = fs.open(remotePath);BufferedReader d = new BufferedReader(new InputStreamReader(in);String line = null;while (line = d.readLine() != null) System.out.println(
18、line);d.close();in.close();fs.close();public static void main(String args) Configuration conf = new Configuration();conf.set(, hdfs:/localhost:9000);String remoteFilePath = /user/hadoop/text.txt; / HDFS路径try System.out.println(读取文件: + remoteFilePath);MyTest4.cat(conf, remoteFilePath);System.out.println(n读取完成); catch (Exception e) e.printStackTrace();5、删除HDFS中指定的文件Java命令:package com.hut.test;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import java.io.*;public class MyTest5
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年高考天津卷文综历史试题解析及答案
- 燃煤机组绿色转型项目可行性研究报告
- 变压器油色谱在线监测系统项目可行性研究报告
- 2026年广东水利电力职业技术学院单招职业适应性测试题库含答案详解(突破训练)
- 2026年广东理工职业学院单招职业倾向性考试题库含答案详解(模拟题)
- 2026年山西省吕梁市单招职业适应性考试题库及一套参考答案详解
- 2026年广东金融学院单招职业倾向性测试题库及答案详解(新)
- 2026年广东松山职业技术学院单招职业技能测试题库附参考答案详解(能力提升)
- 2026年广西体育高等专科学校单招职业技能考试题库及参考答案详解1套
- 2026年广州铁路职业技术学院单招职业适应性测试题库及答案详解(新)
- GB/T 9867-2008硫化橡胶或热塑性橡胶耐磨性能的测定(旋转辊筒式磨耗机法)
- GB/T 3195-2008铝及铝合金拉制圆线材
- 整形美容医院与公立整形医院合作方案
- 合理安排课余生活-完整版公开课件
- bbf-dhda双轴交流伺服驱动器说明书
- 《电视摄像教程》课件第6章
- 人大换届选举培训提纲汇编课件
- 2022青岛版科学五年级下册全册优质教案教学设计
- Unit10Lesson2Communityspirit课件-高中英语北师大版(2019)选择性必修第四册
- 建设项目全过程跟踪审计底稿(综合类、工程类、财务类)
- 2020 新ACLS-PCSA课前自我测试-翻译版玉二医【复制】附有答案
评论
0/150
提交评论