3.1块

  • 显示块信息

    % hdfs fsck / -files -blocks
    

3.5 Java接口

3.5.1从hadoop URL读取数据

使用java.net.URL 对象来打开一个数据流

InputStream in = null;
try {in = new URL("hdfs://host/path").openStream();// process in} finally {IOUtils.closeStream(in);
}

用URLStreamHandler以标准输出格式像是hadoop文件系统的文件

public class URLCat {static {URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());}public static void main(String[] args) throws Exception {InputStream in = null;try {in = new URL(args[0]).openStream();IOUtils.copyBytes(in, System.out, 4096, false);} finally {IOUtils.closeStream(in);}}
}

usage

% export HADOOP_CLASSPATH=hadoop-examples.jar
% hadoop URLCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.

3.5.2使用filesystem api读取数据

  • FileSystem
public class FileSystemCat {public static void main(String[] args) throws Exception {String uri = args[0];Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(uri), conf);InputStream in = null;try {in = fs.open(new Path(uri));IOUtils.copyBytes(in, System.out, 4096, false);} finally {IOUtils.closeStream(in);}}
}

usage

% hadoop FileSystemCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
  • FSDataInputStream

    package org.apache.hadoop.fs;public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable {// implementation elided
    }public interface Seekable {void seek(long pos) throws IOException;long getPos() throws IOException;
    }public class FileSystemDoubleCat {public static void main(String[] args) throws Exception {String uri = args[0];Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(uri), conf);FSDataInputStream in = null;try {in = fs.open(new Path(uri));IOUtils.copyBytes(in, System.out, 4096, false);in.seek(0); // go back to the start of the fileIOUtils.copyBytes(in, System.out, 4096, false);} finally {IOUtils.closeStream(in);}}
    }
    

    usage

    % hadoop FileSystemDoubleCat hdfs://localhost/user/tom/quangle.txt
    On the top of the Crumpetty Tree
    本文档由Linux公社 www.linuxidc.com 收集整理
    The Quangle Wangle sat,
    But his face you could not see,
    On account of his Beaver Hat.
    On the top of the Crumpetty Tree
    The Quangle Wangle sat,
    But his face you could not see,
    On account of his Beaver Hat.
    

3.5.3 写入数据

  • 将本地文件复制到hdfs并显示进度
public class FileCopyWithProgress {public static void main(String[] args) throws Exception {String localSrc = args[0];String dst = args[1];InputStream in = new BufferedInputStream(new FileInputStream(localSrc));Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(dst), conf);OutputStream out = fs.create(new Path(dst), new Progressable() {public void progress() {System.out.print(".");}});IOUtils.copyBytes(in, out, 4096, true);}
}

usage

% hadoop FileCopyWithProgress input/docs/1400-8.txt
hdfs://localhost/user/tom/1400-8.txt
.................

3.5.4 目录

  • 显示文件状态信息
public class ShowFileStatusTest {private MiniDFSCluster cluster; // use an in-process HDFS cluster for testingprivate FileSystem fs;@Beforepublic void setUp() throws IOException {Configuration conf = new Configuration();if (System.getProperty("test.build.data") == null) {System.setProperty("test.build.data", "/tmp");}cluster = new MiniDFSCluster.Builder(conf).build();fs = cluster.getFileSystem();OutputStream out = fs.create(new Path("/dir/file"));out.write("content".getBytes("UTF-8"));out.close();}@Afterpublic void tearDown() throws IOException {if (fs != null) {fs.close();}if (cluster != null) {cluster.shutdown();}}@Test(expected = FileNotFoundException.class)public void throwsFileNotFoundForNonExistentFile() throws IOException {fs.getFileStatus(new Path("no-such-file"));}@Testpublic void fileStatusForFile() throws IOException {Path file = new Path("/dir/file");FileStatus stat = fs.getFileStatus(file);assertThat(stat.getPath().toUri().getPath(), is("/dir/file"));assertThat(stat.isDirectory(), is(false));assertThat(stat.getLen(), is(7L));assertThat(stat.getModificationTime(), is(lessThanOrEqualTo(System.currentTimeMillis())));assertThat(stat.getReplication(), is((short) 1));assertThat(stat.getBlockSize(), is(128 * 1024 * 1024L));assertThat(stat.getOwner(), is(System.getProperty("user.name")));assertThat(stat.getGroup(), is("supergroup"));assertThat(stat.getPermission().toString(), is("rw-r--r--"));}@Testpublic void fileStatusForDirectory() throws IOException {Path dir = new Path("/dir");FileStatus stat = fs.getFileStatus(dir);assertThat(stat.getPath().toUri().getPath(), is("/dir"));assertThat(stat.isDirectory(), is(true));assertThat(stat.getLen(), is(0L));assertThat(stat.getModificationTime(), is(lessThanOrEqualTo(System.currentTimeMillis())));assertThat(stat.getReplication(), is((short) 0));assertThat(stat.getBlockSize(), is(0L));assertThat(stat.getOwner(), is(System.getProperty("user.name")));assertThat(stat.getGroup(), is("supergroup"));assertThat(stat.getPermission().toString(), is("rwxr-xr-x"));}
}
  • 显示一个hdfs的一些路径文件信息
public class ListStatus {public static void main(String[] args) throws Exception {String uri = args[0];本文档由Linux公社 www.linuxidc.com 收集整理Configuration conf = new Configuration();FileSystem fs = FileSystem.get(URI.create(uri), conf);Path[] paths = new Path[args.length];for (int i = 0; i < paths.length; i++) {paths[i] = new Path(args[i]);}FileStatus[] status = fs.listStatus(paths);Path[] listedPaths = FileUtil.stat2Paths(status);for (Path p : listedPaths) {System.out.println(p);}}
}

usage

% hadoop ListStatus hdfs://localhost/ hdfs://localhost/user/tom
hdfs://localhost/user
hdfs://localhost/user/tom/books
hdfs://localhost/user/tom/quangle.txt
  • PathFilter 用于排除匹配一个正则表达式的路劲
public class RegexExcludePathFilter implements PathFilter {private final String regex;public RegexExcludePathFilter(String regex) {this.regex = regex;}public boolean accept(Path path) {return !path.toString().matches(regex);}
}

usage

fs.globStatus(new Path("/2007/*/*"), new RegexExcludeFilter("^.*/2007/12/31$"))

3.6 数据流

3.6.1文件读取

distributionFileSystem通过使用RPC来调用名称节点,以确定文件开头部分的块的位置。对于每一个快,名称节点返回具有该块副本的数据节点地址。此外,这些数据节点根据他们与客户点的距离来排序(网络集群的拓扑)如果该客户端本身就为数据节点(MR任务中),便从本地数据节点读取。

  • 网络拓扑与hadoop

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yIjhVNUe-1607581109250)(C:\Users\18451\AppData\Roaming\Typora\typora-user-images\image-20201127094028503.png)\

3.6.2文件写入剖析

  • 副本的放置

所有的副本都放在一个节点基本上不会损失写入贷款,但这样并没有实现真正的冗余。离机架读取的带宽很高。

  • hadoop的策略

在与客户点相同的节点上放置第一个副本(如果客户端在集群之外,就可以随机选取节点,不过系统会避免挑选太满太忙的节点。)

一旦选定副本放置的位置,就会生成一个管线,会考虑到网络拓扑。

如图:

在写入数据的时候,有以下代码执行。当前写入的块不能被客户端读取。超过一个块的数据后,新读取者才能看见第一个块。

Path p = new Path("p");
fs.create(p);
//报告文件已存在
assertThat(fs.exists(p), is(true));Path p = new Path("p");
OutputStream out = fs.create(p);
out.write("content".getBytes("UTF-8"));
out.flush();
//报告文件长度为0
assertThat(fs.getFileStatus(p).getLen(), is(0L));
  • sync检查写入的数据,对所有读取者都是可见切一致的
Path p = new Path("p");
OutputStream out = fs.create(p);
out.write("content".getBytes("UTF-8"));
out.flush();
assertThat(fs.getFileStatus(p).getLen(), is(0L));

3.7 distcp并行复制

#复制目录
% hadoop distcp hdfs://namenode1/foo hdfs://namenode2/bar
#第二个节点产生目录/bar/foo,bar不存在的话就自动创建。
#复制文件
% hadoop distcp dir1 dir2

添加参数(-overwrite、-update、-m、-delete)

% hadoop distcp -update hdfs://namenode1/foo hdfs://namenode2/bar/foo#注意要相同目录
% hadoop distcp -update -delete -p hdfs://namenode1/foo hdfs://namenode2/foo

​ delete标志使distcp删除源文件中不存在的目标文件或目录,而-p表示保留权限、块大小和复制等文件状态属性。您可以在不带参数的情况下运行distcp,以查看精确的使用指令。

  • distcp 是一个MR任务,复制工作由map来完成,没有reduce。

-m参数为设置该MR任务的map数量。

例如,若总文件为1000 GB,-m 1000会分配1000个map,平均每个复制1 GB 。

  • 不同版本的 HDFS 上使用 distcp ,使用hdfs协议是会失败的,因为 RPC 系统是不兼容的。可以使用基于http协议的hftp文件系统从源中读取。
% hadoop distcp webhdfs://namenode1:50070/foo webhdfs://namenode2:50070/foo

3.8hadoop归档文件

hadoop archives 或HAR文件,是一个更高效的将文件放入HDFS块中的文件存档设备,在减少名称节点内存使用的同时,仍然允许对文件进行透明的访问。具体来说,HAR可以被用作MR的输入。

  • HAR文件总是有一个har的扩展名,这是必须的。

usage

#创建
hadoop archive -archiveName files.har /mu/files /my#查看
hadoop fs -lsr har:///mu/files.har
为什么har文件必须要有har扩展名?因为har文件系统将har URI转换成为一个基础文件系统的URI。如下所示。
hadoop fs -lsr har://hdfs-localhost:8082/my/files.har#删除
hadoop fs -rmr /my/files.har
  • 不足

1、占用和源文件同样大小的磁盘空间,目前还不支持档案压缩。

2、一旦创建,archives不可改变,要增删的话,必须要重新创建归档文件。

3、没有归档inputformat可以打包多个文件到一个单一的MapReduce,所以即使在har文件中处理许多小文件,也仍然低效的。(第七章有解决此问题的另一种方法)

【hadoop权威指南第四版】第三章hadoop分布式文件系统【笔记+代码】相关推荐

  1. 【hadoop权威指南第四版】第六章MR的工作原理【笔记+代码】

    6.1 运行MR作业 工作原理 四大模块: 客户端,提交MR作业. jobtracker,协调作业的运行.jobtracker 是一个java应用程序,主类是Jobtracker. tasktrack ...

  2. 【hadoop权威指南第四版】第七章MR的类型与格式【笔记+代码】

    7.1MR类型 7.2 输入格式 7.2.1输入分片与记录 InputFormat类的层次结构 每一个map操作只处理一个输入分片,并且一个一个地处理每条记录,也就是一个键值对. 在数据库中,一个输入 ...

  3. 【hadoop权威指南第四版】第五章MR应用【笔记+代码】

    5.1 API的配置 配置文件 <?xml version="1.0"?> <configuration> <property> <nam ...

  4. Hadoop权威指南(第3版) 修订版(带目录书签) 中文PDF--高清晰

    一.下载地址(永久有效) 百度云盘下载(公开永久):Hadoop权威指南(第3版) 修订版(带目录书签) 中文PDF高清晰 CSDN积分下载:Hadoop权威指南(第3版)+高清晰 二.数据的存储和分 ...

  5. 《JavaScript权威指南第四版》 电子版 电子书下载

    JavaScript权威指南第四版 图书评价:★★★★☆ 图书语言:简体图书 图书大小:19.11MB 图书格式:PDF 图书作者:David Flanagan 更新日期:2006-05-23 下载次 ...

  6. 翻译:《JavaScript 权威指南(第5版)》第一章(一)

    声明:翻译只有一个目的:学习用途.若有版权问题请及时联系本人. 本贴文根据篇幅将第一章的翻译分为两个部分,这是第一部分的内容. Chapter 1. Introduction to JavaScrip ...

  7. Hadoop权威指南(第二版)pdf中文版

    今天终于找到 hadoop权威指南第二版的中文pdf版本了,发给大家共享一下 下载地址:http://dl.dbank.com/c0hh1arjiz ------------------------- ...

  8. css权威指南第四版_16个非常有用的CSS伪选择器,你千万不要错过了!

    英文 | https://blog.bitsrc.io/css-pseudo-selectors-you-never-knew-existed-b5c0ddaa8116译文 | https://jue ...

  9. 汇编语言 王爽 第四版 第三章 检测点3.2

    汇编语言 王爽 第四版 课后检测点 课后实验 持续更新~~ 检测点3.2 1 补全下面的程序,使其可以将10000H-1000FH中的8个字,逆序拷贝到20000H-2000FH中. mov ax,1 ...

最新文章

  1. 一次“ora-12170 tns 连接超时”的经历
  2. Eclipse中修改tomcat内存大小
  3. C语言中的位域的使用
  4. 敏捷开发的6个实战经验
  5. 正确使用PresentModalViewController
  6. socket 选项 详细说明
  7. 解决CentOS 中部署JasperReport时出错的问题。错误:net.sf.jasperreports.engine.util.JRFontNotFoundException: Font '宋体
  8. JavaScript的三大基本操作案例讲解
  9. 另一种“人老心不老”
  10. Linux 下 4 种禁用 Root 登陆的方法,你掌握了哪几种呢?
  11. 一个32岁入门的70后程序员给我的启示
  12. sql查询练习题的参考答案
  13. python安装包的路径
  14. 读Excel发送工资条小工具
  15. Base64基本原理
  16. 1.14食油大学acm训练赛NO.6
  17. Windows“启动”文件夹
  18. 云原生到底是什么?一文了解云原生四要素!
  19. 【数学建模】灰色关联(Matlab代码实现)
  20. 计算机的英语作文模板,高中英语作文模板 第243期:My Computer 我的电脑

热门文章

  1. QT中自定义控件和插件大致方法
  2. 程序员怒批996背后的支持者,刘强东和马云哑口无言!
  3. Python装逼神器,Python实现一键批量扣图
  4. SSM框架练手项目【虎牙个人博客】手把手带你搭建自己的个人博客
  5. PMBOK6 核对单和核查表的区别
  6. linux bam文件格式介绍,Sam和bam文件说明
  7. 【微分方程数值解】常微分方程(一)欧拉方法和改进欧拉方法(附python算例,封装类)
  8. 「Cold Chain 2015国际冷链物流展」
  9. 说说Redis的常用应用场景
  10. 韩国5G产品定价及营销策略剖析