<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    第一個MapReduce任務(wù)

    Posted on 2008-08-23 11:08 dennis 閱讀(2658) 評論(0)  編輯  收藏 所屬分類: javaHadoop與分布式
        前兩天在公司內(nèi)網(wǎng)上搭了個2個節(jié)點hadoop集群,暫時沒有多大實際意義,僅用作自己的測試。遇到的問題在阿里巴巴這位仁兄的《Hadoop集群配置和使用技巧》都有提到的。也遇到了reduce任務(wù)卡住的問題,只需要在每個節(jié)點的/etc/hosts將集群中的機(jī)器都配置上即可解決。
       今天將一個日志統(tǒng)計任務(wù)用Hadoop MapReduce框架重新實現(xiàn)了一次,數(shù)據(jù)量并不大,每天分析一個2G多的日志文件罷了。先前是用Ruby配合cat、grep命令搞定,運行一次在50多秒左右,如果純粹采用Ruby的話CPU占用率非常高而且慢的無法忍受,利用IO.popen調(diào)用linux的cat、grep命令先期處理就好多了。看看這個MapReduce任務(wù):
    public class GameCount extends Configured implements
            org.apache.hadoop.util.Tool {
        
    public static class MapClass extends MapReduceBase implements
                Mapper
    <LongWritable, Text, Text, IntWritable> {

            
    private Pattern pattern;

            
    public void configure(JobConf job) {
                String gameName 
    = job.get("mapred.mapper.game");
                pattern 
    = Pattern.compile("play\\sgame\\s" + gameName
                        
    + ".*uid=(\\d+),score=(-?\\d+),money=(-?\\d+)");
            }

            @Override
            
    public void map(LongWritable key, Text value,
                    OutputCollector
    <Text, IntWritable> output, Reporter reporter)
                    
    throws IOException {
                String text 
    = value.toString();
                Matcher matcher 
    = pattern.matcher(text);
                
    int total = 0// 總次數(shù)
                while (matcher.find()) {
                    
    int record = Integer.parseInt(matcher.group(2));
                    output.collect(
    new Text(matcher.group(1)), new IntWritable(
                            record));
                    total 
    += 1;
                }
                output.collect(
    new Text("total"), new IntWritable(total));
            }
        }

        
    public static class ReduceClass extends MapReduceBase implements
                Reducer
    <Text, IntWritable, Text, IntWritable> {

            @Override
            
    public void reduce(Text key, Iterator<IntWritable> values,
                    OutputCollector
    <Text, IntWritable> output, Reporter reporter)
                    
    throws IOException {
                
    int sum = 0;
                
    while (values.hasNext()) {
                    sum 
    += values.next().get();
                }
                output.collect(key, 
    new IntWritable(sum));
            }

        }

        
    static int printUsage() {
            System.out
                    .println(
    "gamecount [-m <maps>] [-r <reduces>] <input> <output> <gamename>");
            ToolRunner.printGenericCommandUsage(System.out);
            
    return -1;
        }

       
    public int run(String[] args) throws Exception {
            JobConf conf 
    = new JobConf(getConf(), GameCount.class);
            conf.setJobName(
    "gamecount");

          
    conf.setOutputKeyClass(Text.class);
            conf.setOutputValueClass(IntWritable.class);

            conf.setMapperClass(MapClass.
    class);
            conf.setCombinerClass(ReduceClass.
    class);
            conf.setReducerClass(ReduceClass.
    class);

            List
    <String> other_args = new ArrayList<String>();
            
    for (int i = 0; i < args.length; ++i) {
                
    try {
                    
    if ("-m".equals(args[i])) {
                        conf.setNumMapTasks(Integer.parseInt(args[
    ++i]));
                    } 
    else if ("-r".equals(args[i])) {
                        conf.setNumReduceTasks(Integer.parseInt(args[
    ++i]));
                    } 
    else {
                        other_args.add(args[i]);
                    }
                } 
    catch (NumberFormatException except) {
                    System.out.println(
    "ERROR: Integer expected instead of "
                            
    + args[i]);
                    
    return printUsage();
                } 
    catch (ArrayIndexOutOfBoundsException except) {
                    System.out.println(
    "ERROR: Required parameter missing from "
                            
    + args[i - 1]);
                    
    return printUsage();
                }
            }
            
    // Make sure there are exactly 2 parameters left.
            if (other_args.size() != 3) {
                System.out.println(
    "ERROR: Wrong number of parameters: "
                        
    + other_args.size() + " instead of 2.");
                
    return printUsage();
            }
            FileInputFormat.setInputPaths(conf, other_args.get(
    0));
            FileOutputFormat.setOutputPath(conf, 
    new Path(other_args.get(1)));
            conf.set(
    "mapred.mapper.game", args[2]);
            JobClient.runJob(conf);
            
    return 0;
        }

        
    public static void main(String[] args) throws Exception {
            
    long start = System.nanoTime();
            
    int res = ToolRunner.run(new Configuration(), new GameCount(), args);
            System.out.println(
    "running time:" + (System.nanoTime() - start)
                    
    / 1000000 + " ms");
            System.exit(res);
        }

    }
        代碼沒啥好解釋的,就是分析類似"play game DouDiZhu result:uid=1871653,score=-720,money=0"這樣的字符串,分析每天玩家玩游戲的次數(shù)、分?jǐn)?shù)等。打包成GameCount.jar,執(zhí)行:
    hadoop jar GameCount.jar test.GameCount /usr/logs/test.log /usr/output GameName

       統(tǒng)計的運行時間在100多秒,適當(dāng)增加map和reduce任務(wù)個數(shù)沒有多大改善,不過CPU占用率還是挺高的。


    主站蜘蛛池模板: 免费a级毛片无码a∨免费软件 | 亚洲无码黄色网址| 亚洲精品国产日韩无码AV永久免费网| 久久久精品国产亚洲成人满18免费网站 | 100部毛片免费全部播放完整| 久久久www成人免费毛片| 免费无遮挡无码永久在线观看视频| 久久精品国产亚洲精品| 亚洲色欲或者高潮影院| 亚洲av无码一区二区三区四区| 一个人看的免费视频www在线高清动漫| 在线看片免费人成视频播| 在线天堂免费观看.WWW| 亚洲A∨午夜成人片精品网站| 亚洲av女电影网| 亚洲人成人无码.www石榴| 久久久久久久国产免费看 | www.免费在线观看| 免费国产成人午夜电影| 亚洲AV日韩精品久久久久| 亚洲综合一区国产精品| 人体大胆做受免费视频| 成人免费福利视频| 亚洲另类少妇17p| 亚洲日产2021三区| 十八禁的黄污污免费网站| 1000部拍拍拍18勿入免费视频下载| 国产精品久久免费视频| 久久久亚洲欧洲日产国码二区 | 偷自拍亚洲视频在线观看| 无码A级毛片免费视频内谢| 日本高清免费网站| 亚洲人成电影在线天堂| 色偷偷亚洲第一综合网| 8x8x华人永久免费视频| 亚洲精品tv久久久久久久久久| 亚洲另类图片另类电影| 久青草视频在线观看免费| 性色av免费观看| 亚洲一区综合在线播放| 成年免费a级毛片|