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
- hdfs回顾总结(2)
- hdfs回顾总结(1)
- 2013个人回顾及总结(2)
- 软件工程视频总结(2)----整体回顾
- hdfs回顾总结(3)---使用hadoopRpc模拟namenode元数据的查询
- Hadoop回顾--HDFS工作流程
- 回顾——HDFS文件系统
- 回顾总结
- IOCP模型总结(总结回顾)
- HDFS总结
- HDFS总结
- HDFS总结
- HDFS学习笔记(2)HDFS运行机制
- 2017.11.2总结,回顾及成果
- C++基础总结(C++ Primer 回顾)
- SSH2开发回顾总结(一)
- 学年总结(2015-2016学年回顾)
- 数据结构(十七)赫夫曼树 树总结回顾
- First Day
- 随机数 与无穷大
- 命令历史记录
- java生成word
- Leetcode 287. Find the Duplicate Number
- hdfs回顾总结(2)
- 创建者模式
- lemon mossle OA 工程自己学习指南, BMP Activiti BPM JBPM
- (POJ 3140 Contestants Division)树型DP + 删边
- 单例设计模式之“懒汉式”+“饿汉式”
- linux开发环境搭建(4)-从SD卡启动uboot
- 使用java画出正三角形
- 简单理解堆排序
- 函数调用模式4种方式详解