MapReduce对 file1.txt , file2.txt里面的内容进行去重,排序,并输出结果

题目:利用MapReduce对 file1.txt和 file2.txt里面对里面的内容进行去重,排序,并输出结果。。。

1.Mapper阶段:
                主要是对<k1,v1>进行排序,排序之后<k2,v2>作为Map的输出;

public class DistinctMapper extends Mapper<LongWritable,Text,Text,Text>{
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		context.write(value, new Text()); //置v2为空,不可直接写null
	}
}

2.Reducer阶段:此时<k2,v2>是已经排好序的,
           

public class DistinctReducer extends Reducer<Text, Text, Text, Text> {
	 @Override
	protected void reduce(Text key, Iterable<Text> values, Context context)
			throws IOException, InterruptedException {
            //v3可以直接:null , 在<k3,v3>该阶段已对k3进行去重处理
	
        	 context.write(key, null); 
	}
}

3.Driver阶段-主类

public class DistinctDriver {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		 Path  outfile=new Path("file:///D:/outToDate");
		 FileSystem fs=outfile.getFileSystem(conf);
		 if(fs.exists(outfile)){
			 fs.delete(outfile,true);
		 }
		
	    Job job = Job.getInstance(conf);		  
		job.setJarByClass(DistinctDriver.class);
		job.setJobName("mysort");
		job.setMapperClass(DistinctMapper.class);//输入数据方法
		job.setReducerClass(DistinctReducer.class);//计算结果
		 
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		 
		 FileInputFormat.addInputPath(job, new Path("file:///D:/quchong"));
		 FileOutputFormat.setOutputPath(job, outfile);
	 
		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}
}

4.处理结果
 

【file1.txt】
        2012-3-1 a
	2012-3-2 b
	2012-3-3 c
	2012-3-4 d
	2012-3-5 a
	2012-3-6 b
	2012-3-7 c
	2012-3-3 c
【file2.txt】
    2012-3-1 b
	2012-3-2 a
	2012-3-3 b
	2012-3-4 d
	2012-3-5 a
	2012-3-6 c
	2012-3-7 d
	2012-3-3 c
part-r-00000  --运行程序输出的结果(已去重并且排序后的数据)
    2012-3-1 a
    2012-3-1 b
    2012-3-2 a
    2012-3-2 b
    2012-3-3 b
    2012-3-3 c
    2012-3-4 d
    2012-3-5 a
    2012-3-6 b
    2012-3-6 c
    2012-3-7 c
    2012-3-7 d

很简单的一个去重排序的小程序!!!

 

 

 

 

 

 

 

 

好的,这个问题可以分为两个部分来回答。 第一部分是编写MapReduce程序,对文本文件中的单词进行计数。MapReduce程序可以使用Java编写,主要包括两个部分: 1. Map函数:将输入的文本数据按照指定的分隔符进行切分,并输出每个单词的出现次数,输出格式为(key,value)形式,其中key为单词,value为出现次数。 2. Reduce函数:对Map函数输出的相同key的value进行求和,得到该单词在文本中出现的总次数,并将结果输出到HDFS。 以下是MapReduce程序的Java代码示例: ```java public class WordCount { public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "wordcount"); job.setJarByClass(WordCount.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } ``` 第二部分是将MapReduce程序输出结果输出到HDFS中。可以使用Hadoop命令行工具将结果文件上传到HDFS中,命令格式为: ``` $ hadoop fs -put <local_file> <hdfs_dir> ``` 其中,`local_file`是本地计算机上的结果文件路径,`hdfs_dir`是HDFS中的目标目录。 例如,如果结果文件名为`result.txt`,本地路径为`/home/hadoop/result.txt`,HDFS目标目录为`/output`,则命令为: ``` $ hadoop fs -put /home/hadoop/result.txt /output ``` 这样就可以将MapReduce程序输出结果上传到HDFS中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值