hadoop 编程规范(hadoop专利分析)

网上有很多hadoop例子,但是不难发现,即使是一个wordcount都有很多不一样的地方,我们不可能总拿着别人的例子跑,所以自己要总结出一套规范,让api即使更新也能马上适应过来。这里也以hadoop 专利分析作为炮灰
右键新建map/reduce项目,然后点击项目右键Mapper,Reducer,MapperReduce Driver 并且在MapperReduce Driver 里填上刚才新建的Mapper,Reducer的类名,建好后,并修改
MapperReduce Driver里的路径为args[0],args[1],然后Run AS 里选择RunConfiguration点击Javaapplication配置Arguments为:
hdfs://master:9000/user/input/file1.txt
hdfs://master:9000/user/aa
这种东西,这样一套规范就完成了
接下来,我们来对“专利分析”这个案例,一一进行分析

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MapClass extends Mapper<LongWritable, Text, Text, Text> {

    public void map(LongWritable ikey, Text ivalue, Context context) throws IOException, InterruptedException {
        String[] citation = ivalue.toString().split(",");  
        context.write(new Text(citation[1]), new Text(citation[0]));  
    }
}
源文件类似这样
专利号    引用专利号
K1  ,   V1
K2  ,   V2
K3  ,   V3
K1  ,   V3

LongWritable ikey 代表的是文本中的每一行
ivalue代表的就是文本里的值
String[] citation = ivalue.toString().split(“,”);
就是对文本以逗号为分界的分割
context.write(new Text(citation[1]), new Text(citation[0]));
Context 是MapReduce任务运行的一个上下文,包含了整个任务的全部信息
上下文写入:键为引用专利号,值为专利号的map,键是唯一的,所以hadoop会自动将值何在一起,即:

专利号    引用专利号
V1     K1
V2     K2
V3     K3 k1
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class Reduce extends Reducer<Text, Text, Text, Text> {

    public void reduce(Text _key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
          // process values  
        String csv = "";  
        for (Text val : values) {  
            if (csv.length() > 0) {  
                csv += ",";  
            }  
            csv += val.toString();  
        }  
        context.write(_key, new Text(csv)); 
    }

}
Text _key, Iterable<Text> values:
这里就是上面map分解后传给你的东西了
    即
    专利号    引用专利号
    V3       K3 k1

  String csv = "";  
        for (Text val : values) {  
            if (csv.length() > 0) {  
                csv += ",";  
            }  
            csv += val.toString();  
        }  
        context.write(_key, new Text(csv)); 
这里就是在value上加上逗号方便观察了
最后,这也是自动生成的代码。。。右键运行在hadoop选择你刚才配置的那个就可以了
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Driver {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "JobName");
        job.setJarByClass(Driver.class);
        job.setMapperClass(MapClass.class);

        job.setReducerClass(Reduce.class);

        // TODO: specify output types
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // TODO: specify input and output DIRECTORIES (not files)
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        if (!job.waitForCompletion(true))
            return;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值