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

上传人:jiups****uk12 文档编号:40256932 上传时间:2018-05-25 格式:DOC 页数:27 大小:4.87MB
返回 下载 相关 举报
实验3-熟悉常用的HDFS操作-答案_第1页
第1页 / 共27页
实验3-熟悉常用的HDFS操作-答案_第2页
第2页 / 共27页
实验3-熟悉常用的HDFS操作-答案_第3页
第3页 / 共27页
实验3-熟悉常用的HDFS操作-答案_第4页
第4页 / 共27页
实验3-熟悉常用的HDFS操作-答案_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《实验3-熟悉常用的HDFS操作-答案》由会员分享,可在线阅读,更多相关《实验3-熟悉常用的HDFS操作-答案(27页珍藏版)》请在金锄头文库上搜索。

1、实验 2 熟悉常用的 HDFS 操作实验手册主讲教师:刘斌 第 1 页实验 2 熟悉常用的 HDFS 操作1 实验目的实验目的1.理解 HDFS 在 Hadoop 体系结构中的角色; 2.熟练使用 HDFS 操作常用的 Shell 命令; 3.熟悉 HDFS 操作常用的 Java API。2 实验平台实验平台操作系统:Linux Hadoop 版本:2.6.0 或以上版本 JDK 版本:1.6 或以上版本 Java IDE:Eclipse3 实验内容和要求实验内容和要求1. 编程实现以下指定功能,并利用 Hadoop 提供的 Shell 命令完成相同任务:提示: 1) 部分 Shell 命令的

2、参数路径只能是本地路径或者 HDFS 路径。 2) 若 Shell 命令的参数既可以是本地路径,也可以是 HDFS 路径时,务必注意区分。为保 证操作正确,可指定路径前缀 hdfs:/ 或者 file:/ 3) 注意区分相对路径与绝对路径 4) 具体命令的说明可参考教材或 http:/hadoop.apache.org/docs/stable/hadoop-project-dist/had oop-common/FileSystemShell.html(1) 向 HDFS 中上传任意文本文件,如果指定的文件在 HDFS 中已经存在,由用户 指定是追加到原有文件末尾还是覆盖原有的文件;Shell

3、 命令: 检查文件是否存在: ./hdfs dfs -test -e text.txt(执行完这一句不会输出结果,需要继续输入命 令 “ echo $?“) 追加命令: ./hdfs 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 才会真正执行

4、):if $(./hdfs dfs -test -e text.txt); then $(./hdfs dfs -appendToFile local.txt text.txt); else $(./hdfs dfs -copyFromLocal -f local.txt text.txt); fiJava 代码:实验 2 熟悉常用的 HDFS 操作实验手册主讲教师:刘斌 第 2 页import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*;public class H

5、DFSApi /* 判断路径是否存在*/public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = FileSystem.get(conf);return fs.exists(new Path(path);/* 复制文件到指定路径* 若路径已存在,则进行覆盖*/public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) th

6、rows IOException FileSystem fs = FileSystem.get(conf);Path localPath = new Path(localFilePath);Path remotePath = new Path(remoteFilePath);/* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆 盖 */fs.copyFromLocalFile(false, true, localPath, remotePath);fs.close();/* 追加文件内容*/public static void appendToFi

7、le(Configuration conf, String localFilePath, String remoteFilePath) throws IOException FileSystem fs = FileSystem.get(conf);Path remotePath = new Path(remoteFilePath);/* 创建一个文件读入流 */FileInputStream in = new FileInputStream(localFilePath);/* 创建一个文件输出流,输出的内容将追加到文件末尾 */FSDataOutputStream out = fs.appen

8、d(remotePath);/* 读写文件内容 */byte data = new byte1024;int read = -1;while ( (read = in.read(data) 0 ) 实验 2 熟悉常用的 HDFS 操作实验手册主讲教师:刘斌 第 3 页out.write(data, 0, read);out.close();in.close();fs.close();/* 主函数*/ public static void main(String args) Configuration conf = new Configuration();conf.set(“fs.default

9、.name“,“hdfs:/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, re

10、moteFilePath) fileExists = true; System.out.println(remoteFilePath + “ 已存在.“); else System.out.println(remoteFilePath + “ 不存在.“); /* 进行处理 */ if ( !fileExists) / 文件不存在,则上传 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); System.out.println(localFilePath + “ 已上传至 “ + remoteFilePath); el

11、se if ( choice.equals(“overwrite“) ) / 选择覆盖 HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath); System.out.println(localFilePath + “ 已覆盖 “ + remoteFilePath); else if ( choice.equals(“append“) ) / 选择追加 HDFSApi.appendToFile(conf, localFilePath, remoteFilePath); System.out.println(localFile

12、Path + “ 已追加至 “ + remoteFilePath); catch (Exception e) e.printStackTrace(); 实验 2 熟悉常用的 HDFS 操作实验手册主讲教师:刘斌 第 4 页(2) 从 HDFS 中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对 下载的文件重命名;Shell 命令:if $(./hdfs dfs -test -e file:/home/hadoop/text.txt); then $(./hdfs dfs -copyToLocal text.txt ./text2.txt); else $(./hdfs dfs -co

13、pyToLocal text.txt ./text.txt); fiJava 代码:import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*;public class HDFSApi /* 下载文件到本地* 判断本地路径是否已存在,若已存在,则自动进行重命名*/public static void copyToLocal(Configuration conf, String remoteFilePath, String localFilePath) throws IO

14、Exception FileSystem fs = FileSystem.get(conf);Path remotePath = new Path(remoteFilePath);File f = new File(localFilePath);/* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 .) */if (f.exists() System.out.println(localFilePath + “ 已存在.“);实验 2 熟悉常用的 HDFS 操作实验手册主讲教师:刘斌 第 5 页Integer i = 0;while (true) f = new File(local

15、FilePath + “_“ + i.toString();if (!f.exists() localFilePath = localFilePath + “_“ + i.toString();break;System.out.println(“将重新命名为: “ + localFilePath);/ 下载文件到本地Path localPath = new Path(localFilePath);fs.copyToLocalFile(remotePath, localPath);fs.close();/* 主函数*/ public static void main(String args) Configuration conf = new Configuration();conf.set(“fs.default.name“,“hdfs:/localhost:9000“); String localFilePath = “/home/hadoop/text.txt“; / 本地路径 String remoteFilePath = “/user/hadoop/text.txt“; / HDFS 路径try HDFSApi.copyToLocal(conf, remoteFilePath, localFilePath); System.out.println(“下载完成“); cat

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号