对于MapReduce等框架来说,需要有一套更底层的API来获取某个指定文件中的一部分数据,而不是一整个文件

因此使用流的方式来操作 HDFS上的文件,可以实现读取指定偏移量范围的数据

1.客户端测试类代码:

package cn.bigdata.hdfs;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.junit.Before; public class HdfsStreamAcess {
//获取客户端操作hdfs的实例对象
private FileSystem fs = null;
Configuration conf = null;
@Before
public void inin() throws IOException, InterruptedException, URISyntaxException{
conf = new Configuration();
//拿到一个文件系统操作的客户端实例对象,最后一个参数为用户名
fs = FileSystem.get(new URI("hdfs://shizhan2:9000"),conf,"root");
}
}

2.流式上传文件:

    //流式上传文件
@Test
public void testUploadWithStream() throws IllegalArgumentException, IOException{
//true:该文件夹存在就覆盖 IOUtils:工具类
FSDataOutputStream outputstream = fs.create(new Path("/angelababy.love"), true);
FileInputStream input = new FileInputStream("c:/xxx.txt");
IOUtils.copy(input, outputstream);
}

3.流式下载文件:

    //流式下载文件
@Test
public void testDownloadWithStream() throws Exception{
FSDataInputStream in = fs.open(new Path("/angelababy.love"));
FileOutputStream out = new FileOutputStream("d:/access_stream.log");
IOUtils.copy(in, out);
}

4.流式读取指定长度的文件:

//文件的随机读写
@Test
public void testRandomAccess() throws Exception{
FSDataInputStream in = fs.open(new Path("/regist-copy.log"));
FileOutputStream out = new FileOutputStream("d:/random_stream.log");
IOUtils.copyLarge(in, out, 1*1024*1024, 1*1024*1024); // 从1M位置开始读,读1M
}

hdfs支持随机定位进行文件读取,而且可以方便地读取指定长度,用于上层分布式运算框架并发处理数据

5.控制台打印HDFS文件内容:

@Test
public void testCat() throws Exception{
FSDataInputStream in = fs.open(new Path("/angelababy.love"));
IOUtils.copy(in,System.out);
}

6.递归列出指定目录下所有子文件夹中的文件:

@Test
public void testLs() throws Exception {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while(listFiles.hasNext()){
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("blocksize: " +fileStatus.getBlockSize());
System.out.println("owner: " +fileStatus.getOwner());
System.out.println("Replication: " +fileStatus.getReplication());
System.out.println("Permission: " +fileStatus.getPermission());
System.out.println("Name: " +fileStatus.getPath().getName());
System.out.println("------------------");
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for(BlockLocation b:blockLocations){
System.out.println("块起始偏移量: " +b.getOffset());
System.out.println("块长度:" + b.getLength());
//块所在的datanode节点
String[] datanodes = b.getHosts();
for(String dn:datanodes){
System.out.println("datanode:" + dn);
}
}
}
}

7.获取文件块信息:

    @Test
public void testGetFileBlock() throws Exception{
FileStatus fileStatus = fs.getFileStatus(new Path("/pcre-8.35.tar.gz"));
BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for (BlockLocation bl : blockLocations) {
System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
}

Hadoop_11_HDFS的流式 API 操作的更多相关文章

  1. Java8 流式 API(`java.util.stream`)

    熟悉 ES6 的开发者,肯定对数组的一些方法不是很陌生:map.filter 等.在对一组对象进行统一操作时,利用这些方法写出来的代码比常规的迭代代码更加的简练.在 C♯ 中,有 LINQ 来实现.那 ...

  2. jackson 流式API

    http://www.cnblogs.com/lee0oo0/articles/2652528.html Jackson提供了三种可选的JSON处理方法 1.流式API     com.fasterx ...

  3. FunDA(2)- Streaming Data Operation:流式数据操作

    在上一集的讨论里我们介绍并实现了强类型返回结果行.使用强类型主要的目的是当我们把后端数据库SQL批次操作搬到内存里转变成数据流式按行操作时能更方便.准确.高效地选定数据字段.在上集讨论示范里我们用集合 ...

  4. Java 8 集合之流式(Streams)操作, Streams API 详解

    因为当时公司的业务需要对集合进行各种各样的业务逻辑操作,为了提高性能,就用到了这个东西,因为以往我们以前用集合都是需要去遍历(串行),所以效率和性能都不是特别的好,而Streams就可以使用并行的方式 ...

  5. Go 通过 Map/Filter/ForEach 等流式 API 高效处理数据

    什么是流处理 如果有 java 使用经验的同学一定会对 java8 的 Stream 赞不绝口,极大的提高了们对于集合类型数据的处理能力. int sum = widgets.stream() .fi ...

  6. Jackson流式API

    public class JacksonTester {   public static void main(String args[]){    JacksonTester tester = new ...

  7. lambda表达式以及stream流式api用法

    https://www.cnblogs.com/aoeiuv/p/5911692.html 这篇文章讲的简单全面,记录下 kotlin一些符号的用法 https://www.cnblogs.com/l ...

  8. Mysql中使用JDBC流式查询避免数据量过大导致OOM

    一.前言 java 中MySQL JDBC 封装了流式查询操作,通过设置几个参数,就可以避免一次返回数据过大导致 OOM. 二.如何使用 2.1 之前查询 public void selectData ...

  9. JDK8新特性(二) 流式编程Stream

    流式编程是1.8中的新特性,基于常用的四种函数式接口以及Lambda表达式对集合类数据进行类似流水线一般的操作 流式编程分为大概三个步骤:获取流 → 操作流 → 返回操作结果 流的获取方式 这里先了解 ...

随机推荐

  1. 【FIORI系列】SAP 一文读懂SAP Fiori是什么

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FIORI系列]SAP 一文读懂SAP Fio ...

  2. 架构模式: 命令查询职责分离 (CQRS)

    架构模式: 命令查询职责分离 (CQRS) 问题 如何在微服务架构中实现查询 结论 将应用程序拆分为两部分:命令端和查询端.命令端处理创建,更新和删除请求,并在数据更改时发出事件.查询端通过对一个或多 ...

  3. Spring boot + Jpa + Maven + Mysql 初级整合

    1.使用Idea创建spring boot工程的博客 https://www.cnblogs.com/black-spike/p/8017768.html 2.本篇博客参考网址 https://blo ...

  4. xtrabackup备份失败(error writing file 'UNOPENED')

    xtrabackup备份失败 解决了,是因为limit open files值设置太小了 (3)修改资源限制参数 vi /etc/security/limits.conf nproc:用户创建进程数限 ...

  5. PHP中的闭包

    1.语句结构 在PHP中,由于存在函数内部不能访问全局作用的,所以就需要一种可以引入上一级作用域的语法结构,这种就是 function () use () {} 将需要引入到这个函数作用于内的变量写入 ...

  6. Spring4学习回顾之路05—自动装配,Bean的继承,依赖和作用域

    自动装配 xml配置里的Bean的自动装配,Spring IOC容器可以自动装配Bean,仅仅需要做的是在<bean>标签里的autowire属性里指定自动装配的模式. ①byType(根 ...

  7. 线性基求交(2019牛客国庆集训派对day4)

    题意:https://ac.nowcoder.com/acm/contest/1109/C 问你有几个x满足A,B集合都能XOR出x. 思路: 就是线性基求交后,有几个基就是2^几次方. #defin ...

  8. 并不对劲的复健训练-bzoj5253:loj2479:p4384:[2018多省联考]制胡窜

    题目大意 给出一个字符串\(S\),长度为\(n\)(\(n\leq 10^5\)),\(S[l:r]\)表示\(S_l,S_{l+1}...,S_r\)这个子串.有\(m\)(\(m\leq 3\t ...

  9. Windows 安装和配置 WSL

    Windows 安装和配置 WSL 什么是 WSL 引用百度百科的一段话: Windows Subsystem for Linux(简称WSL)是一个为在Windows 10上能够原生运行Linux二 ...

  10. 【已解决】Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.

    目录 问题 解决办法 备注 问题 在项目中,我们使用Spring的@Autowired注解去引入其他类时有时候阿里的编码规约插件就会提示:"Field injection is not re ...