实验3-熟悉常用的HDFS操作-答案 下载本文

内容发布更新时间 : 2024/5/1 20:30:03星期一 下面是文章的全部内容请认真阅读。

实验2熟悉常用的HDFS操作

1 实验目的

1. 理解HDFS在Hadoop体系结构中的角色; 2. 熟练使用HDFS操作常用的Shell命令; 3. 熟悉HDFS操作常用的Java API。

2 实验平台

操作系统:Linux

Hadoop版本:2.6.0或以上版本 JDK版本:1.6或以上版本 Java IDE:Eclipse

3 实验内容和要求

1. 编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务: 提示: 1) 部分Shell命令的参数路径只能是本地路径或者HDFS路径。 2) 若Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分。为保证操作正确,可指定路径前缀 hdfs:/// 或者 3) 注意区分相对路径与绝对路径 4) 具体命令的说明可参考教材或 (1) 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户

指定是追加到原有文件末尾还是覆盖原有的文件; Shell命令: 检查文件是否存在: ./hdfs dfs -test -e text.txt(执行完这一句不会输出结果,需要继续输入命令 \) 追加命令: ./hdfs dfs -appendTo text.txt 覆盖命令1: ./hdfs dfs -copyFromLocal -f local.txt text.txt 覆盖命令2: ./hdfs dfs -cp -f text.txt 也可以使用如下命令实现: (如下代码可视为一行代码,在终端中输入第一行代码后,直到输入 fi 才会真正执行): if $(./hdfs dfs -test -e text.txt); then $(./hdfs dfs -appendTo text.txt); else $(./hdfs dfs -copyFromLocal -f local.txt text.txt); fi Java代码: 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 { fs = (conf); return fs.exists(new Path(path)); } /** * 复制文件到指定路径 * 若路径已存在,则进行覆盖 */ public static void copyFromLocal conf, String local, String remote) throws IOException { fs = (conf); Path localPath = new Path(local); Path remotePath = new Path(remote); /* fs.copyFromLocalFile 第一个参数表示是否删除源文件,第二个参数表示是否覆盖 */ fs.copyFromLocal, true, localPath, remotePath); fs.close(); } /** * 追加文件内容 */ public static void appendTo conf, String local, String remote) throws IOException { fs = (conf); Path remotePath = new Path(remote); /* 创建一个文件读入流 */ in = new (local); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append(remotePath); /* 读写文件内容 */ byte[] data = new byte[1024]; int read = -1; while ( (read = in.read(data)) > 0 ) { out.write(data, 0, read); } out.close(); in.close(); fs.close(); } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String local = \ // 本地路径 String remote = \ // HDFS路径 String choice = \ // 若文件存在则追加到文件末尾 // String choice = \ // 若文件存在则覆盖 try { /* 判断文件是否存在 */ Boolean = false; if (HDFSApi.test(conf, remote)) { = true; System.out.println(remote + \已存在.\ } else { System.out.println(remote + \不存在.\ } /* 进行处理 */ if ( !) { // 文件不存在,则上传 HDFSApi.copyFromLocal, local, remote); System.out.println(local + \已上传至 \ } else if ( choice.equals(\ // 选择覆盖 HDFSApi.copyFromLocal, local, remote); System.out.println(local + \已覆盖 \ } else if ( choice.equals(\ // 选择追加 HDFSApi.appendTo, local, remote); System.out.println(local + \已追加至 \ } } catch (Exception e) { e.printStackTrace(); } } }

(2) 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对

下载的文件重命名; Shell命令: if $(./hdfs dfs -test -e ); then $(./hdfs dfs -copyToLocal text.txt ./text2.txt); else $(./hdfs dfs -copyToLocal text.txt ./text.txt); fi Java代码: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; public class HDFSApi { /** * 下载文件到本地 * 判断本地路径是否已存在,若已存在,则自动进行重命名 */ public static void copyToLocal(Configuration conf, String remote, String local) throws IOException { fs = (conf); Path remotePath = new Path(remote); File f = new ); /* 如果文件名存在,自动重命名(在文件名后面加上 _0, _1 ...) */ if (f.exists()) { System.out.println(local + \已存在.\ Integer i = 0; while (true) { f = new + \ if (!f.exists()) { local = local + \ break; } } System.out.println(\将重新命名为: \ } // 下载文件到本地 Path localPath = new Path(local); fs.copyToLocal, localPath); fs.close(); } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String local = \ // 本地路径 String remote = \ // HDFS路径 try { HDFSApi.copyToLocal(conf, remote, local); System.out.println(\下载完成\ } catch (Exception e) { e.printStackTrace(); } } }