sorting - 排序数据Hadoop Mapreduce

标签 sorting hadoop mapreduce dataset

我有以下算法按字母顺序对数据进行排序

public void setup(Context context) throws IOException,
        InterruptedException {
      conf = context.getConfiguration();
      caseSensitive = conf.getBoolean("amasort.case.sensitive", true);

    }

    @Override
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      String line = (caseSensitive) ? value.toString() : value.toString().toLowerCase();
      word.set(line+"_"+key.toString());
      context.write(word, one);
      System.out.println("key:"+key.toString()+";value:"+value.toString());
      }
    }

  public static class ForwardReducer
       extends Reducer<Text,NullWritable,Text,NullWritable> {
    private NullWritable result = NullWritable.get();

    public void reduce(Text key, Iterable<NullWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {

      String originalWord = key.toString();
      originalWord = originalWord.substring(0, originalWord.lastIndexOf("_"));
      key.set(originalWord);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    GenericOptionsParser optionParser = new GenericOptionsParser(conf, args);
    String[] remainingArgs = optionParser.getRemainingArgs();
    Job job = Job.getInstance(conf, "word sort");
    job.setJarByClass(AmaSort.class);
    job.setMapperClass(LineMapper.class);
//    job.setCombinerClass(ForwardReducer.class);
    job.setReducerClass(ForwardReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(NullWritable.class);

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

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

我尝试使用此算法对包含(@xxxxxxx,0,tcp,xx,1,1,1,2,4,5,....)的mydata集进行排序,但是输出以@开头的所有行均已删除,并且数据行结构0,tcp,x1x1,1,114,....被修改,我只想对我的数据集使用此特定字符(@)进行排序,所有在文件的第一行以@开头的行,其余保持相同的结构。
任何人都可以帮助我修改此算法吗?

最佳答案

您可以使用下面的修改后的代码进行排序,

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

public class AmaSort
{
    static Configuration conf = null;
    private static boolean caseSensitive;
    private static Text word = new Text();

    public static class LineMapper extends Mapper<Object, Text, Text, NullWritable>{

        public void setup(Context context) throws IOException, InterruptedException
        {
            conf = context.getConfiguration();
            caseSensitive = conf.getBoolean("amasort.case.sensitive", true);

        }

        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException
        {
            String line = (caseSensitive) ? value.toString() : value.toString().toLowerCase();
            word.set(line);
            context.write(word, NullWritable.get());

        }
    }

    public static class ForwardReducer extends Reducer<Text, NullWritable, Text, NullWritable>
    {
        private NullWritable result = NullWritable.get();

        public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException
        {
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception
    {
    Configuration conf = new Configuration();
    GenericOptionsParser optionParser = new GenericOptionsParser(conf, args);
    String[] remainingArgs = optionParser.getRemainingArgs();
//  Job job = Job.getInstance(conf, "word sort");
    Job job = new Job(conf, "word sort");
    job.setJarByClass(AmaSort.class);
    job.setMapperClass(LineMapper.class);
    // job.setCombinerClass(ForwardReducer.class);
    job.setReducerClass(ForwardReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(NullWritable.class);

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

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

}

关于sorting - 排序数据Hadoop Mapreduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305720/

相关文章:

c++ - std::sort 不适用于重载 < 运算符的用户定义对象

mongodb - 通过 mongodb 加权平均评分

hadoop - 如何在 Hadoop 1.1.1 上安装 Oozie 3.3.1?

hadoop - 在 HDFS 中遇到大量文件时,实际理想的 NameNode 内存大小是多少

hadoop - 什么时候使用身份映射器/缩减器?

NFS 上的 Hadoop 集群

ios - 按键中的字符串日期对 NSDictionaries 的 NSArray 进行排序?

python - 从 Python 中的字母数字字符串列表中执行降序数字排序,然后升序字母排序

python - 在 Python 3 中按字典顺序对混合数据类型的深度嵌套列表进行排序

java - JobControl 和 JofConf.setMapperClass() 错误