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

内容发布更新时间 : 2024/4/28 20:27:35星期一 下面是文章的全部内容请认真阅读。

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

* 判断目录是否为空 * 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 rmDir(Configuration conf, String remoteDir, boolean recursive) throws IOException { FileSystem fs = FileSystem.get(conf); Path dirPath = new Path(remoteDir); /* 第二个参数表示是否递归删除所有文件 */ boolean result = fs.delete(dirPath, recursive); fs.close(); return result; } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remoteDir = \ // HDFS目录 Boolean forceDelete = false; // 是否强制删除 try { if ( !HDFSApi.isDirEmpty(conf, remoteDir) && !forceDelete ) { System.out.println(\目录不为空,不删除\ } else { if ( HDFSApi.rmDir(conf, remoteDir, forceDelete) ) { System.out.println(\目录已删除: \ } else { System.out.println(\操作失败\ } } } catch (Exception e) {

主讲教师:刘斌 第21页

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

e.printStackTrace(); } } }

(11) 在HDFS中,将文件从源路径移动到目的路径。 Shell命令: ./hdfs dfs -mv text.txt text2.txt Java代码: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; public class HDFSApi { /** * 移动文件 */ public static boolean mv(Configuration conf, remoteToFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(remoteFilePath); Path dstPath = new Path(remoteToFilePath); boolean result = fs.rename(srcPath, dstPath); fs.close(); return result; }

String remoteFilePath, String 主讲教师:刘斌 第22页

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

/** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remoteFilePath = \ // 源文件HDFS路径 String remoteToFilePath = \ // 目的HDFS路径 try { if ( HDFSApi.mv(conf, remoteFilePath, remoteToFilePath) ) { System.out.println(\将文件 \移动到 \ } else { System.out.println(\操作失败(源文件不存在或移动失败)\ } } catch (Exception e) { e.printStackTrace(); } } }

2. 编程实现一个类“MyFSDataInputStream”,该类继承“org.apache.hadoop.fs.FSDataInput

Stream”,要求如下:实现按行读取HDFS中指定文件的方法“readLine()”,如果读到文件末尾,则返回空,否则返回文件一行的文本。 Java代码: import org.apache.hadoop.conf.Configuration;

主讲教师:刘斌 第23页

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

import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import java.io.*; public class MyFSDataInputStream extends FSDataInputStream { public MyFSDataInputStream(InputStream in) { super(in); } /** * 实现按行读取 * 每次读入一个字符,遇到\结束,返回一行内容 */ public static String readline(BufferedReader br) throws IOException { char[] data = new char[1024]; int read = -1; int off = 0; // 循环执行时,br 每次会从上一次读取结束的位置继续读取,因此该函数里,off 每次都从0开始 while ( (read = br.read(data, off, 1)) != -1 ) { if (String.valueOf(data[off]).equals(\) { off += 1; break; } off += 1; } if (off > 0) { return String.valueOf(data); } else { return null; } } /** * 读取文件内容 */ public static void cat(Configuration conf, String remoteFilePath) throws IOException { FileSystem fs = FileSystem.get(conf); Path remotePath = new Path(remoteFilePath); FSDataInputStream in = fs.open(remotePath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = null; while ( (line = MyFSDataInputStream.readline(br)) != null ) {

主讲教师:刘斌 第24页

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

System.out.println(line); } br.close(); in.close(); fs.close(); } /** * 主函数 */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remoteFilePath = \ // HDFS路径 try { MyFSDataInputStream.cat(conf, remoteFilePath); } catch (Exception e) { e.printStackTrace(); } } }

3. 查看Java帮助手册或其它资料,用“java.net.URL”和“org.apache.hadoop.fs.FsURLStrea

mHandlerFactory”编程完成输出HDFS中指定文件的文本到终端中。 Java代码: import org.apache.hadoop.fs.*; import org.apache.hadoop.io.IOUtils;

主讲教师:刘斌 第25页