HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo...
26 集群使用初步
HDFS的設計思路
l?設計思想??
分而治之:將大文件、大批量文件,分布式存放在大量服務器上,以便于采取分而治之的方式對海量數據進行運算分析;
?
l?在大數據系統中作用:
為各類分布式運算框架(如:mapreduce,spark,tez,……)提供數據存儲服務
?
l?重點概念:文件切塊,副本存放,元數據
26.1 HDFS使用
1、查看集群狀態
命令:?? hdfs? dfsadmin?–report
可以看出,集群共有3個datanode可用
也可打開web控制臺查看HDFS集群信息,在瀏覽器打開http://hadoop:50070/
2、上傳文件到HDFS
查看HDFS中的目錄信息
命令:hadoop? fs? –ls? /
?
上傳文件
命令:hadoop fs -put ./findbugs-1.3.9 /
| [toto@hadoop software]$ hadoop fs -put ./findbugs-1.3.9 / put: `/findbugs-1.3.9/LICENSE-ASM.txt': File exists put: `/findbugs-1.3.9/LICENSE-AppleJavaExtensions.txt': File exists put: `/findbugs-1.3.9/LICENSE-bcel.txt': File exists put: `/findbugs-1.3.9/LICENSE-commons-lang.txt': File exists put: `/findbugs-1.3.9/LICENSE-docbook.txt': File exists put: `/findbugs-1.3.9/LICENSE-dom4j.txt': File exists put: `/findbugs-1.3.9/LICENSE-jFormatString.txt': File exists |
查看上傳后的信息列表(hadoop fs –ls / 或 hadoop fs -ls /findbugs-1.3.9)
?
從HDFS下載文件
命令:hadoop fs -get /findbugs-1.3.9/LICENSE-ASM.txt
| [toto@hadoop learn]$ cd /home/toto/learn /home/toto/learn [toto@hadoop learn]$ pwd /home/toto/learn [toto@hadoop learn]$ hadoop fs -get /findbugs-1.3.9/LICENSE-ASM.txt [toto@hadoop learn]$ ls LICENSE-ASM.txt |
?
yarn的管理界面是:http://hadoop:8088/cluster
?
26.2模擬運行一個mapreduce程序
模擬運行一個mapreduce程序的時候,需要先啟動hdfs,啟動命令是:
| [toto@hadoop1 hadoop-2.8.0]$cd /home/toto/software/hadoop-2.8.0 [toto@hadoop1 hadoop-2.8.0]$sbin/start-dfs.sh |
在/home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce下有一個mapreduce的運行例子:
| [toto@hadoop mapreduce]$ cd /home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce [toto@hadoop mapreduce]$ pwd /home/toto/software/hadoop-2.8.0/share/hadoop/mapreduce [toto@hadoop mapreduce]$ ll 總用量 5088 -rw-r--r--. 1 toto hadoop? 562900 3月? 17 13:31 hadoop-mapreduce-client-app-2.8.0.jar -rw-r--r--. 1 toto hadoop? 782739 3月? 17 13:31 hadoop-mapreduce-client-common-2.8.0.jar -rw-r--r--. 1 toto hadoop 1571179 3月? 17 13:31 hadoop-mapreduce-client-core-2.8.0.jar -rw-r--r--. 1 toto hadoop? 195000 3月? 17 13:31 hadoop-mapreduce-client-hs-2.8.0.jar -rw-r--r--. 1 toto hadoop?? 31533 3月? 17 13:31 hadoop-mapreduce-client-hs-plugins-2.8.0.jar -rw-r--r--. 1 toto hadoop?? 66999 3月? 17 13:31 hadoop-mapreduce-client-jobclient-2.8.0.jar -rw-r--r--. 1 toto hadoop 1587158 3月? 17 13:31 hadoop-mapreduce-client-jobclient-2.8.0-tests.jar -rw-r--r--. 1 toto hadoop?? 75495 3月? 17 13:31 hadoop-mapreduce-client-shuffle-2.8.0.jar -rw-r--r--. 1 toto hadoop? 301934 3月? 17 13:31 hadoop-mapreduce-examples-2.8.0.jar drwxr-xr-x. 2 toto hadoop??? 4096 3月? 17 13:31 jdiff drwxr-xr-x. 2 toto hadoop??? 4096 3月? 17 13:31 lib drwxr-xr-x. 2 toto hadoop??? 4096 3月? 17 13:31 lib-examples drwxr-xr-x. 2 toto hadoop??? 4096 3月? 17 13:31 sources [toto@hadoop mapreduce]$ ? 使用命令運行mapreduce命令: [toto@hadoop mapreduce]$ hadoop jar hadoop-mapreduce-examples-2.8.0.jar pi 5 5 Number of Maps? = 5 Samples per Map = 5 Wrote input for Map #0 Wrote input for Map #1 Wrote input for Map #2 Wrote input for Map #3 Wrote input for Map #4 Starting Job 17/05/29 14:47:36 INFO client.RMProxy: Connecting to ResourceManager at hadoop/192.168.106.80:8032 17/05/29 14:47:37 INFO input.FileInputFormat: Total input files to process : 5 17/05/29 14:47:37 INFO mapreduce.JobSubmitter: number of splits:5 17/05/29 14:47:38 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1495998405307_0001 17/05/29 14:47:39 INFO impl.YarnClientImpl: Submitted application application_1495998405307_0001 17/05/29 14:47:39 INFO mapreduce.Job: The url to track the job: http://hadoop:8088/proxy/application_1495998405307_0001/ 17/05/29 14:47:39 INFO mapreduce.Job: Running job: job_1495998405307_0001 17/05/29 14:48:00 INFO mapreduce.Job: Job job_1495998405307_0001 running in uber mode : false 17/05/29 14:48:00 INFO mapreduce.Job:? map 0% reduce 0% |
進入hdfs的管理界面(http://hadoop:8088/cluster/apps),查看程序運行情況:
?
26.2 MAPREDUCE使用
mapreduce是hadoop中的分布式運算編程框架,只要按照其編程規范,只需要編寫少量的業務邏輯代碼即可實現一個強大的海量數據并發處理程序
?
26.2.1 Demo開發——wordcount
1、需求
從大量(比如T級別)文本文件中,統計出每一個單詞出現的總次數
?
2、mapreduce實現思路
Map階段:
a)????????從HDFS的源數據文件中逐行讀取數據
b)????????將每一行數據切分出單詞
c)????????為每一個單詞構造一個鍵值對(單詞,1)
d)????????將鍵值對發送給reduce
?
Reduce階段:
a)????????接收map階段輸出的單詞鍵值對
b)????????將相同單詞的鍵值對匯聚成一組
c)????????對每一組,遍歷組中的所有“值”,累加求和,即得到每一個單詞的總次數
d)????????將(單詞,總次數)輸出到HDFS的文件中
?
?
1、? 具體編碼實現
(1)定義一個mapper類
| //首先要定義四個泛型的類型 //keyin:? LongWritable??? valuein: Text //keyout: Text??????????? valueout:IntWritable ? public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ ???????? //map方法的生命周期:? 框架每傳一行數據就被調用一次 ???????? //key :? 這一行的起始點在文件中的偏移量 ???????? //value: 這一行的內容 ???????? @Override ???????? protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { ?????????????????? //拿到一行數據轉換為string ?????????????????? String line = value.toString(); ?????????????????? //將這一行切分出各個單詞 ?????????????????? String[] words = line.split(" "); ?????????????????? //遍歷數組,輸出<單詞,1> ?????????????????? for(String word:words){ ?????????????????? ???????? context.write(new Text(word), new IntWritable(1)); ?????????????????? } ???????? } } |
?
(2)定義一個reducer類
| ???????? //生命周期:框架每傳遞進來一個kv 組,reduce方法被調用一次 ???????? @Override ???????? protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { ?????????????????? //定義一個計數器 ?????????????????? int count = 0; ?????????????????? //遍歷這一組kv的所有v,累加到count中 ?????????????????? for(IntWritable value:values){ ??????????????????????????? count += value.get(); ?????????????????? } ?????????????????? context.write(key, new IntWritable(count)); ???????? } } |
?
(3)定義一個主類,用來描述job并提交job
| public class WordCountRunner { ???????? //把業務邏輯相關的信息(哪個是mapper,哪個是reducer,要處理的數據在哪里,輸出的結果放哪里。。。。。。)描述成一個job對象 ???????? //把這個描述好的job提交給集群去運行 ???????? public static void main(String[] args) throws Exception { ?????????????????? Configuration conf = new Configuration(); ?????????????????? Job wcjob = Job.getInstance(conf); ?????????????????? //指定我這個job所在的jar包 //?????????????? wcjob.setJar("/home/hadoop/wordcount.jar"); ?????????????????? wcjob.setJarByClass(WordCountRunner.class); ?????????????????? ?????????????????? wcjob.setMapperClass(WordCountMapper.class); ?????????????????? wcjob.setReducerClass(WordCountReducer.class); ?????????????????? //設置我們的業務邏輯Mapper類的輸出key和value的數據類型 ?????????????????? wcjob.setMapOutputKeyClass(Text.class); ?????????????????? wcjob.setMapOutputValueClass(IntWritable.class); ?????????????????? //設置我們的業務邏輯Reducer類的輸出key和value的數據類型 ?????????????????? wcjob.setOutputKeyClass(Text.class); ?????????????????? wcjob.setOutputValueClass(IntWritable.class); ?????????????????? ?????????????????? //指定要處理的數據所在的位置 ?????????????????? FileInputFormat.setInputPaths(wcjob, "hdfs://hdp-server01:9000/wordcount/data/big.txt"); ?????????????????? //指定處理完成之后的結果所保存的位置 ?????????????????? FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://hdp-server01:9000/wordcount/output/")); ?????????????????? ?????????????????? //向yarn集群提交這個job ?????????????????? boolean res = wcjob.waitForCompletion(true); ?????????????????? System.exit(res?0:1); ???????? } |
?
26.2.2 程序打包運行
1.????????將程序打包
2.????????準備輸入數據
vi? /home/hadoop/test.txt
| Hello tom Hello jim Hello ketty Hello world Ketty tom |
在hdfs上創建輸入數據文件夾:
hadoop?? fs?mkdir? -p? /wordcount/input
將words.txt上傳到hdfs上
???????? hadoop? fs? –put? /home/hadoop/words.txt? /wordcount/input
?
3.????????將程序jar包上傳到集群的任意一臺服務器上
?
4.????????使用命令啟動執行wordcount程序jar包
$ hadoop jar wordcount.jar cn.toto.bigdata.mrsimple.WordCountDriver/wordcount/input /wordcount/out
5.????????查看執行結果
$ hadoop fs –cat /wordcount/out/part-r-00000
?
總結
以上是生活随笔為你收集整理的HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 指针数组,数组指针,函数指针,main函
- 下一篇: Redis 小白指南(二)- 聊聊五大类