黑马程序员技术交流社区

标题: 【上海校区】Hadoop 实验 - 熟悉常用的 HDFS 操作 [打印本页]

作者: 不二晨    时间: 2018-12-10 10:40
标题: 【上海校区】Hadoop 实验 - 熟悉常用的 HDFS 操作
实验目的
实验平台
实验内容和要求
一,编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务:二,编程实现一个类“MyFSDataInputStream”,该类继承“org.apache.hadoop.fs.FSDataInputStream”,要求如下:实现按行读取HDFS中指定文件的方法“readLine()”,如果读到文件末尾,则返回空,否则返回文件一行的文本。package com.zucc.tiny.hdfs;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;public class MyFSDataInputStream extends FSDataInputStream {    public MyFSDataInputStream(InputStream in) {        super(in);        // TODO Auto-generated constructor stub    }    public static String readline(Configuration conf, String remoteFilePath) throws IOException {         FileSystem fs = FileSystem.get(conf);         Path remotePath = new Path(remoteFilePath);         FSDataInputStream in = fs.open(remotePath);         BufferedReader d = new BufferedReader(new InputStreamReader(in));         String line = null;         if((line=d.readLine())!=null)         {             d.close();             in.close();             fs.close();             return line;         }         else{             d.close();             in.close();             fs.close();             return null;         }     }    public static void main(String[] args) {        Configuration conf = new Configuration();        conf.set("fs.default.name","hdfs://localhost:9000");        String remoteFilePath = "/user/tiny/text.txt";    // HDFS路径        try {            System.out.println("读取文件: " + remoteFilePath);            System.out.println(MyFSDataInputStream.readline(conf, remoteFilePath));            System.out.println("\n读取完成");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}三,查看Java帮助手册或其它资料,用“java.net.URL”和“org.apache.hadoop.fs.FsURLStreamHandlerFactory”编程完成输出HDFS中指定文件的文本到终端中。package com.zucc.tiny.hdfs;import java.io.IOException;import java.io.InputStream;import java.net.URL;import org.apache.hadoop.fs.*;import org.apache.hadoop.io.IOUtils;public class FsUrl {    static {        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());    }    public static void cat(String remoteFilePath) throws IOException {         InputStream in = null;         try {            in = new URL("hdfs","localhost",9000,remoteFilePath).openStream();            IOUtils.copyBytes(in, System.out, 4096, false);        } finally {            IOUtils.closeStream(in);        }     }     /**      * 主函数      */     public static void main(String[] args) {         String remoteFilePath = "/user/tiny/text.txt";    // HDFS路径         try {             System.out.println("读取文件: " + remoteFilePath);             FsUrl.cat(remoteFilePath);             System.out.println("\n读取完成");         } catch (Exception e) {             e.printStackTrace();         }     }    }【转载】仅作分享,侵删链接:https://juejin.im/entry/58a57f12128fe1006cafc8d9


作者: 不二晨    时间: 2018-12-11 15:53

作者: 小影姐姐    时间: 2018-12-13 14:52





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2