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

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

实验2 熟悉常用的HDFS操作实验手册

(6) 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所

在目录不存在,则自动创建目录; Shell命令: 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); fi 删除文件:./hdfs dfs -rm dir1/dir2/filename 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 { FileSystem fs = FileSystem.get(conf); return fs.exists(new Path(path)); } /** * 创建目录 */

主讲教师:刘斌 第11页

实验2 熟悉常用的HDFS操作实验手册

public static boolean mkdir(Configuration conf, String remoteDir) throws IOException { FileSystem fs = FileSystem.get(conf); Path dirPath = new Path(remoteDir); boolean result = fs.mkdirs(dirPath); fs.close(); return result; } /** * 创建文件 */ public static void touchz(Configuration conf, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); FSDataOutputStream outputStream = fs.create(remotePath); outputStream.close(); fs.close(); } /** * 删除文件 */ public static boolean rm(Configuration conf, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); boolean result = fs.delete(remotePath, false); fs.close(); return result; } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remoteFilePath = \ // HDFS路径 String remoteDir = \ // HDFS路径对应的目录 try { /* 判断路径是否存在,存在则删除,否则进行创建 */ if ( HDFSApi.test(conf, remoteFilePath) ) { HDFSApi.rm(conf, remoteFilePath); // 删除 System.out.println(\删除路径: \

主讲教师:刘斌 第12页

实验2 熟悉常用的HDFS操作实验手册

} else { if ( !HDFSApi.test(conf, remoteDir) ) { // 若目录不存在,则进行创建 HDFSApi.mkdir(conf, remoteDir); System.out.println(\创建文件夹: \ } HDFSApi.touchz(conf, remoteFilePath); System.out.println(\创建路径: \ } } catch (Exception e) { e.printStackTrace(); } } }

(7) 提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,

如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录; Shell命令: 创建目录:./hdfs dfs -mkdir -p dir1/dir2 删除目录(如果目录非空则会提示not empty,不执行删除):./hdfs dfs -rmdir dir1/dir2 强制删除目录:./hdfs dfs -rm -R dir1/dir2 Java代码: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*;

主讲教师:刘斌 第13页

实验2 熟悉常用的HDFS操作实验手册

public class HDFSApi { /** * 判断路径是否存在 */ public static boolean test(Configuration conf, String path) throws IOException { FileSystem fs = FileSystem.get(conf); return fs.exists(new Path(path)); } /** * 判断目录是否为空 * true: 空,false: 非空 */ public static boolean isDirEmpty(Configuration conf, String remoteDir) throws IOException { FileSystem fs = FileSystem.get(conf); Path dirPath = new Path(remoteDir); RemoteIterator remoteIterator = fs.listFiles(dirPath, true); return !remoteIterator.hasNext(); } /** * 创建目录 */ public static boolean mkdir(Configuration conf, String remoteDir) throws IOException { FileSystem fs = FileSystem.get(conf); Path dirPath = new Path(remoteDir); boolean result = fs.mkdirs(dirPath); fs.close(); return result; } /** * 删除目录 */ public static boolean rmDir(Configuration conf, String remoteDir) throws IOException { FileSystem fs = FileSystem.get(conf); Path dirPath = new Path(remoteDir); /* 第二个参数表示是否递归删除所有文件 */ boolean result = fs.delete(dirPath, true); fs.close(); return result; }

主讲教师:刘斌 第14页

实验2 熟悉常用的HDFS操作实验手册

/** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remoteDir = \ // HDFS目录 Boolean forceDelete = false; // 是否强制删除 try { /* 判断目录是否存在,不存在则创建,存在则删除 */ if ( !HDFSApi.test(conf, remoteDir) ) { HDFSApi.mkdir(conf, remoteDir); // 创建目录 System.out.println(\创建目录: \ } else { if ( HDFSApi.isDirEmpty(conf, remoteDir) || forceDelete ) { // 目录为空或强制删除 HDFSApi.rmDir(conf, remoteDir); System.out.println(\删除目录: \ } else { // 目录不为空 System.out.println(\目录不为空,不删除: \ } } } catch (Exception e) { e.printStackTrace(); } } }

主讲教师:刘斌 第15页