歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hadoop HelloWord - 排序

Hadoop HelloWord - 排序

日期:2017/3/1 9:53:36   编辑:Linux編程

抓緊時間Hadoop入門。不得不說Hadoop the Definitive Guide是本好書(下載見下面),但是卻不是一本好的入門書,一上來講了一堆各種Hadoop架構,對與一個菜鳥來說讀起來總感覺有點心虛,一行Hadoop代碼沒寫過,一直看各種Hadoop的架構,讓人感覺非常的不踏實。找來找去也只是看到一個WordCount的demo,還好實驗室Xia兄推薦了個文檔,是蝦皮工作室寫的,名字叫做“細細評味Hadoop”系列的第9章,好幾個由簡單到復雜的demo,推薦,並在此對作者表示感謝。

相關閱讀:

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Hadoop: The Definitive Guide【PDF版】 http://www.linuxidc.com/Linux/2012-01/51182.htm

吐槽下:Hadoop的官方文檔應該學學directx sdk的官方文檔,各種由簡單到復雜的demo,後期demo都是不少經典論文的實現,效果也非常cool,加上足夠的說明,一個個下來讓人感覺非常的踏實和日益精進。相比之下Hadoop的官方文檔也太簡陋了一點了。

這個demo是對數據做簡單的排序。學了wordcount後有點入門後,大家都知道經過map函數後,到達reduce之前會有個shuffle和sort的過程,這個過程主要對map函數output的key進行排序。我們就利用這個過程來對我們自己的數據排序。這樣子思路就很簡單了,在map階段,我們將一個個值作為key輸出,value隨便寫,reduce階段將這些map階段輸入的key直接寫出來就可以了。當然為了增加趣味性,可以增加一個變量count統計這個key值排在第幾位。。

輸入數據可以是:

//data1.txt:

123
12
87
150
22
23423
9874
9876

//data2.txt

29347
9877
27985
98776
989
767
2345
1532
8702
8702

詳細代碼如下:

import java.util.*;
import java.awt.datatransfer.StringSelection;
import java.io.*;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class DataSort {

public static class SortMapper extends Mapper<Object,Text,IntWritable,IntWritable>{

IntWritable one = new IntWritable(1);
@Override
public void map(Object key, Text value, Context context)throws IOException, InterruptedException
{
context.write( new IntWritable(Integer.parseInt(value.toString())), one);
}
}

public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{

private static IntWritable count = new IntWritable(0);

@Override
public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
{
for(IntWritable val : values)
{
context.write(key, count);
count.set(count.get() + 1);
}
}
}


public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();

Job job =new Job(conf,"DataSort");
job.setJarByClass(DataSort.class);

job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReducer.class);

job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

最終結果輸出:

12 0
22 1
87 2
123 3
150 4
767 5
989 6
1532 7
2345 8
8702 9
8702 10
9874 11
9876 12
9877 13
23423 14
27985 15
29347 16
98776 17

最後分享下我犯的一個弱智錯誤,繼承Mapper和Reducer兩個虛類後必須實現map和reduce函數,但是我reduce函數不小心寫成reducer,導致整個程序相當於從來沒有進入reduce階段,導致最後輸出的結果一直是map的中間結果,還好Xia兄過來看後發現了這個錯誤。大家以後可以加上標志@Override,這樣子以後萬一不小心寫錯了編譯器也可以提示。

更多Hadoop相關信息見Hadoop 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=13

Copyright © Linux教程網 All Rights Reserved