实验3-熟悉常用的HDFS操作-答案_第1页
实验3-熟悉常用的HDFS操作-答案_第2页
实验3-熟悉常用的HDFS操作-答案_第3页
实验3-熟悉常用的HDFS操作-答案_第4页
实验3-熟悉常用的HDFS操作-答案_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

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

文档简介

1、实验2熟悉常用的HDFS操作1实验目的1.了解HDFS在Hadoop体系结构中的作用。熟练使用HDFS操作中常用的shell命令;熟悉HDFS操作常用的Java API。2实验平台操作系统:LinuxHadoop版本:2.6.0或更高版本JDK版本:1.6或更高版本Java IDE:Eclipse3实验内容和要求1.编程实现以下指定功能,并使用Hadoop提供的shell命令执行相同的操作:提示:1)某些Shell命令的参数路径只能是本地路径或Shell路径。2)如果Shell命令的参数可以是本地路径或Shell路径,则必须小心。要正确操作,可以指定路径前缀hdfs:/或file:/3)必须区

2、分相对路径和绝对路径(4)教材或http:/Hadoop . Apache . org/docs/stable/Hadoop-project-dist/Hadoop-common/file systems中有关特定命令的说明(1)将文本文件上载到HDFS,如果HDFS中已存在指定的文件,请指定用户是将其添加到原始文件的末尾,还是复盖原始文件。shell命令:文件中的:确认/hdfs DFS -test -e text.txt存在。执行此句子不会输出结果,必须继续输入命令Echo $?),以获取详细信息附加命令:/HDFS DFS-append to file local . txt text

3、. txt重新定义指令1:/HDFS DFS-copy from local-f local . txt text . txt复盖命令23360。/HDFS DFS-CP-f文件:/home/Hadoop/local . txt text . txt也可以使用以下命令实现:(可以将以下代码视为一行代码,在终端中输入第一行代码后,必须输入fi才能实际执行以下代码:)If $(./HDFS DFS-test-e text . txt);Then $(./HDFS DFS-append to file local . txt text . txt);Else $(./HDFS DFS-copy fr

4、om local-f local . txt text . txt);FiJava代码:importorg . Apache . Hadoop . conf . configuration;import org . Apache . Hadoop . fs . *;import Java . io . *;Public class hdf SAPI/* *验证路径是否存在*/public static boolean test(string path)throws io exception file system fs=file system . get(conf);return fs . e

5、xists(new Path(Path);/* *将文件复制到指定路径*如果路径已存在,则复盖*/public static void copy from local file(configuration conf,string local file path,string remote file path)throws io exceptionfile system fs=file system . get(conf);path local path=new path(local file path);Path remote path=new path(远程文件路径);/* fs.copyF

6、romLocalFile第一个参数指示是否删除源文件,第二个参数指示是否复盖*/Fs.copy from local file (false、true、localpath、remote path);fs . close();/* *其他文件内容*/public static void append to file(configuration conf,string local file path,string remote file path)throws io exception file system fs=file system . get(conf);Path remote path=

7、new path(远程文件路径);/*创建文件读取流*/file input stream in=new file input stream(local file path);/*创建文件输出流,输出内容将附加到文件末尾*/fs data output streamout=fs . append(remote path);/*读取/写入文件内容*/bytedata=new byte1024;int read=-1;While (read=in.read(data) 0) Out.write(data,0,read);out . close();in . close();fs . close()

8、;/* *主函数*/public static void main(stringargs)configuration conf=new configuration();Conf.set ( , HDFS :/localhost 3360009 );string local file path=/home/Hadoop/text . txt ;/本地路径string remote file path=/user/Hadoop/text . txt ;/HDFS路径String choice= append/如果文件存在,则添加到文件末尾/String choice=

9、 overwrite ;/如果文件存在,则复盖Try /*判断档案是否存在*/Boolean fileExists=falseIf (hdf sapi.test (conf,remote file path)FileExists=trueSystem.out.println(远程文件路径已存在。); elseSystem.out.println(缺少远程文件路径);/*处理*/If(!FileExists) /文件不存在,请上载Hdf sapi.copy from local file (conf,local file path,remote file path);System.out.pri

10、ntln(本地文件路径已上载到“remote file path”); else if(choice . equals( overwrite )/复盖选项Hdf sapi.copy from local file (conf,local file path,remote file path);System.out.println(本地文件路径复盖“remote file path”); else if(choice . equals( append )/选择其他Hdf sapi.append to file (conf,local file path,remote file path);Sys

11、tem.out.println(本地文件路径已添加到“remoteFilePath”中); catch (exception e)e . printstacktrace();(2)从HDFS下载指定的文件,如果本地文件与要下载的文件同名,则自动重命名下载的文件;壳命令:If $(./HDFS DFS-test-e file :/home/Hadoop/text . txt);Then $(./HDFS DFS-copy to local text . txt ./text 2 . txt);Else $(./HDFS DFS-copy to local text . txt ./text .

12、txt);FiJava代码:importorg . Apache . Hadoop . conf . configuration;import org . Apache . Hadoop . fs . *;import Java . io . *;Public class hdf SAPI/* *将文件下载到本地*检查本地路径是否已存在,如果已存在,则自动重命名*/public static void copy to local(configuration conf,string remote file path,string local file path,string local file

13、 path)throws io exception file system fs=file system . get(conf);Path remote path=new path(远程文件路径);File f=new file(本地文件路径);/*如果文件名存在,则自动重命名(文件名后接_ 0、_ 1、)*/If (f.exists() system . out . print ln(local file path 已存在);integer I=0;While (true) f=new file(local file path _ I . tostring();If(!f . exists(

14、)local file path=local file path _ I . tostring();Break名称变更为system . out . print ln( : local file path)。/文件本地下载path local path=new path(local file path);Fs.copy to local file(远程路径,本地路径);fs . close();/* *主函数*/public static void main(stringargs)configuration conf=new configuration();Conf.set (fs.defau

15、 , HDFS :/localhost 3360009 );string local file path=/home/Hadoop/text . txt ;/本地路径string remote file path=/user/Hadoop/text . txt ;/HDFS路径Try Hdf sapi.copy to local (conf,远程文件路径,本地文件路径);System.out.println(“下载完成”); catch (exception e)e . printstacktrace();(3)将HDFS中指定文件的内容输出到终端。壳命令:./hdfs DFS -cat text.txtJava代码:importorg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论