maven3.5+hadoop2.7.3统计米骑测试日志KPI指标(四)

本文介绍了一个使用Hadoop MapReduce实现的IP访问次数统计程序,该程序能够找出访问次数最多的IP地址。通过Mapper阶段筛选并统计每个IP的访问次数,Reducer阶段汇总数据,最终输出访问次数最高的IP。

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

下面统计下访问次数最高的ip.

(1)主要通过比较每个ip的次数,然后保留次数大的,最后输出次数最大的.比较完后,用cleanup清除资源.

public class Kpi_IP_TopCount {
	
	public static class TopMapper extends Mapper<Object, Text, LongWritable, NullWritable> {
		long max = Long.MIN_VALUE;
		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			if(value.toString().indexOf("\\")==-1){
				// split 
    			String line = value.toString();
    			String[] fields = line.split("\t");    			
    			long temp = Long.parseLong(fields[1]);
    			if(temp>max)
    				max = temp;
			}
			
		}
		protected void cleanup(Context context) throws java.io.IOException ,InterruptedException {
			context.write(new LongWritable(max), NullWritable.get());
		}
	}

	
	public static class TopReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable> {
		long max = Long.MIN_VALUE;

		public void reduce(LongWritable key, Iterable<NullWritable> values, Context context)
				throws IOException, InterruptedException {			
			long temp = key.get();
			if(temp>max){
				max = temp;
			}
		}
		protected void cleanup(Context context) throws java.io.IOException ,InterruptedException {
			context.write(new LongWritable(max), NullWritable.get());
		}
	}

	public static void main(String[] args) throws Exception {		
		Configuration conf = new Configuration();

		Job job = new Job(conf, "ip count topCount");
		job.setJarByClass(Kpi_IP_TopCount.class);
		job.setMapperClass(TopMapper.class);
		//job.setMapOutputKeyClass(LongWritable.class);
		//job.setMapOutputValueClass(NullWritable.class);
		//job.setCombinerClass(TopReducer.class);
		
		job.setReducerClass(TopReducer.class);
		job.setOutputKeyClass(LongWritable.class);
		job.setOutputValueClass(NullWritable.class);		
		
		FileInputFormat.addInputPath(job, new Path("hdfs://119.29.174.43:9000/user/hadoop/kpi_ip_log_Input"));
		FileOutputFormat.setOutputPath(job, new Path("hdfs://119.29.174.43:9000/user/hadoop/kpi_ip_log_OutTopCount"));
					
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值