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

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

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

(8) 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结

尾; Shell命令: 追加到文件末尾:./hdfs dfs -appendToFile local.txt text.txt 追加到文件开头: (由于没有直接的命令可以操作,方法之一是先移动到本地进行操作,再进行上传覆盖): ./hdfs dfs -get text.txt cat text.txt >> local.txt ./hdfs dfs -copyFromLocal -f text.txt text.txt 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)); } /** * 追加文本内容 */ public static void appendContentToFile(Configuration conf, String content, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append(remotePath); out.write(content.getBytes()); out.close(); fs.close(); } /** * 追加文件内容 */ public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException {

主讲教师:刘斌 第16页

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

FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); /* 创建一个文件读入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ 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 moveToLocalFile(Configuration conf, String remoteFilePath, String localFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); Path localPath = new Path(localFilePath); fs.moveToLocalFile(remotePath, localPath); } /** * 创建文件 */ 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 void main(String[] args) {

主讲教师:刘斌 第17页

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

Configuration conf = new Configuration(); conf.set(\ String remoteFilePath = \ // HDFS文件 String content = \新追加的内容\\n\ String choice = \ //追加到文件末尾 // String choice = \ // 追加到文件开头 try { /* 判断文件是否存在 */ if ( !HDFSApi.test(conf, remoteFilePath) ) { System.out.println(\文件不存在: \ } else { if ( choice.equals(\追加在文件末尾 HDFSApi.appendContentToFile(conf, content, remoteFilePath); System.out.println(\已追加内容到文件末尾\ } else if ( choice.equals(\ { // 追加到文件开头 /* 没有相应的api可以直接操作,因此先把文件移动到本地,创建一个新的HDFS,再按顺序追加内容 */ String localTmpPath = \ HDFSApi.moveToLocalFile(conf, remoteFilePath, localTmpPath); // 移动到本地 HDFSApi.touchz(conf, remoteFilePath); // 创建一个新文件 HDFSApi.appendContentToFile(conf, content, remoteFilePath); // 先写入新内容 HDFSApi.appendToFile(conf, localTmpPath, remoteFilePath); // 再写入原来内容 System.out.println(\已追加内容到文件开头: \ } } } catch (Exception e) { e.printStackTrace(); } } } 主讲教师:刘斌 第18页

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

(9) 删除HDFS中指定的文件; Shell命令: ./hdfs dfs -rm text.txt Java命令: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; public class HDFSApi { /** * 删除文件 */ 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();

主讲教师:刘斌 第19页

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

conf.set(\ String remoteFilePath = \ // HDFS文件 try { if ( HDFSApi.rm(conf, remoteFilePath) ) { System.out.println(\文件删除: \ } else { System.out.println(\操作失败(文件不存在或删除失败)\ } } catch (Exception e) { e.printStackTrace(); } } }

(10) 删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录; Shell命令: 删除目录(如果目录非空则会提示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.*; public class HDFSApi { /**

主讲教师:刘斌 第20页