1.hdfs的特征
1.1大規(guī)模數(shù)據(jù)分布存儲能力
1.2高并發(fā)訪問能力
1.3強大的容錯能力
1.4順序式文件訪問
1.5簡單的一致性模型
1.6數(shù)據(jù)塊存儲方式
2.hdfs的基本組成結(jié)構(gòu)與訪問過程

hdfs的基本文件訪問過程:
2.1首先,用戶的應(yīng)用程序通過hdfs的客戶端程序?qū)⑽募l(fā)送至NameNode。
2.2NameNode接收到文件名之后,在hdfs的目錄中檢索文件名對應(yīng)的數(shù)據(jù)塊,再根據(jù)數(shù)據(jù)塊的信息找到保存數(shù)據(jù)塊的DataNode地址,將這些地址回送到客戶端。
2.3客戶端接收到這些DataNode地址后,與這些DataNode并行地進(jìn)行數(shù)據(jù)傳輸操作,同時將操作結(jié)果的相關(guān)日志(比如是否成功,修改后的數(shù)據(jù)塊信息等)提交到NameNode。
數(shù)據(jù)塊 命名空間 通信協(xié)議 客戶端
3.hdfs可靠性設(shè)計

安全模式、SecondaryNameNode、心跳包和副本重新創(chuàng)建、數(shù)據(jù)一致性(數(shù)據(jù)校驗)、租約、回滾
4.hdfs文件存儲組織和讀寫
4.1文件數(shù)據(jù)的存儲組織
1.NameNode的目錄結(jié)構(gòu)
2.DataNode的目錄結(jié)構(gòu)
3.CheckPointNode
5.數(shù)據(jù)的讀寫過程
1.讀取

2.寫入

6.hdfs文件操作系統(tǒng)操作命令
hdfs文件操作命令
查看文件:hadoop dfs -put Downloads/train.csv hdfs://localhost:9000/user/hadoop
hadoop dfs -cat hdfs://localhost:9000/user/hadoop/train.csv
改變文件所屬用戶組:chgrp
改變文件權(quán)限:chmod
改變用戶的所屬用戶:chown
上傳到hdfs:put
下載到本地:get
統(tǒng)計目錄下目錄數(shù)、文件數(shù)、字節(jié)數(shù):count
hadoop dfs -count hdfs://localhost:9000/user/hadoop
結(jié)果:
3 11 87933 hdfs://localhost:9000/user/hadoop
將文件拷貝到目標(biāo)路徑
hadoop dfs -cp hdfs://localhost:9000/user/hadoop/output hdfs://localhost:9000/user/hadoop/input
顯示目錄下文件及大小
hadoop dfs -du /user/hadoop/input
累加大?。篽adoop dfs -du -s /user/hadoop/input
易于閱讀:hadoop dfs -du -h /user/hadoop/input
清空回收站
hadoop dfs -expunge
將文件拷貝到本地
hadoop dfs -get /user/hadoop/input/output/_SUCCESS /home/hadoop
將文件夾合并到本地文件夾下
hadoop dfs -get /user/hadoop/input/output/ /home/hadoop/merge_test
返回文件狀態(tài)
hadoop dfs -ls
返回遞歸的文件狀態(tài)
hadoop dfs -lsr
創(chuàng)建文件夾
hadoop dfs -mkdir /user/hadoop/hdfs
上傳或下載后刪除源文件
moveFromLocal
moveToLocal
移動數(shù)據(jù)
mv(不可跨文件系統(tǒng))
put
上傳,可以從標(biāo)準(zhǔn)輸入中讀取,用-表示本地文件。
hadoop dfs -put - /user/hadoop/hdfs/a.txt
rm、rmr
刪除文件
setrep
更改副本數(shù)
stat
返回對應(yīng)路徑的狀態(tài)信息
hadoop dfs -stat /user/hadoop/hdfs
tail
在標(biāo)準(zhǔn)輸出中顯示文件末尾1KB的數(shù)據(jù)
hadoop dfs -tail /user/hadoop/input/hdfs-site.xml
test
判斷文件信息
hadoop dfs -test -e[-z或-d]
text
將文本文件或某些格式的非文本文件通過文本格式輸出
touchz
創(chuàng)建一個大小為0的文件
hadoop dfs -touch
hdfs高級功能
archive(存在問題)
hadoop archive -archiveName hdfs.har -p /user/hadoop/input /outputdir
balancer
hadoop balancer [-threshold <threshold>]
distcp2
dfsadmin
6.hdfs實例
找出目錄下所有包含hadoop文字的行,輸入到本地文本中。
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
public class resultFilter {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(conf);
FileSystem local=FileSystem.getLocal(conf);
System.out.println(args.length);
Path inputDir,localFile;
FileStatus[] inputFiles;
FSDataOutputStream out=null;
FSDataInputStream in=null;
Scanner scan;
String str;
byte[] buf;
int singleFileLines;
int numLines,numFiles,i;
if(args.length!=4){
System.err.println("usage resultFilter <dfs path><local path>"+"<match str><single file lines>");
return;
}
inputDir=new Path(args[0]);
singleFileLines=Integer.parseInt(args[3]);
try{
inputFiles=hdfs.listStatus(inputDir);
numLines=0;
numFiles=1;
localFile=new Path(args[1]);
if(local.exists(localFile))
local.delete(localFile,true);
for(i=0;i<inputFiles.length;i++){
if(inputFiles[i].isDir()==true)
continue;
System.out.println(inputFiles[i].getPath().getName());
in=hdfs.open(inputFiles[i].getPath());
scan=new Scanner(in);
while(scan.hasNext()){
str=scan.nextLine();
if(str.indexOf(args[2])==-1)
continue;
numLines++;
if(numLines==1){
localFile=new Path(args[1]+File.separator+numFiles);
out=local.create(localFile);
numFiles++;
}
buf=(str+"\n").getBytes();
out.write(buf,0,buf.length);
if(numLines==singleFileLines){
out.close();
numLines=0;
}
}
scan.close();
in.close();
}
if(out !=null)
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}