hdfs回顾总结(2)

来源:互联网 发布:源码分享论坛 编辑:程序博客网 时间:2024/06/05 20:41

基础的流式上传和下载以及block的获取

相对那些封装好的方法而言的更底层一些的操作方式
上层那些mapreduce、spark等运算框架,去hdfs中获取数据的时候,
就是调的这种底层的api

package hdfs;import org.apache.commons.io.IOUtils;import org.apache.commons.lang.ArrayUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.*;import org.junit.Before;import org.junit.Test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;/** * 相对那些封装好的方法而言的更底层一些的操作方式 * 上层那些mapreduce、spark等运算框架,去hdfs中获取数据的时候, * 就是调的这种底层的api * Created by tianjun on 2017/3/15 0015. */public class HdfsStreamAccess {    FileSystem fs = null;    Configuration conf = null;    @Before    public void init() throws IOException, URISyntaxException, InterruptedException {        conf = new Configuration();        /**         * 方法1:vm参数 -DHADOOP_USER_NAME=root         * 或者是 System.setProperty("HADOOP_USER_NAME","root");         *///        conf.set("fs.defaultFS","hdfs://mini01:9000");//        fs = FileSystem.get(conf);        /**         * 方法2         */        fs = FileSystem.get(new URI("hdfs://cmcc"), conf, "dev");    }    /**     * 通过流的方式上传     * @throws IOException     */    @Test    public void testLoad() throws IOException {        FSDataOutputStream outputStream = fs.create(new Path("/test/wc/input/a.txt"));        FileInputStream inputStream = new FileInputStream("e:/hello.txt");        //utils工具会帮助把流关掉        IOUtils.copy(inputStream,outputStream);    }    /**     * 通过流的方式下载     * @throws IOException     */    @Test    public void testDownLoad() throws IOException {        FSDataInputStream inputStream = fs.open(new Path("/test/wc/input/a.txt"));        FileOutputStream outputStream = new FileOutputStream("e:/hello.txt");        IOUtils.copy(inputStream,outputStream);    }    /**     * 随机读取(定长)     * @throws IOException     */    @Test    public void testRadomAccess() throws IOException {        FSDataInputStream inputStream = fs.open(new Path("/test/wc/input/a.txt"));        //定义从第12个字节开始读(方法一)//        inputStream.seek(3);        FileOutputStream outputStream = new FileOutputStream("e:/hello.txt.part2");//        IOUtils.copy(inputStream,outputStream);        //方法二        IOUtils.copyLarge(inputStream,outputStream,5,10);    }    /**     * 输出到屏幕     * @throws IOException     */    @Test    public void testCat() throws IOException {        FSDataInputStream inputStream = fs.open(new Path("/test/wc/input/a.txt"));        IOUtils.copy(inputStream,System.out);    }    /**     * 读取指定的block     */    @Test    public void testCat2() throws IOException {        int i = 1;        FSDataInputStream in = fs.open(new Path("/cenos-6.5-hadoop-2.6.4.tar.gz"));        FileOutputStream out = new FileOutputStream("e:/bak1");        //拿到文件信息        FileStatus[] listStatus = fs.listStatus(new Path("/cenos-6.5-hadoop-2.6.4.tar.gz"));        //获取这个文件所有的block信息        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());        for (BlockLocation b : fileBlockLocations) {            System.out.println("第" + i + "块");            System.out.println("块开始偏移量:" + b.getOffset());            System.out.println("块大小:" + b.getLength()/1024/1024+"M");            System.out.println("块所在主机:" + ArrayUtils.toString(b.getHosts()));            ++i;        }        //获取第一个block写入输出流        IOUtils.copyLarge(in, out, 0, fileBlockLocations[0].getLength());    }}

最后一个test的结果如下:

1块块开始偏移量:0块大小:134217728(128M)块所在主机:{mini02,mini03}2块块开始偏移量:134217728块大小:46595337(44M)块所在主机:{mini03,mini02}
0 0