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

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

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

    莊周夢蝶

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

    第一個MapReduce任務

    Posted on 2008-08-23 11:08 dennis 閱讀(2667) 評論(0)  編輯  收藏 所屬分類: javaHadoop與分布式
        前兩天在公司內網上搭了個2個節點hadoop集群,暫時沒有多大實際意義,僅用作自己的測試。遇到的問題在阿里巴巴這位仁兄的《Hadoop集群配置和使用技巧》都有提到的。也遇到了reduce任務卡住的問題,只需要在每個節點的/etc/hosts將集群中的機器都配置上即可解決。
       今天將一個日志統計任務用Hadoop MapReduce框架重新實現了一次,數據量并不大,每天分析一個2G多的日志文件罷了。先前是用Ruby配合cat、grep命令搞定,運行一次在50多秒左右,如果純粹采用Ruby的話CPU占用率非常高而且慢的無法忍受,利用IO.popen調用linux的cat、grep命令先期處理就好多了??纯催@個MapReduce任務:
    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// 總次數
                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"這樣的字符串,分析每天玩家玩游戲的次數、分數等。打包成GameCount.jar,執行:
    hadoop jar GameCount.jar test.GameCount /usr/logs/test.log /usr/output GameName

       統計的運行時間在100多秒,適當增加map和reduce任務個數沒有多大改善,不過CPU占用率還是挺高的。


    主站蜘蛛池模板: 97亚洲熟妇自偷自拍另类图片 | 色婷婷亚洲一区二区三区| 黄页免费在线观看| 久久久久亚洲AV成人网人人网站 | 羞羞视频在线观看免费| 色吊丝最新永久免费观看网站| 亚洲成人免费在线观看| 亚洲一级免费毛片| 亚洲人成网站18禁止久久影院| 蜜桃AV无码免费看永久| 亚洲AV色吊丝无码| 德国女人一级毛片免费| 亚洲av无码专区国产不乱码| 又大又黄又粗又爽的免费视频| 亚洲av成人一区二区三区观看在线 | 十九岁在线观看免费完整版电影| 亚洲精品乱码久久久久66| 成年免费a级毛片免费看无码| 国产亚洲精品a在线观看app| 未满十八18禁止免费无码网站 | 日本高清免费中文字幕不卡| 校园亚洲春色另类小说合集| 亚洲乱码中文字幕综合234| 国产一级一毛免费黄片| 亚洲成人动漫在线| 亚洲精品动漫免费二区| 国产精品亚洲二区在线| 亚洲乱码精品久久久久..| 99久久精品免费视频| 国产亚洲中文日本不卡二区| 四虎AV永久在线精品免费观看| 中文字幕无线码免费人妻| 亚洲色大成网站www永久| 国产精品成人免费视频网站京东| 噜噜噜亚洲色成人网站| 亚洲国产成人高清在线观看| 无码av免费毛片一区二区| 春暖花开亚洲性无区一区二区| 亚洲大尺度无码专区尤物| 在线观看H网址免费入口| 美女黄频视频大全免费的|