現在有多個輸入文件,每個文件中的每行內容均為一個整數。要求讀取所有文件中的整數,進行升序排序后,輸出到一個新的文件中,輸出的數據格式為每行兩個整數,第一個整數為第二個整數的排序位次,第二個整數為原待排列的整數。


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