何为HDFS?
该文来自百度百科,自我收藏。
Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统。它和现有的分布式文件系统有很多共同点。但同时,它和其他的分布式文件系统的区别也是很明显的。HDFS是一个高度容错性的系统,适合部署在廉价的机器上。HDFS能提供高吞吐量的数据访问,非常适合大规模数据集上的应用。HDFS放宽了一部分POSIX约束,来实现流式读取文件系统数据的目的。HDFS在最开始是作为Apache Nutch搜索引擎项目的基础架构而开发的。HDFS是Apache Hadoop Core项目的一部分。
HDFS有着高容错性(fault-tolerant)的特点,并且设计用来部署在低廉的(low-cost)硬件上。而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求(requirements)这样可以实现流的形式访问(streaming access)文件系统中的数据。
- 特点和目标

文件命名空间
数据复制

安全模式
通信协议
异常处理
可靠性
重新复制
数据正确性
元数据失效
特点
快照
数据组织
阶段状态
流水式复制
可访问性
DFSShell
Action
|
Command
|
创建目录 /foodir
|
hadoop dfs -mkdir /foodir
|
查看文件 /foodir/myfile.txt
|
hadoop dfs -cat /foodir/myfile.txt
|
删除文件/foodir/myfile.txt
|
hadoop dfs -rm /foodir myfile.txt
|
Action
|
Command
|
将集群设置成安全模式
|
bin/hadoop dfsadmin -safemode enter
|
产生一个数据节点的列表
|
bin/hadoop dfsadmin -report
|
去掉一个数据节点
|
bin/hadoop dfsadmin -decommission datanodename
|
浏览器接口
存储空间回收
HDFS的文件读取解析
1
2
3
4
5
|
public static void read(Path path) throws IOException{ FileSystem hdfs = HdfsUtils.getFilesystem(); //步骤 1 FSDataInputStream fsDataInputStream = hdfs.open(path); //步骤 2 IOUtils.copyBytes(fsDataInputStream, System.out, 4096 , false ); //步骤 3 } |
获取文件系统对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.yq.common; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; public class HdfsUtils { public static FileSystem getFilesystem(){ FileSystem hdfs= null ; Configuration conf= new Configuration(); try { hdfs = FileSystem.get(uri,conf); } catch (Exception ex){ // } return hdfs; } } |
打开文件
1
|
FSDataInputStream fsDataInputStream = hdfs.open(path); |
1
2
3
|
public FSDataInputStream open(Path f) throws IOException { return open(f, getConf().getInt( "io.file.buffer.size" , 4096 )); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException; //这个方法在DistributedFileSystem类有实现,如下 @Override public FSDataInputStream open(Path f, final int bufferSize) throws IOException { statistics.incrementReadOps( 1 ); Path absF = fixRelativePart(f); return new FileSystemLinkResolver<FSDataInputStream>() { @Override public FSDataInputStream doCall( final Path p) throws IOException, UnresolvedLinkException { return new HdfsDataInputStream( dfs.open(getPathName(p), bufferSize, verifyChecksum)); } @Override public FSDataInputStream next( final FileSystem fs, final Path p) throws IOException { return fs.open(p, bufferSize); } }.resolve( this , absF); } |
1
2
3
4
5
6
|
public DFSInputStream open(String src, int buffersize, boolean verifyChecksum) throws IOException, UnresolvedLinkException { checkOpen(); // Get block info from namenode return new DFSInputStream( this , src, buffersize, verifyChecksum); } |
1
2
3
4
5
6
7
8
9
10
|
DFSInputStream(DFSClient dfsClient, String src, int buffersize, boolean verifyChecksum ) throws IOException, UnresolvedLinkException { this .dfsClient = dfsClient; this .verifyChecksum = verifyChecksum; this .buffersize = buffersize; this .src = src; this .cachingStrategy = dfsClient.getDefaultReadCachingStrategy(); openInfo(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * Grab the open-file info from namenode */ synchronized void openInfo() throws IOException, UnresolvedLinkException { lastBlockBeingWrittenLength = fetchLocatedBlocksAndGetLastBlockLength(); int retriesForLastBlockLength = dfsClient.getConf().retryTimesForGetLastBlockLength; while (retriesForLastBlockLength > 0 ) { // Getting last block length as -1 is a special case. When cluster // restarts, DNs may not report immediately. At this time partial block // locations will not be available with NN for getting the length. Lets // retry for 3 times to get the length. if (lastBlockBeingWrittenLength == - 1 ) { DFSClient.LOG.warn( "Last block locations not available. " + "Datanodes might not have reported blocks completely." + " Will retry for " + retriesForLastBlockLength + " times" ); waitFor(dfsClient.getConf().retryIntervalForGetLastBlockLength); lastBlockBeingWrittenLength = fetchLocatedBlocksAndGetLastBlockLength(); } else { break ; } retriesForLastBlockLength--; } if (retriesForLastBlockLength == 0 ) { throw new IOException( "Could not obtain the last block locations." ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
private long fetchLocatedBlocksAndGetLastBlockLength() throws IOException { final LocatedBlocks newInfo = dfsClient.getLocatedBlocks(src, 0 ); if (DFSClient.LOG.isDebugEnabled()) { DFSClient.LOG.debug( "newInfo = " + newInfo); } if (newInfo == null ) { throw new IOException( "Cannot open filename " + src); } if (locatedBlocks != null ) { Iterator<LocatedBlock> oldIter = locatedBlocks.getLocatedBlocks().iterator(); Iterator<LocatedBlock> newIter = newInfo.getLocatedBlocks().iterator(); while (oldIter.hasNext() && newIter.hasNext()) { if (! oldIter.next().getBlock().equals(newIter.next().getBlock())) { throw new IOException( "Blocklist for " + src + " has changed!" ); } } } locatedBlocks = newInfo; long lastBlockBeingWrittenLength = 0 ; if (!locatedBlocks.isLastBlockComplete()) { final LocatedBlock last = locatedBlocks.getLastLocatedBlock(); if (last != null ) { if (last.getLocations().length == 0 ) { if (last.getBlockSize() == 0 ) { // if the length is zero, then no data has been written to // datanode. So no need to wait for the locations. return 0 ; } return - 1 ; } final long len = readBlockLength(last); last.getBlock().setNumBytes(len); lastBlockBeingWrittenLength = len; } } currentNode = null ; return lastBlockBeingWrittenLength; } |
1
|
private final List<LocatedBlock> blocks; // array of blocks with prioritized locations |
将文件内容在标准输出显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if (close) { out.close(); out = null ; in.close(); in = null ; } } finally { if (close) { closeStream(out); closeStream(in); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void copyBytes(InputStream in, OutputStream out, int buffSize) throws IOException { PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null ; byte buf[] = new byte [buffSize]; int bytesRead = in.read(buf); while (bytesRead >= 0 ) { out.write(buf, 0 , bytesRead); if ((ps != null ) && ps.checkError()) { throw new IOException( "Unable to write to output stream." ); } bytesRead = in.read(buf); } } |
何为HDFS?的更多相关文章
- HDFS简单入门
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-simple.html,转载请注明源地址. 欢迎关注我的个人博客:www.wuyudong.com, 更 ...
- HDFS入门
HDFS入门 欢迎关注我的个人博客:http://www.cnblogs.com/yjd_hycf_space 更多大数据以及编程相关的精彩文章 为什么我们需要HDFS 文件系统由三部分组成:与文件管 ...
- HDFS源码分析心跳汇报之整体结构
我们知道,HDFS全称是Hadoop Distribute FileSystem,即Hadoop分布式文件系统.既然它是一个分布式文件系统,那么肯定存在很多物理节点,而这其中,就会有主从节点之分.在H ...
- hadoop 2.7.3本地环境运行官方wordcount-基于HDFS
接上篇<hadoop 2.7.3本地环境运行官方wordcount>.继续在本地模式下测试,本次使用hdfs. 2 本地模式使用fs计数wodcount 上面是直接使用的是linux的文件 ...
- Hadoop学习之旅二:HDFS
本文基于Hadoop1.X 概述 分布式文件系统主要用来解决如下几个问题: 读写大文件 加速运算 对于某些体积巨大的文件,比如其大小超过了计算机文件系统所能存放的最大限制或者是其大小甚至超过了计算机整 ...
- python基础操作以及hdfs操作
目录 前言 基础操作 hdfs操作 总结 一.前言 作为一个全栈工程师,必须要熟练掌握各种语言...HelloWorld.最近就被"逼着"走向了python开发之路, ...
- C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。
一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...
- HDFS的架构
主从结构 主节点,只有一个: namenode 从节点,有很多个: datanodes 在版本1中,主节点只有一个,在 版本2中主节点有两个. namenode 负责(管理): 接收用户操作请求 维护 ...
- hdfs以及hbase动态增加和删除节点
一个知乎上的问题:Hbase的Region server和hadoop的datanode是否可以部署在一台服务器上?如果是的话,二者是否是一对一的关系?部署在同一台服务器上,可以减少数据跨网络传输的流 ...
随机推荐
- ajax-登陆+验证码
登陆用户名和密码判断+验证码验证 省略dao层和service层 1.生成验证码的number.jsp <%@ page contentType="image/jpeg" l ...
- python笔记1-转义字符
print(r'dd"e"f')print(r'dd'e'f')print(r"dd"e"f")print(r"dd'e'f&qu ...
- cookie---session
//以下文字摘自慕课网教程..... 设置cookie PHP设置Cookie最常用的方法就是使用setcookie函数,setcookie具有7个可选参数,我们常用到的为前5个: name( Coo ...
- 用java解析字符串,如字符串"(1+2/5)*3"当成是数值表达式,进行计算出结果来
import java.io.*;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;public cla ...
- [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF
遇到这两个错误,是因为Git的换行符检查功能. core.safecrlf Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符.这个功能的选项 ...
- QR 二维码总结
@(Java ThirdParty)[QR] QR 二维码总结 根据Wiki中的介绍,QR Code(Quick Response Code),二维条形码,由日志设计应用于汽车制造工业中.条形码中包含 ...
- MyBatis源码分析(4)—— Cache构建以及应用
@(MyBatis)[Cache] MyBatis源码分析--Cache构建以及应用 SqlSession使用缓存流程 如果开启了二级缓存,而Executor会使用CachingExecutor来装饰 ...
- select、poll、epoll之间的区别总结[整理]
select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...
- Django模型的Field Types总结
转:http://blog.csdn.net/devil_2009/article/details/41735611 Field Types 常用参数: null 如果设置为 True , Djang ...
- 11个审查Linux是否被入侵的方法
11个审查Linux是否被入侵的方法 一.检查系统日志 lastb命令 检查系统错误登陆日志,统计IP重试次数 二.检查系统用户 1.cat /etc/passwd 查看是否有异常的系统用户 2.gr ...