Hadoop MapReduce 简单案例--求素数个数

本文介绍了一个使用Hadoop MapReduce编写的Java程序,该程序用于从输入文本中读取一系列数字,并统计其中素数的数量。Map阶段将每个数字作为键值对输出,Reduce阶段负责检查每个数字是否为素数并进行计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 Prime {
//map只传值
	public static class SumMapper extends
			Mapper<Object, Text, Text, LongWritable> {
		private Text word = new Text();
		private static LongWritable one = new LongWritable(1);
		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {		
				  String s = itr.nextToken();  
	                long val = Long.parseLong(s);
	                one.set(val);context.write(word, one);
			}			
		}
	}
//reduce判断是否为素数
 public static class SumReducer extends
				Reducer<Text, LongWritable, Text, LongWritable> {
			private LongWritable result = new LongWritable();
			private Text m1 = new Text("Sum");
			public void reduce(Text key, Iterable<LongWritable> values,
					Context context) throws IOException, InterruptedException {
				long sum=0;
				for (LongWritable val : values) {
					int a=2;
					long b=val.get();
					while(a<b){
						if(b%a==0)
							break;
						a++;
					}
					if(a==b){
						sum++;
					}					
				}
				result.set(sum);
				context.write(m1, result);
			}
		}
	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: numbersum <in> <out>");
			System.exit(2);
		}
		long startTime = System.currentTimeMillis();// 计算时间
		Job job = new Job(conf);
		job.setJarByClass(Prime.class);
		job.setMapperClass(SumMapper.class);
		job.setReducerClass(SumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		job.waitForCompletion(true);
		long endTime = System.currentTimeMillis();
		System.out.println("time=" + (endTime - startTime));
		System.exit(0);
	}
}

实现过程:

建立文件夹in 建立文档1

文档1中的数据:

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值