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

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

尾; Shell命令: 追加到文件末尾:./hdfs dfs -appendTo 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 { fs = (conf); return fs.exists(new Path(path)); } /** * 追加文本内容 */ public static void appendContentTo conf, String content, String remote) throws IOException { fs = (conf); Path remotePath = new Path(remote); /* 创建一个文件输出流,输出的内容将追加到文件末尾 */ FSDataOutputStream out = fs.append(remotePath); out.write(content.getBytes()); out.close(); 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 moveToLocal conf, String remote, String local) throws IOException { fs = (conf); Path remotePath = new Path(remote); Path localPath = new Path(local); fs.moveToLocal, localPath); } /** * 创建文件 */ public static void touchz(Configuration conf, String remote) throws IOException { fs = (conf); Path remotePath = new Path(remote); FSDataOutputStream outputStream = fs.create(remotePath); outputStream.close(); fs.close(); } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remote = \ // HDFS文件 String content = \新追加的内容\\n\ String choice = \ //追加到文件末尾 // String choice = \ // 追加到文件开头 try { /* 判断文件是否存在 */ if ( !HDFSApi.test(conf, remote) ) { System.out.println(\文件不存在: \ } else { if ( choice.equals(\追加在文件末尾 HDFSApi.appendContentTo, content, remote); System.out.println(\已追加内容到文件末尾\ } else if ( choice.equals(\ { // 追加到文件开头 /* 没有相应的api可以直接操作,因此先把文件移动到本地,创建一个新的HDFS,再按顺序追加内容 */ String localTmpPath = \ HDFSApi.moveToLocal, remote, localTmpPath); // 移动到本地 HDFSApi.touchz(conf, remote); // 创建一个新文件 HDFSApi.appendContentTo, content, remote); // 先写入新内容 HDFSApi.appendTo, localTmpPath, remote); // 再写入原来内容 System.out.println(\已追加内容到文件开头: \ } } } catch (Exception e) { e.printStackTrace(); } } }

(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 remote) throws IOException { fs = (conf); Path remotePath = new Path(remote); boolean result = fs.delete(remotePath, false); fs.close(); return result; } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remote = \ // HDFS文件 try { if ( HDFSApi.rm(conf, remote) ) { 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 { /**

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4