生活随笔
收集整理的這篇文章主要介紹了
Hadoop大数据--Mapreduce编程规范及入门示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mapreduce是一個分布式的運算編程框架,核心功能是將用戶編寫的核心邏輯代碼分布式地運行在一個集群的很多服務器上.
Mapreduce的存在價值
(1)海量數據在單機上處理因為硬件資源限制,無法勝任,因為需要采用分布式集群的方式來處理。
(2)而一旦將單機版程序擴展到集群來分布式運行,將極大地增加程序的復雜度和開發難度
(3)引入mapreduce框架后,開發人員可以將絕大部分工作集中在業務邏輯的開發上,而將分布式計算中的復雜性交由框架來處理
hadoop與mapreduce的關系
Hadoop的發布包中內置了一個hadoop-mapreduce-example-2.4.1.jar,這個jar包中有各種MR示例程序,可以通過以下步驟運行:
啟動hdfs,yarn
然后在集群中的任意一臺服務器上執行,(比如運行wordcount):
hadoop jar hadoop-mapreduce-example-2.4.1.jar wordcount /wordcount/data /wordcount/out
mapreduce編程規范
(1)用戶程序會分成三個部分:Mapper,Reducer,Driver
(2)Mapper的輸入數據是KV對的形式,KV的類型可以設置
(3)Mapper的輸出數據是KV對的形式,KV的類型可以設置
(4)Mapper中的業務邏輯寫在map方法中
(5)map方法是每進來一個KV對調用一次
(6)Reducer的輸入數據應該對應Mapper的輸出數據,也是KV
(7)Reducer的業務邏輯寫在reduce方法中
(8)reduce方法是對每一個<key,valueList>調用一次
(9)用戶的Mapper和Reducer都要繼承各自的父類
(10)整個程序需要一個Drvier來進行提交,提交的是一個描述了各種必要信息的job對象.
wordcount示例編寫
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{@Overrideprotected void map(LongWritable key
, Text value
, Context context
) throws IOException
, InterruptedException
{String line
= value
.toString();String
[] words
= line
.split(" ");for(String word
:words
){context
.write(new Text(word
), new IntWritable(1));}}
}
@Overrideprotected void reduce(Text key
, Iterable
<IntWritable> values
, Context context
) throws IOException
, InterruptedException
{int count
= 0;for(IntWritable value
:values
){count
+= value
.get();}context
.write(key
, new IntWritable(count
));}
}
public class WordCountRunner {public static void main(String
[] args
) throws Exception
{Configuration conf
= new Configuration();Job wcjob
= Job
.getInstance(conf
);
wcjob
.setJarByClass(WordCountRunner
.class);wcjob
.setMapperClass(WordCountMapper
.class);wcjob
.setReducerClass(WordCountReducer
.class);wcjob
.setMapOutputKeyClass(Text
.class);wcjob
.setMapOutputValueClass(IntWritable
.class);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/"));boolean res
= wcjob
.waitForCompletion(true);System
.exit(res
?0:1);}
總結
以上是生活随笔為你收集整理的Hadoop大数据--Mapreduce编程规范及入门示例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。