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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验3熟悉常用的HDFS操纵答案解析Word下载.docx

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

2、xt 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 $(./hdfs dfs -copyFromLocal

3、-f local.txt text.txt);fiJava代码:import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import java.io.*;public class HDFSApi /* * 判断路径是否存在 */ public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = FileSystem.get(conf); return fs.exists(new P

4、ath(path); * 复制文件到指定路径 * 若路径已存在,则进行覆盖 public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException Path localPath = new Path(localFilePath); Path remotePath = new Path(remoteFilePath); /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */ f

5、s.copyFromLocalFile(false, true, localPath, remotePath); fs.close(); * 追加文件内容 public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSD

6、ataOutputStream out = fs.append(remotePath); /* 读写文件内容 */ byte data = new byte1024; int read = -1; while ( (read = in.read(data) 0 ) out.write(data, 0, read); out.close(); in.close(); * 主函数 public static void main(String args) Configuration conf = new Configuration(); conf.set(fs.default.name,hdfs:/

7、localhost:9000); String localFilePath = /home/hadoop/text.txt; / 本地路径 String remoteFilePath = /user/hadoop/text.txt / HDFS路径 String choice = append / 若文件存在则追加到文件末尾/ String choice = overwrite / 若文件存在则覆盖 try /* 判断文件是否存在 */ Boolean fileExists = false; if (HDFSApi.test(conf, remoteFilePath) fileExists =

8、 true; System.out.println(remoteFilePath + 已存在. else 不存在. /* 进行处理 */ if ( !fileExists) / 文件不存在,则上传 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); System.out.println(localFilePath + 已上传至 + remoteFilePath); else if ( choice.equals() ) / 选择覆盖 已覆盖 ) ) / 选择追加 HDFSApi.appendToFile(conf, l

9、ocalFilePath, remoteFilePath); 已追加至 catch (Exception e) e.printStackTrace(); (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); * 下载文件到本

10、地 * 判断本地路径是否已存在,若已存在,则自动进行重命名 public static void copyToLocal(Configuration conf, String remoteFilePath, String localFilePath) throws IOException File f = new File(localFilePath); /* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 .) */ if (f.exists() System.out.println(localFilePath + Integer i = 0; while (true) f =

11、new File(localFilePath + _ + i.toString(); if (!f.exists() localFilePath = localFilePath + + i.toString(); break; System.out.println(将重新命名为: + localFilePath); / 下载文件到本地 fs.copyToLocalFile(remotePath, localPath); HDFSApi.copyToLocal(conf, remoteFilePath, localFilePath); System.out.println(下载完成(3) 将HD

12、FS中指定文件的内容输出到终端中;./hdfs dfs -cat text.txt * 读取文件内容 public static void cat(Configuration conf, String remoteFilePath) throws IOException FSDataInputStream in = fs.open(remotePath); BufferedReader d = new BufferedReader(new InputStreamReader(in); String line = null; while ( (line = d.readLine() != nul

13、l ) System.out.println(line); d.close();读取文件: HDFSApi.cat(conf, remoteFilePath);n读取完成(4) 显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;./hdfs dfs -ls -h text.txtimport java.text.SimpleDateFormat; * 显示指定文件的信息 public static void ls(Configuration conf, String remoteFilePath) throws IOException FileStatus fileStatuse

14、s = fs.listStatus(remotePath); for (FileStatus s : fileStatuses) 路径: + s.getPath().toString();权限: + s.getPermission().toString();大小: + s.getLen(); /* 返回的是时间戳,转化为时间日期格式 */ Long timeStamp = s.getModificationTime(); SimpleDateFormat format = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss String date = format

15、.format(timeStamp);时间: + date);读取文件信息: HDFSApi.ls(conf, remoteFilePath);(5) 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;./hdfs dfs -ls -R -h /user/hadoop * 显示指定文件夹下所有文件的信息(递归) public static void lsDir(Configuration conf, String remoteDir) throws IOException Path dirPath =

16、new Path(remoteDir); /* 递归获取目录下的所有文件 */ RemoteIterator remoteIterator = fs.listFiles(dirPath, true); /* 输出每个文件的信息 */ while (remoteIterator.hasNext() FileStatus s = remoteIterator.next(); System.out.println( System.out.println(); String remoteDir = /user/hadoop(递归)读取目录下所有文件的信息: + remoteDir); HDFSApi.

17、lsDir(conf, remoteDir);读取完成(6) 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;if $(./hdfs dfs -test -d dir1/dir2);then $(./hdfs dfs -touchz dir1/dir2/filename);else $(./hdfs dfs -mkdir -p dir1/dir2 & hdfs dfs -touchz dir1/dir2/filename);删除文件:./hdfs dfs -rm dir1/dir2/filename * 创建目录 public static boolean mkdir(Configuration conf, String remoteDir) throws IOException Path dirPath = new

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2