Hadoop 统计不同引用次数的专利数目(三)

一、在(二)的基础之上做修改,代码如下:

 

import java.io.IOException;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 计算不同引用次数专利的数目
 * 
 * @author liuhongbo
 * 
 */
public class PatentCiteCount extends Configured implements Tool {

	public static class PatentCiteCountMapper extends
			Mapper<Text, Text, Text, IntWritable> {

		private final IntWritable one = new IntWritable(1);

		@Override
		protected void map(Text key, Text value, Context context)
				throws IOException, InterruptedException {
			context.write(value, one);
		}
	}

	public static class PatentCiteCountReducer extends
			Reducer<Text, IntWritable, Text, LongWritable> {
		private final LongWritable result = new LongWritable();

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable value : values) {
				sum += value.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	@Override
	public int run(String[] args) throws Exception {
		Job job = new Job(getConf());
		job.setJarByClass(getClass());
		job.setJobName("patentcitecount");

		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);

		job.setMapperClass(PatentCiteCountMapper.class);
		job.setReducerClass(PatentCiteCountReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);

		FileInputFormat.setInputPaths(job, new Path(
				"/patent/test/input/patentcitecount.txt"));
		FileOutputFormat.setOutputPath(job, new Path("/patent/test/outnput"));

		boolean success = job.waitForCompletion(true);

		return success ? 0 : 1;

	}

	public static void main(String[] args) throws Exception {
		int result = ToolRunner.run(new PatentCiteCount(), args);
		System.exit(result);
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现统计网站2023每日的访问次数,可以使用Hadoop中的MapReduce框架。具体步骤如下: 1. 将网站2023的访问日志文件上传至Hadoop分布式文件系统中。 2. 编写MapReduce程序,其中Mapper阶段将每条访问记录解析成日期和访问次数键值对,Reducer阶段将相同日期的访问次数累加起来。 3. 运行MapReduce程序,得到每日的访问次数统计结果。 以下是示例代码: Mapper部分: ```java public class AccessLogMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); private final static IntWritable ONE = new IntWritable(1); private Text date = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] fields = line.split("\t"); if (fields.length >= 3) { try { Date timestamp = sdf.parse(fields[0]); date.set(sdf.format(timestamp)); context.write(date, ONE); } catch (ParseException e) { // ignore bad records } } } } ``` Reducer部分: ```java public class AccessLogReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable totalCount = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable value : values) { count += value.get(); } totalCount.set(count); context.write(key, totalCount); } } ``` 运行命令: ```bash hadoop jar access-log.jar AccessLog /input/access.log /output ``` 其中,`access-log.jar`是打包好的MapReduce程序,`AccessLog`是程序的入口类,`/input/access.log`是输入文件路径,`/output`是输出文件路径。最终的统计结果将保存在`/output`目录下的结果文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值