Hadoop排序问题(替代标题:1175不低于119!)

时间:2014-02-10 05:53:39

标签: java sorting hadoop mapreduce

我是Hadoop的新手,完成了典型的“计算日志中的IP地址”练习。现在我试图通过在第一个之后立即运行第二个MapReduce作业来对输出进行排序。几乎所有东西都在工作,除了输出收集器没有按照我喜欢的方式处理排序的事实。这是我输出的片段:

-101   71.59.196.132
-115   59.103.11.163
-1175  59.93.51.231
-119   127.0.0.1
-1193  115.186.128.19
-1242  59.93.64.161
-146   192.35.79.70

我无法弄清楚为什么,例如,1175被认为是比119更低的价值。我试过玩Comparators,但它没有产生任何积极的影响。

数据收集的Map和Reduce作业既标准又无问题。他们输出的列表很像上面的代码段,但完全没有排序。 SortMap,SortReduce和Runner类有点不同。这是我的Runner课程:

public class Runner {

public static void main(String[] args) throws Exception
{
    JobConf conf = new JobConf(Runner.class);
    JobConf sortStage = new JobConf(Runner.class);

    conf.setJobName("ip-count");
    conf.setMapperClass(IpMapper.class);
    conf.setMapOutputKeyClass(Text.class);
    conf.setMapOutputValueClass(IntWritable.class);
    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);       
    conf.setReducerClass(IpReducer.class);
    conf.setOutputValueGroupingComparator(IntWritable.Comparator.class);


    //Input and output from command line...
    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    sortStage.setJobName("sort-stage");
    sortStage.setMapperClass(SortMapper.class);
    sortStage.setMapOutputKeyClass(Text.class);
    sortStage.setMapOutputValueClass(IntWritable.class);
    sortStage.setReducerClass(SortReducer.class);
    sortStage.setOutputKeyClass(IntWritable.class);
    sortStage.setOutputValueClass(IntWritable.class);

    //Input and output from command line...
    FileInputFormat.setInputPaths(sortStage, new Path(args[2]));
    FileOutputFormat.setOutputPath(sortStage, new Path(args[3]));

    JobClient.runJob(conf);
    JobClient.runJob(sortStage);

}
}

“SortMapper”:

public class SortMapper extends MapReduceBase 
  implements Mapper<LongWritable, Text, Text, IntWritable>
{
private static final IntWritable one = new IntWritable(1);  
public void map(LongWritable fileOffset, Text lineContents,
  OutputCollector<Text, IntWritable> output, Reporter reporter)
  throws IOException {
  {
    //Grab the whole string, formatted as (Count /t IP), e.g., 101  128.10.3.40
    String ip = lineContents.toString();
    //Output it with a count of 1
    output.collect(new Text(ip), one);
  }
}
}

“SortReducer”:

public class SortReducer extends MapReduceBase implements Reducer<Text, IntWritable,      
IntWritable, Text> 
{
public void reduce(Text ip, Iterator<IntWritable> counts,
  OutputCollector<IntWritable, Text> output, Reporter reporter)
  throws IOException{

  String delimiter = "[\t]";
  String[] splitString = ip.toString().split(delimiter);

  //Count represented as 0-count to easily sort in descending order vs. ascending
  int sortCount = 0-Integer.parseInt(splitString[0]);
  output.collect(new IntWritable(sortCount), new Text(splitString[1]));
  }
}

这只是一个单节点的工作,所以我不认为分区是一个因素。很抱歉,如果这是一件小事 - 我在问题上度过了一段尴尬的时间,找不到任何处理这个特定排序问题的事情。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

您的数字按字母顺序进行比较。这是因为有字符串。如果你想象字母排序,aabc出现在aac之前。如果你把它们变成数字,1123就会出现在113之前。

如果要进行数字比较,则必须将它们转换为整数。