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)檔案系統中的數據。
基本介紹
- 全稱:Hadoop Distributed File System
- 簡稱:hdfs
- 實質:分散式檔案系統
- 作用:作為Apache Nutch的基礎架構
- 特點:高容錯性
- 適用:大規模數據集
特點和目標

檔案命名空間
數據複製

安全模式
通信協定
異常處理
可靠性
重新複製
數據正確性
元數據失效
特點
快照
數據組織
階段狀態
流水式複製
可訪問性
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的檔案讀取解析
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}獲取檔案系統對象
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{ URI uri = new URI("hdfs://localhost:9000"); hdfs = FileSystem.get(uri,conf); } catch(Exception ex){ // } return hdfs; }}打開檔案
FSDataInputStream fsDataInputStream = hdfs.open(path);
public FSDataInputStream open(Path f) throws IOException { return open(f, getConf().getInt("io.file.buffer.size", 4096)); } 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); } 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); } 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(); } /** * 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."); } } 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; }private final List<LocatedBlock> blocks; // array of blocks with prioritized locations
將檔案內容在標準輸出顯示
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); } } }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); } }
