实验1HDFSJavaAPI编程_第1页
实验1HDFSJavaAPI编程_第2页
实验1HDFSJavaAPI编程_第3页
实验1HDFSJavaAPI编程_第4页
实验1HDFSJavaAPI编程_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论