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

上传人:夏** 文档编号:486427350 上传时间:2022-08-27 格式:DOC 页数:27 大小:4.81MB
返回 下载 相关 举报
实验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 实验目的1.理解HDFS在Hadoop体系结构中的角色;2.熟练使用HDFS操作常用的Shell命令;3.熟悉HDFS操作常用的Java API。2 实验平台操作系统:LinuxHadoop版本:2.6.0或以上版本JDK版本:1.6或以上版本Java IDE:Eclipse3 实验内容和要求1. 编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务:提示:1) 部分Shell命令的参数路径只能是本地路径或者HDFS路径。2) 若Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分。为保证操作正确,可指定路径前缀 hdf

2、s:/ 或者 file:/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 -appendT

3、oFile 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 $(./hdfs dfs -c

4、opyFromLocal -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

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

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

7、remotePath = new Path(remoteFilePath); /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append(remotePath); /* 读写文件内容 */ byte data = new byte1024; int read = -1; while ( (read = in.read(data) 0 ) out.write(data, 0, rea

8、d); out.close(); in.close(); 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路径String choice =

9、 append; / 若文件存在则追加到文件末尾/String choice = overwrite; / 若文件存在则覆盖try /* 判断文件是否存在 */Boolean fileExists = false;if (HDFSApi.test(conf, remoteFilePath) fileExists = true;System.out.println(remoteFilePath + 已存在.); else System.out.println(remoteFilePath + 不存在.);/* 进行处理 */if ( !fileExists) / 文件不存在,则上传HDFSApi

10、.copyFromLocalFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath + 已上传至 + remoteFilePath); else if ( choice.equals(overwrite) ) / 选择覆盖HDFSApi.copyFromLocalFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath + 已覆盖 + remoteFilePath); else if ( choice.equa

11、ls(append) ) / 选择追加HDFSApi.appendToFile(conf, localFilePath, remoteFilePath);System.out.println(localFilePath + 已追加至 + remoteFilePath); catch (Exception e) e.printStackTrace();(2) 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;Shell命令:if $(./hdfs dfs -test -e file:/home/hadoop/text.txt);then $(./hdfs d

12、fs -copyToLocal text.txt ./text2.txt); else $(./hdfs dfs -copyToLocal 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(Configuratio

13、n conf, String remoteFilePath, String localFilePath) throws IOException FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); File f = new File(localFilePath); /* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 .) */ if (f.exists() System.out.println(localFilePath + 已存在.); Integer i = 0; while (true) f = new File(localFilePath + _ + i.toString(); if (!f.exists() localFilePath = localFilePath + _ + i.toString(); break; System.out.println(将重新命名为: + localFilePath

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

当前位置:首页 > 资格认证/考试 > 自考

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