ImageVerifierCode 换一换
格式:DOCX , 页数:36 ,大小:713.12KB ,
资源ID:27711063      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/27711063.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验3熟悉常用的HDFS操作答案.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验3熟悉常用的HDFS操作答案.docx

1、实验3熟悉常用的HDFS操作答案实验2熟悉常用的HDFS操作1实验目的1.理解HDFS在HadooP体系结构中的角色;2.熟练使用HDFS操作常用的Shell命令;3.熟悉HDFS操作常用的JaVa APl。2实验平台操作系统:Lin UXHadoop版本:2.6.0或以上版本JDK版本:1.6或以上版本JaVa IDE: ECIiPSe3实验内容和要求1.编程实现以下指定功能,并利用 HadooP提供的Shell命令完成相同任务: 提示:1) 部分Shell命令的参数路径只能是本地路径或者 HDFS路径。2) 若Shell命令的参数既可以是本地路径,也可以是 HDFS路径时,务必注意区分。为

2、保证操作正确,可指定路径前缀 hdfs:/或者file:/3) 注意区分相对路径与绝对路径4) 具体命令的说明可参考教材或 http:/hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-com mon/FileSystemShell.html(1) 向HDFS中上传任意文本文件,如果指定的文件在 HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;Shell命令:检查文件是否存在:./hdfs dfs -test -e text.txt(执行完这一句不会输出结果,需要继续输入命令echo $?)追加命令:./hd

3、fs dfs -appendToFile local.txt text.txt覆盖命令 1: ./hdfs dfs -COPyFromLocal -f local.txt text.txt覆盖命令 2: ./hdfs dfs -CP -f file:/home/hadoop/local.txt text.txt也可以使用如下命令实现:(如下代码可视为一行代码,在终端中输入第一行代码后,直到输入 fi才会真正执行):if $(./hdfs dfs -test -e text.txt);then $(./hdfs dfs -appendToFile local.txt text.txt);else

4、 $(./hdfs dfs -COPyFromLocal -f local.txt text.txt);fiJaVa代码:import org.apache.hadoop.c onf.Con figuratio n;import org.apache.hadoop.fs.;import java.io.*;PUbIiC class HDFSAPi /*判断路径是否存在*/PUbIiC StatiC boolea n test(C On figurati On conf, String Path) throws IOEXCePti On FiIeSyStem fs = FiIeSyStem.ge

5、t(c onf);return fs.exists(new Path(Path);/*复制文件到指定路径若路径已存在,则进行覆盖*/PUbIiC StatiC void COPyFromLocalFile(COnfiguratiOn conf, String localFilePath, StringremoteFilePath) throws IOEXCePti On FiIeSyStem fs = FiIeSyStem.get(c onf);Path localPath = new Path(IOcalFilePath);Path remotePath = new Path(remoteF

6、ilePath);/* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖*/fs.copyFromLocalFile(false, true, localPath, remotePath);fs.close();out.close();in .close();fs.close();*主函数*/PUbIiC StatiC void main( Stri ng args) Con figurati On Conf = new Con figurati on();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:900

7、0);String localFilePath = /home/hadoop/text.txt; / 本地路径String remoteFilePath = userhadooptext.txt; / HDFS 路径String ChOiCe = append; /若文件存在则追加到文件末尾/ String ChOiCe = overwrite; / 若文件存在则覆盖try /*判断文件是否存在 */Boolea n fileExists = false;if (HDFSAPi.test(conf, remoteFilePath) fileExists = true;SyStem.out.pr

8、intln(remoteFilePath + 已存在.); else SyStem.out.println(remoteFilePath + 不存在.);/*进行处理*/if ( !fileExis ts) / 文件不存在,则上传HDFSAPi.copyFromLocalFile(co nf, IocaIFiIePath, remoteFilePath);SyStem.out.pri ntln( IoCaIFilePath + 已上传至 + remoteFilePath); else if ( ChOiCe.equals(overwrite) ) / 选择覆盖HDFSAPi.copyFromL

9、ocaIFiIe(co nf, IocaIFiIePath, remoteFilePath);SyStem.out.println(IoCaIFilePath + 已覆盖+ remoteFilePath); else if ( ChOiCe.equals(append) ) / 选择追加HDFSAPi.appendToFiIe(conf, IocaIFiIePath, remoteFilePath);SyStem.out.pri ntln (IoCaIFilePath + 已追加至 + remoteFilePath); CatCh (EXCePti On e) e.pri ntStackTra

10、ce();(2) 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;Shell命令:if $(./hdfs dfs -test -e file:/home/hadoop/text.txt);then $(./hdfs dfs -COPyToLocal text.txt ./text2.txt);else $(./hdfs dfs -COPyToLocal text.txt ./text.txt);fiIn teger i = 0;while (true) f = new File(IoCalFiIePath + _ + i.toStri ng();if (

11、!f.exists() localFilePath = localFilePath + _ + i.toStri ng(); break;SyStem.out.println(将重新命名为:+ localFilePath);/下载文件到本地Path localPath = new Path(IocalFilePath); fs.copyToLocalFile(remotePath, IocaIPath); fs.close();*主函数*/PUbIiC StatiC void main(String args) Con figurati On Conf = new Con figurati o

12、n();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:9000);String IocaIFiIePath = /home/hadoop/text.txt; / 本地路径String remoteFilePath = /user/hadoop/text.txt; / HDFS 路径try HDFSAPi.copyToLocaI(conf, remoteFilePath, IocaIFiIePath); SyStem.out.println(下载完成); CatCh (EXCePti On e) e.pri ntStackTrace();(3)将HDFS中

13、指定文件的内容输出到终端中;Shell命令:.hdfs dfs -Cat text.txt*主函数*/PUbIiC StatiC void main(String args) Con figurati On Conf = new Con figurati on();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:9000);String remoteFilePath = /user/hadoop/text.txt; / HDFS 路径 try SyStem.out.println(读取文件:+ remoteFilePath);HDFSAPi.cat(co

14、nf, remoteFilePath);SyStem.out.println(n 读取完成); CatCh (EXCePti On e) e.pri ntStackTrace();EW j: lfaf al Sril M 祕 1d h3iMSF 4 1 fp i刁 ra5l.4 M*F4Ekvc LLH.-fl!=-RfflyiMlfc Ehin UI(4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;Shell命令:.hdfs dfs -IS -h text.txtimport java.text.SimpleDateFormat;PUbIiC class HDFSAPi

15、/*显示指定文件的信息*/PUbIiC StatiC void ls(COnfiguratiOn conf, String remoteFilePath) throws IOEXCePtion FiIeSyStem fs = FiIeSyStem.get(c onf);Path remotePath = new Path(remoteFilePath);FiIeStatus fileStatuses = fs.listStatus(remotePath);for (FiIeStatUS S : fileStatuses) SyStem.out.println(路径:+ s.getPath().

16、toString();SyStem.out.println(权限:+ s.getPermission().toString();SyStem.out.println(大小:+ s.getLen();/*返回的是时间戳,转化为时间日期格式 */Long timeStamp = s.getModificatiOnTime();SimPIeDateFOrmat format = new SimPIeDateFormat(yyyy-MM-dd HH:mm:ss);Stri ng date = format.format(timeStamp);SyStem.out.println(时间:+ date);

17、fs.close();*主函数*/PUbIiC StatiC void main(String args) Con figurati On Conf = new Con figurati on();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:9000);String remoteFilePath = /user/hadoop/text.txt; / HDFS 路径try SyStem.out.println(读取文件信息:+ remoteFilePath);HDFSAPi.ls(co nf, remoteFilePath);SyStem.out.pri

18、ntln(n 读取完成); CatCh (EXCePti On e) e.pri ntStackTrace();卜 g M3rp4 3L2.M.i IIli m g Ilrt IVl-昌 jvv-u L9-Jm 平 -r-F C a Hfl: IaQlMl IO M:*审卜 E IlirMy-M-1 M . JM P wJ,j i JIrB plje- OJii-彳(5)给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建 时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信 息;Shell命令:.hdfs dfs -IS -R -h userhadoopSy

19、Stem.out.println(大小:+ s.getLen();/*返回的是时间戳,转化为时间日期格式 */Long timeStamp = s.getModificatiOnTime();SimPIeDateFOrmat format = new SimPIeDateFormat(yyyy-MM-dd HH:mm:ss);Stri ng date = format.format(timeStamp);SyStem.out.println(时间:+ date);SyStem.out.pri ntl n();fs.close();/*主函数*/PUbIiC StatiC void main(S

20、tring args) Con figurati On Conf = new Con figurati on();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:9000);String remoteDir = /user/hadoop; / HDFS 路径try SyStem.out.println(递归)读取目录下所有文件的信息 :+ remoteDir);HDFSAPi.lSDir(CO nf, remoteDir);SyStem.out.println(读取完成); CatCh (EXCePti On e) e.pri ntStackTrace()

21、;(6) 提供一个 HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;Shell命令:if $(./hdfs dfs -test -d dir1dir2);then $(./hdfs dfs -touchz dir1dir2file name);else $(./hdfs dfs -mkdir -P dir1dir2 & hdfs dfs -touchz dir1dir2file name);fi 删除文件:./hdfs dfs -rm dir1dir2file namePUbliC StatiC boolea n mkdir(C On figurat

22、i On conf, String remoteDir) throws IoEXCePti On FileSyStem fs = FiIeSyStem.get(c onf);Path dirPath = new Path(remoteDir);boolea n result = fs.mkdirs(dirPath);fs.close();return result;*/创建文件PUbIiC StatiC void touchz(Configuration conf, String remoteFilePath) throws IOEXCePtion FiIeSyStem fs = FiIeSy

23、Stem.get(c onf);Path remotePath = new Path(remoteFilePath);FSDataOUtPUtStream OUtPUtStream = fs.create(remotePath);OUtPUtStream.close();fs.close();*删除文件*/PUbIiC StatiC boolea n rm(C On figurati On conf, Stri ng remoteFilePath) throws IOEXCePti On FiIeSyStem fs = FiIeSyStem.get(c onf);Path remotePath

24、 = new Path(remoteFiIePath); boolea n result = fs.deIete(remotePath, false); fs.close();retur n result;*主函数*/PUbIiC StatiC void main(String args) Con figurati On Conf = new Con figurati on();Con f.set(fs.default. name,hdfs:/IoCaIhoSt:9000);String remoteFilePath = /user/hadoop/input/text.txt; / HDFS

25、路径String remoteDir = /user/hadoop/input; / HDFS 路径对应的目录try /*判断路径是否存在,存在则删除,否则进行创建 */if ( HDFSAPi.test(co nf, remoteFilePath) ) HDFSAPi.rm(co nf, remoteFilePath); / 删除SyStem.out.println(删除路径:+ remoteFilePath); else if ( !HDFSApi.test(co nf, remoteDir) ) / 若目录不存在,则进行创建HDFSAPi.mkdir(c onf, remoteDir);

26、SyStem.out.println(创建文件夹:+ remoteDir);HDFSAPi.touchz(co nf, remoteFilePath);SyStem.out.println(创建路径:+ remoteFilePath); CatCh (EXCePti On e) e.pri ntStackTrace();(7) 提供一个 HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定 当该目录不为空时是否还删除该目录;Shell命令:创建目录:.hdfs dfs -mkdir -P dir1dir2删除目录(如

27、果目录非空则会提示 not empty,不执行删除):./hdfs dfs -rmdir dir1dir2 强制删除目录:./hdfs dfs -rm -R dir1dir2JaVa代码:import org.apache.hadoop.c onf.Con figuratio n;import org.apache.hadoop.fs.*;import java.io.*;PUbIiC class HDFSAPi /*判断路径是否存在*/PUbIiC StatiC boolea n test(C On figurati On conf, String Path) throws IOEXCePt

28、i On FiIeSyStem fs = FiIeSyStem.get(c onf);return fs.exists(new Path(Path);*判断目录是否为空* true:空,false:非空*/PUbIiC StatiC boolean isDirEmpty(COnfiguratiOnConf, String remoteDir) throws IOEXCePtiOn FiIeSyStem fs = FiIeSyStem.get(c onf);Path dirPath = new Path(remoteDir);RemOteIterator remoteIterator = fs.

29、IistFiIes(dirPath, true);return !remotelterator.hasNext();*/创建目录PUbIiC StatiC boolea n mkdir(C On figurati On conf, String remoteDir) throws IOEXCePti On FiIeSyStem fs = FiIeSyStem.get(c onf);Path dirPath = new Path(remoteDir);boolea n result = fs.mkdirs(dirPath);fs.close();return result;/*删除目录*/PUbIiC StatiC boolea n rmDir(C On figurati On conf, Stri ng remoteDir) throws IOEXCePti On FiIeSyStem fs = FiIeSyStem.get(c onf);Path dirPath = new Path(remoteDir);/*第二个参数表示是否递归删除所有文件 */boolea n result = fs.deIete(dirPath, true);fs.close();retur

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1