hadoop按字母数量进行排序

package demo;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;

import javax.security.auth.kerberos.KerberosTicket;
import javax.ws.rs.core.NewCookie;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.examples.SecondarySort.IntPair.Comparator;
import org.apache.hadoop.examples.Sort;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
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 Ch
{
  public static void main(String[] args)
    throws Exception
  {
	  Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
      System.err.println("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
}
    Job job = Job.getInstance(conf, "word");
    job.setJarByClass(Ch.class);
    job.setMapperClass(TokenizerMapper.class);
	job.setOutputKeyClass(IntWritable.class);
	job.setOutputValueClass(Text.class);

for (int i = 0; i < otherArgs.length - 1; i++) {

      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}

    FileOutputFormat.setOutputPath(job, new Path(otherArgs[(otherArgs.length - 1)]));

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


  public static class TokenizerMapper extends Mapper<Object, Text, Object, Text>
  {
    private static final IntWritable one = new IntWritable(1);
    HashMap<String,Integer> map = new HashMap<String,Integer>();
    private Text word = new Text();
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
    	String str1 = value.toString();
    	String str = str1.replaceAll(" ","");
        for (int i=0;i<str.length();i++){
            char c=str.charAt(i);
            String key1=String.valueOf(c);
            if(map.containsKey(key1)){
                Integer value1=map.get(key1);
                map.put(key1, value1+1);
            }
            else{
                if(c>='A'&&c<='Z'||c>='a'&&c<='z'){
                    map.put(key1, 1);
                }
            }
        }
        for (String key2 : map.keySet()) {
            Integer value2 = map.get(key2);  
            context.write(new IntWritable(value2), new Text(key2));
          
        }
    }
  }  
  }

猜你喜欢

转载自www.cnblogs.com/szj666/p/12654714.html