现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,进行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个整数为第二个整数的排序位次,第二个整数为原待排列的整数。


package org.apache.hadoop.examples;
import java.util.HashMap;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 B_sortInt {
	public static Integer numsum = new Integer(0);
	public B_sortInt() {
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://localhost:9000");
		String[] otherArgs = new String[]{"input","output"};
		if(otherArgs.length < 2) {
			System.err.println("Usage: wordcount <in> [<in>...] <out>");
			System.exit(2);
		}

		Job job = Job.getInstance(conf, "sort");
		job.setJarByClass(B_sortInt.class);
		job.setMapperClass(B_sortInt.TokenizerMapper.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(Text.class);
		job.setReducerClass(B_sortInt.IntSumReducer.class);
		job.setOutputKeyClass(Text.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 IntSumReducer extends Reducer<IntWritable, Text, Text, Text> {
		private Text word = new Text();
		public IntSumReducer() {
		}

		public void reduce(IntWritable key, Iterable<Text> values, Reducer<IntWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
			this.word.set(key.toString());
			numsum+=1;
			context.write(new Text(numsum.toString()), word);
		}
		//System.out.println(key.toString()+"\n"+result.toString());
	}


	public static class TokenizerMapper extends Mapper<Object, Text, IntWritable, Text> {
		private IntWritable one = new IntWritable();
		public TokenizerMapper() {
		}

		public void map(Object key, Text value, Mapper<Object, Text, IntWritable, Text>.Context context) throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while(itr.hasMoreTokens()) {
				String tmpstr = itr.nextToken();
				this.one.set(Integer.parseInt(tmpstr));
				//System.out.println("["+one.toString()+"]");
				context.write(one, new Text("a"));
			}

		}
	}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 设计一个从5个整数中取最小数和最大数的程序 输入输入只有一组测试数据,为五个不大于1万的正整数输出输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。 1.1编程基础之输入输出-02:输出第二个整数 输入整数,按照从大到小的排序输出(两个方法,一个思考) 用指向指针的指针的方法对n个整数排序并输出 要求将排序单独写成一个函数。n个整数在主函数中输入,最后在主函数中输出 用指向指针的指针的方法对n个整数排序并输出要求将排序单独写成一个函数。n个整数在主函数中输入,最后在主函数中输出 10.21用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。n和正整数在主函数中输入。最后在主函数中输出。 python在文件中输入整数 Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。 Input 输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数 班上有学生若干名,已知每名学生的成绩(整数),求班上所有学生的平均成绩,保留到小数点后两位。同时输出该平均成绩整数部分四舍五入后的数值。 第一行有一个整数n(1<= n <= 100),表示学生的人数。其后n行每行有1个整数,表示每个学生的成绩,取值在int范围内。
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM