java - 如何使用Java Hadoop MapReduce以降序对数据集中的列进行排序?

标签 java sorting hadoop mapreduce hadoop-partitioning

我的数据文件是:

Utsav   Chatterjee  Dangerous   Soccer  Coldplay    4
Rodney  Purtle  Awesome Football    Maroon5 3
Michael Gross   Amazing Basketball  Iron Maiden 6
Emmanuel    Ezeigwe Cool    Pool    Metallica   5
John    Doe Boring  Golf    Linkin Park 8
David   Bekham  Godlike Soccer  Justin Beiber   89
Abhishek    Kumar   Geek    Cricket Abhishek Kumar  7
Abhishek    Singh   Geek    Cricket Abhishek Kumar  7

我想在调用hadoop jar时将列号作为参数传递,并且我要求根据该特定列以降序对整个数据集进行排序。通过将所需的列设置为映射器输出中的键,我可以轻松地按升序进行此操作。但是,我无法以降序完成此操作。

我的Mapper和Reducer代码是:
public static class Map extends Mapper<LongWritable,Text,Text,Text>{
        public static void map(LongWritable key, Text value, Context context)
        throws IOException,InterruptedException 
        {
            Configuration conf = context.getConfiguration();
            String param = conf.get("columnRef");
            int colref = Integer.parseInt(param);
            String line = value.toString();
            String[] parts = line.split("\t");
            context.write(new Text(parts[colref]), value);
            }
        }

    public static class Reduce extends Reducer<Text,Text,Text,Text>{
        public void reduce(Text key, Iterable<Text> value, Context context)
        throws IOException,InterruptedException 
        {
            for (Text text : value) {
                context.write(text,null );
            }
        }
    }

我的比较器类是:
public static class sortComparator extends WritableComparator {

         protected sortComparator() {
          super(LongWritable.class, true);
          // TODO Auto-generated constructor stub
         }

         @Override
         public int compare(WritableComparable o1, WritableComparable o2) {
          LongWritable k1 = (LongWritable) o1;
          LongWritable k2 = (LongWritable) o2;
          int cmp = k1.compareTo(k2);
          return -1 * cmp;
         }

        }

我可能对比较器做错了。有谁可以帮我离开这里吗?当我运行此命令时,选择索引为5的列(最后一个数字列)作为这种排序的基础,我仍然会得到升序的结果。

驱动类别:
public static void main(String[] args) throws Exception {

        Configuration conf= new Configuration();
        conf.set("columnRef", args[2]);

        Job job = new Job(conf, "Sort");

        job.setJarByClass(Sort.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setSortComparatorClass(DescendingKeyComparator.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        Path outputPath = new Path(args[1]);

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

        outputPath.getFileSystem(conf).delete(outputPath);

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

关于如何能够完成此任务(降序)的任何建议对我都将非常有帮助!
谢谢

最佳答案

在驱动程序类中,以下代码行:job.setSortComparatorClass(DescendingKeyComparator.class);
您已将类设置为DescendingKeyComparator.class。将其设置为sortComparator.class。它应该工作。

关于java - 如何使用Java Hadoop MapReduce以降序对数据集中的列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35819747/

相关文章:

hadoop - 子查询中的子查询在配置单元中不起作用

postgresql - 无法使用 Cloudera Manager 安装 hadoop

Hadoop MapReduce : Custom Input Format

java - @QueryParam 默认情况下在 jersey 2 @BeanParam 的所有属性上

java - 如何跨多个 JVM 节点实现计数器的最简单/最快的方法

arrays - 如何在 O(n) 运行时间和 O(1) 空间复杂度内重组数组?

java - 时髦的冒泡排序 (Java Eclipse)

java - 使用Spring集成测试时如何控制@PostConstruct

java - 在我的 android 项目中遇到 nullPointerExecption

javascript - 根据与另一个数组的最佳匹配对对象数组进行排序