adoop MR mapreduce和序列化

mapreduce

分三部分

  • mapper
  • reducer
  • driver
    仿写 wordCount
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-03-31 20:48
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text text = new Text();
    private IntWritable count = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] words = line.split(" ");

        for (String word : words) {
            text.set(word);
            context.write(text, count);
        }
    }
}
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-03-31 21:19
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable value = new IntWritable();

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

    }
}
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-03-31 21:33
 */
public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    	//创建 job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
		//指定 job 启动的 main class
        job.setJarByClass(WordCountDriver.class);
		//指定 job 运行的 mapper 和 reducer
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
		//指定 mapper 的输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
		//指定reducer 的输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
		//指定文件的输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
		//job 开始执行
        boolean result = job.waitForCompletion(true);
        System.out.println(result?0:1);
    }
}
  • 项目配置 maven 打包 jar
	<!-- pom 文件增加打包插件-->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <!--指定 main 类 全限定名-->
                                <mainClass>com.zcz.study.hadoop.mapreduce.WordCountDriver</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
mvn clean package
hadoop jar customerWorkCount.jar com.zcz.study.hadoop.mapreduce.WordCountDriver /user/zcz/input/ /user/zcz/output/

序列化

  • hadoop 中 mapper 与 reducer 输出的对象都需要是 hadoop 序列化的对象 实现Writable
  • mapper 与 reducer 输出的 key 需要是可比较的 实现 Comparable
  • Mapeduce 中对象可实现WritableComparable接口 同时支持序列化和比较
  • mapper过程会对 key 进行排序, 所以 value 可以不需要比较
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-04-01 20:53
 */
public class Flow implements WritableComparable<Flow> {
    private Long upFlow;

    private Long downFlow;

    private Long sumFlow;

    public Long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(Long upFlow) {
        this.upFlow = upFlow;
    }

    public Long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(Long downFlow) {
        this.downFlow = downFlow;
    }

    public Long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(Long sumFlow) {
        this.sumFlow = sumFlow;
    }

    public Flow(Long upFlow, Long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = this.upFlow + this.downFlow;
    }

    public Flow() {
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }

    @Override
    public String toString() {
        return upFlow + "\t" +
                downFlow + "\t" +
                sumFlow ;
    }

    @Override
    public int compareTo(Flow o) {
        return this.sumFlow> o.getSumFlow()?1:-1;
    }
}
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-04-01 20:59
 */
public class FlowMapper extends Mapper<LongWritable, Text, Text, Flow> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] strs = value.toString().split("\t");

        Flow flow = new Flow(Long.valueOf(strs[3]), Long.valueOf(strs[4]));
        context.write(new Text(strs[1]), flow);

    }
}
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-04-01 21:09
 */
public class FlowReducer extends Reducer<Text, Flow,Text, Flow> {
    @Override
    protected void reduce(Text key, Iterable<Flow> values, Context context) throws IOException, InterruptedException {
        Long up = 0L;
        Long down = 0L;
        Long sum = 0L;
        for (Flow flow : values) {
            up = up + flow.getUpFlow();
            down = down + flow.getDownFlow();
            sum = sum + flow.getSumFlow();
        }
        context.write(key, new Flow(up, down));
    }
}
/**
 * <h3>study-all</h3>
 *
 * <p></p>
 *
 * @Author zcz
 * @Date 2020-04-01 21:15
 */
public class SerializeDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(SerializeDriver.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Flow.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Flow.class);

        job.setMapperClass(FlowMapper.class);
        job.setReducerClass(FlowReducer.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean result = job.waitForCompletion(true);

        System.out.println(result? 0 : 1 );
    }

}
  • java 类型对应 mapred 序列化类型
Java类型Hadoop Writable类型
booleanBooleanWritable
byteByteWritable
intIntWritable
floatFloatWritable
longLongWritable
doubleDoubleWritable
StringText
mapMapWritable
arrayArrayWritable
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值