1. hdfs定义

HDFS is the primary distributed storage used by Hadoop applications. A HDFS cluster primarily consists of a NameNode that manages the file system metadata and DataNodes that store the actual data.

2. hdfs架构

3. hdfs实例

作为文件系统,文件的读写才是核心:

/*** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import java.io.File;
import java.io.IOException;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;public class HadoopDFSFileReadWrite {static void usage () {System.out.println("Usage : HadoopDFSFileReadWrite <inputfile> <output file>");System.exit(1);}static void printAndExit(String str) {System.err.println(str);System.exit(1);}public static void main (String[] argv) throws IOException {Configuration conf = new Configuration();FileSystem fs = FileSystem.get(conf);if (argv.length != 2)usage();// Hadoop DFS deals with PathPath inFile = new Path(argv[0]);Path outFile = new Path(argv[1]);// Check if input/output are validif (!fs.exists(inFile))printAndExit("Input file not found");if (!fs.isFile(inFile))printAndExit("Input should be a file");if (fs.exists(outFile))printAndExit("Output already exists");// Read from and write to new fileFSDataInputStream in = fs.open(inFile);FSDataOutputStream out = fs.create(outFile);byte buffer[] = new byte[256];try {int bytesRead = 0;while ((bytesRead = in.read(buffer)) > 0) {out.write(buffer, 0, bytesRead);}} catch (IOException e) {System.out.println("Error while copying file");} finally {in.close();out.close();}}
}

上述示例,将一个文件的内容复制到另一个文件中,具体步骤如下:

第一步:创建一个文件系统实例,给该实例传递新的配置。

Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);

第二步:获取文件路径

// Hadoop DFS deals with PathPath inFile = new Path(argv[0]);Path outFile = new Path(argv[1]);// Check if input/output are validif (!fs.exists(inFile))printAndExit("Input file not found");if (!fs.isFile(inFile))printAndExit("Input should be a file");if (fs.exists(outFile))printAndExit("Output already exists");

第三步:打开文件输入输出流,将输入流写到输出流中:

    // Read from and write to new fileFSDataInputStream in = fs.open(inFile);FSDataOutputStream out = fs.create(outFile);byte buffer[] = new byte[256];try {int bytesRead = 0;while ((bytesRead = in.read(buffer)) > 0) {out.write(buffer, 0, bytesRead);}} catch (IOException e) {System.out.println("Error while copying file");} finally {in.close();out.close();}

上面文件读写功能涉及到了文件系统FileSystem、配置文件Configuration、输入流/输出流FSDataInputStream/FSDataOutputStream

4. 基本概念分析

4.1 文件系统

  文件系统的层次结构如下所示:

  文件系统有两个重要的分支,一个是分布式文件系统,另一个是“本地”(映射到本地连接的磁盘)文件系统,本地磁盘适用于比较少的hadoop实例和测试。绝大部分情况下使用分布式文件系统,hadoop 分布式文件系统使用多个机器的系统,但对用户来说只有一个磁盘。它的容错性和大容量性使它非常有用。

  4.2 配置文件

  配置文件的层次结构如下:

我们关注的是HdfsConfiguration,其涉及到的配置文件有hdfs-default.xml和hdfs-site.xml:

  static {addDeprecatedKeys();// adds the default resourcesConfiguration.addDefaultResource("hdfs-default.xml");Configuration.addDefaultResource("hdfs-site.xml");}

  4.3 输入/输出流

    输入/输出流和文件系统相对应,先看一下输入流:

  

其中,HdfsDataInputStream是FSDataInputStream的实现,其构造函数为:

  public HdfsDataInputStream(DFSInputStream in) throws IOException {super(in);}

DFSInputStream层次结构如下图所示:

  在了解一下输出流:

  

其中,重点是HdfsDataOutputStream,其构造函数为:

  public HdfsDataOutputStream(DFSOutputStream out, FileSystem.Statistics stats,long startPosition) throws IOException {super(out, stats, startPosition);}

DFSOutputStream 的层次结构为:

参考文献:

【1】http://hadoop.apache.org/docs/r2.6.0/hadoop-project-dist/hadoop-hdfs/HdfsUserGuide.html

【2】http://hadoop.apache.org/docs/r2.6.0/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html

【3】http://wiki.apache.org/hadoop/HadoopDfsReadWriteExample

【4】http://blog.csdn.net/gaoxingnengjisuan/article/details/11177049

转载于:https://www.cnblogs.com/davidwang456/p/4772728.html

hdfs源码分析第一弹相关推荐

  1. Springboot源码分析第一弹 - 自动装配实现

    Springboot就不用多了吧,解放Java开发双手的神器. 最显著的特点就是,去配置化,自动装配,自动配置.让开发人员只需要注重业务的开发 今天就来了解一下自动装配的源码是怎么实现的 预先准备 直 ...

  2. hdfs源码分析第二弹

    以写文件为例,串联整个流程的源码: FSDataOutputStream out = fs.create(outFile); 1. DistributedFileSystem 继承并实现了FileSy ...

  3. HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程

    在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...

  4. HDFS源码分析心跳汇报之数据结构初始化

    在<HDFS源码分析心跳汇报之整体结构>一文中,我们详细了解了HDFS中关于心跳的整体结构,知道了BlockPoolManager.BPOfferService和BPServiceActo ...

  5. HDFS源码分析DataXceiver之整体流程

    在<HDFS源码分析之DataXceiverServer>一文中,我们了解到在DataNode中,有一个后台工作的线程DataXceiverServer.它被用于接收来自客户端或其他数据节 ...

  6. Mybatis源码分析第一天------Mybatis实用篇

    Mybatis源码分析第一天------Mybatis实用篇 一切最基本的操作就是参考官方文档:https://mybatis.org/mybatis-3/zh/configuration.html ...

  7. spring源码分析第一天------源码分析知识储备

    spring源码分析第一天------源码分析知识储备 Spring源码分析怎么学? 1.环境准备: 2.思路    看:是什么? 能干啥    想:为什么?     实践:怎么做?         ...

  8. jQuery 源码分析第一篇之入口源码

    目前阅读的是jQuery 1.11.3的源码,有参考nuysoft的资料.原来比较喜欢在自己的Evernote上做学习基类,并没有在网上写技术博客的习惯,现在开始学习JS的开源代码,想跟大家多交流,希 ...

  9. QCad源码分析 第一章

    鉴于介绍Qcad相关的文章很少,决定写此博客,一来便于日后查找,二来要有分享精神.本文章基于Qcad3 .21.3.4的开源版本进行分析,分过程中难免有疏漏,如果有新的发现会及时更改,不足之处望高手指 ...

最新文章

  1. 一次性搞懂Spring Boot 注解原理与自动装配原理,图文并茂,万字长文!
  2. 企业网络推广专员浅析大型网站企业网络推广优化需要注意哪些内容?
  3. 汇编语言--段寄存器
  4. mybaits十五:使用trim自定义字符串的截取规则
  5. 期末考试前的预习,科目:化工设备与反应器(5)
  6. IE-OLD IE 提示
  7. go struct 静态函数_Go语言学习笔记(四)结构体struct 接口Interface 反射reflect...
  8. Python中的Monkey Patch(猴子补丁)
  9. 索爱确认2月13日发布Xperia Play
  10. 对 String 字符串的理解
  11. pythonturtle库填充_python turtle库笔记
  12. xsmax进入dfu模式_DFU模式是什么?苹果XR/XS Max的DFU模式进入与退出方法[多图]
  13. C++求解组合数的具体实现
  14. 检测局域网内在线IP
  15. Windows11 Store应用商店下载的软件,怎么创建快捷方式
  16. 网易考拉海购软件测试岗怎么样,【网易考拉海购运营专员面试】面官态度倨傲,社区理念不明,995工作,很不推荐-看准网...
  17. 虚拟信用卡免费申请(工行E卡充值entropay虚拟信用卡)
  18. NSX-V edge HA部署
  19. 【基础知识】BSS段,数据段,代码段,堆栈段
  20. libvlc media player in C# (part 2)

热门文章

  1. gridcontrol 验证错误_值得品读的人生感悟句子,生气,是拿别人的错误惩罚自己...
  2. 三星note4安装linux,Leanote Ubuntu 源码安装
  3. linux db2表死锁,记录一次问题解决:DB2死锁解决办法(SQLCODE=-911, SQLSTATE=40001)
  4. php mysql log文件怎么打开_如何查看mysql的日志文件
  5. java web压缩过滤器_Java Web过滤器Filter(五)
  6. 如何修改influxdb表结构_使用nginx-lua修改influxdb API的返回结构
  7. java 实体类包含list 怎么取值_舅舅是面试官,偷偷告诉你们面试官最爱问的Java面试题...
  8. 多媒体个人计算机必须硬件设备包括,计算机基础在线测试.doc
  9. php pdo bind,PHPPDOStatement对象bindpram()、bindvalue()和bindcolumn之间的区别_php技巧
  10. java db 10.4.13_Spring Data MongoDB 1.10.13和Mongo 3.4