Hadoop的分布式文件系统
??? ?HDFS默認情況下,塊的大小是64MB。與磁盤相比它的塊是巨大的,數(shù)據(jù)轉換的時間就比需找數(shù)據(jù)的開始塊的時間大的多。因此這轉換一個大文件的操作取決于磁盤的速度。
????? Namenodes 和Datanodes
???? Namenodes管理文件系統(tǒng)的命名空間。它維護文件系統(tǒng)的結構和屬性信息對于所有的文件和目錄在樹形結構。這信息被持續(xù)的存儲在本地的硬盤以兩種文件的格式:命名空間的圖像和編輯日志。
?? Datanodes是文件系統(tǒng)的工作空間。他們存儲和獲得塊當他們被告知和他們報告回這namenodes帶有的他們存儲的塊的列表。
?? 命令接口:% hadoop fs -copyFromLocal input/docs/quangle.txt hdfs://localhost/user/tom/
quangle.txt
???????? % hadoop fs -copyToLocal quangle.txt quangle.copy.txt
?????? % md5 input/docs/quangle.txt quangle.copy.txt
?????? MD5 (input/docs/quangle.txt) = a16f231da6b05e2ba7a339320e7dacd9
?
?? 讀取數(shù)據(jù)從一個Hadoop URL中:
?? InputStream in=null;
?? try{
?????? in=new URL("hdfs://host/path").openStream();?
??? }finally{IOUtils.closeStream(in);}
?
?
?
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);
}
}
}
獲取FileSystem
????public static FileSystem get(Configuration conf) throws IOException
??? public static FileSystem get(URI uri, Configuration conf) throws IOException
??? public static FileSystem get(URI uri, Configuration conf, String user) throws IOException
獲取輸入的流:
??? public FSDataInputStream open(Path f) throws IOException
?? public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException
?????其中可以獲得文件指針的位置
?? public interface Seekable {
void seek(long pos) throws IOException;
long getPos() throws IOException;
}
? 寫數(shù)據(jù):
?? public FSDataOutputStream create(Path f) throws IOException獲得寫入的流
? 其中有回調(diào)接口
?? package org.apache.hadoop.util;
?? public interface Progressable {
??????????? public void progress();
?? }
??? 添加內(nèi)容到文件的末尾
?? ?public FSDataOutputStream append(Path f) throws IOException
?? 目錄的操作:
??? public boolean mkdirs(Path f) throws IOException新建目錄
?? 查詢文件系統(tǒng):
?? File的屬性數(shù)據(jù):FileStatus中保存
?? 這FileStatus類封裝文件的屬性數(shù)據(jù)為文件和目錄,包括文件的長度,塊的大小,重復度,修改時間,擁有者和權限信息。
?? 這getFileStatus()在文件系統(tǒng)提供一種獲得一個FileStatus對象對于一個單獨的文件或目錄。
?? public class ShowFileStatusTest {
????????? private MiniDFSCluster cluster; // use an in-process HDFS cluster for testing
????????? private FileSystem fs;
???????? @Before
????????? public void setUp() throws IOException {
??????????????? Configuration conf = new Configuration();
??????????????? if (System.getProperty("test.build.data") == null) {
??????????????? System.setProperty("test.build.data", "/tmp");
??????????? }
????????? cluster = new MiniDFSCluster(conf, 1, true, null);
????????? fs = cluster.getFileSystem();
????????? OutputStream out = fs.create(new Path("/dir/file"));
????????? out.write("content".getBytes("UTF-8"));
????????? out.close();
? }
}
cluster = new MiniDFSCluster(conf, 1, true, null);
fs = cluster.getFileSystem();
OutputStream out = fs.create(new Path("/dir/file"));
out.write("content".getBytes("UTF-8"));
out.close();
}
@After
public 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"));
}
@Test
public 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.isDir(), is(false));
assertThat(stat.getLen(), is(7L));
assertThat(stat.getModificationTime(),
is(lessThanOrEqualTo(System.currentTimeMillis())));
assertThat(stat.getReplication(), is((short) 1));
assertThat(stat.getBlockSize(), is(64 * 1024 * 1024L));
assertThat(stat.getOwner(), is("tom"));
assertThat(stat.getGroup(), is("supergroup"));
assertThat(stat.getPermission().toString(), is("rw-r--r--"));
}
@Test
public void fileStatusForDirectory() throws IOException {
Path dir = new Path("/dir");
FileStatus stat = fs.getFileStatus(dir);
assertThat(stat.getPath().toUri().getPath(), is("/dir"));
assertThat(stat.isDir(), 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("tom"));
assertThat(stat.getGroup(), is("supergroup"));
assertThat(stat.getPermission().toString(), is("rwxr-xr-x"));
}
}
列出文件:
??? 發(fā)現(xiàn)一個單獨文件或目錄的信息是有用的,但是你也需要能夠列出一個目錄中內(nèi)容。這就是FileSystem的listStatus()方法:
????? public FileStatus[] listStatus(Path f) throws IOException
????? public FileStatus[] listStatus(Path f,PathFilter filter) throws IOException
????? public FileStatus[] listStatus(Path[] files) throws IOException
????? public FileStatus[] listStatus(Path[] files,PathFilter filter) throw IOException
?其中,PathFilter可以限制匹配的文件和目錄。
? 文件模式:
??? 它是共同需要的在一個單獨操作處理一套文件。
???? public FileStatus[] globStatus(Path pathPattern) throws IOEXCEPTion
???? public FileStatus[] globStatus(Path pathPattern,PathFilter filter) throws IOException
????? PathFilter:
?? Glob patterns并不是總是足夠強大的來描述你想要獲得的一套文件。例如,它不能夠排除一個特定文件使用一個glob格式。
package org.apache.hadoop.fs;
public interface PathFilter {
boolean accept(Path path);
}
刪除數(shù)據(jù):
?? 使用delete()方法在FileSystem來永久的移除文件或者目錄:
?? public boolean delete(Path f,boolean recursive)
?? 如果recursive是true則一個非空的目錄被刪除和它的內(nèi)容也被刪除。
? 數(shù)據(jù)流動:
??? HDFS打開 Distributed FileSystem -》get block locations? from NameNode。
????HDFS client讀取FSData InputStream,讀取數(shù)據(jù)從DataNode。
數(shù)據(jù)文件寫的分析:
??? HDFS client 創(chuàng)建文件在Distributed FileSystem向NameNode請求創(chuàng)建新文件,完成后也要向NameNode發(fā)送消息。
???? HDFS client寫數(shù)據(jù)通過FSData OutputStream 向datanodes寫入數(shù)據(jù)。完成后關閉。
Hadoop的重復塊的放置:
??? Hadoop的默認策略是放置第一個從發(fā)的在相同的節(jié)點和client。第二個重復的塊被放在隨機選擇的一個節(jié)點和第一個不同的曺內(nèi)。第三個放置在和第二個相同的曺但是不同的節(jié)點上。
保證一致性:
Path p = new Path("p");
FSDataOutputStream out = fs.create(p);
out.write("content".getBytes("UTF-8"));
out.flush();
out.sync();
assertThat(fs.getFileStatus(p).getLen(), is(((long) "content".length())));
? 并行拷貝:
?? % hadoop distcp hdfs://namenode1/foo hdfs://namenode2/bar
使用的存檔:Hado% hadoop fs -lsr /my/filesop
?
?
?
?
?
?
?
?
????????????
?
?
總結
以上是生活随笔為你收集整理的Hadoop的分布式文件系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这Hadoop分布式文件系统
- 下一篇: VC中文字的输出