java hadoop(五) hadoop mapreduce 排序和序列化案例

8 篇文章 0 订阅
5 篇文章 0 订阅

什么是序列化与反序列化:

序列化(Serialization)是指把结构化对象转化为字节流。

反序列化(Deserialization)是序列化的逆过程。把字节流转为结构化对象。

mapreduce中的序列化:

实现这个接口即可

第一个方法是进行序列化

第二个方法是反序列化

mapreduce中的排序:

WritableComparable 接口包含了排序以及序列化,如果我们序列化的同时还需要排序,就实现这个接口即可。

 

需求:

代码实现:

目录结构:

包装类:通过继承实现排序与序列化

package sortserializedemo;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class SortBean implements WritableComparable<SortBean> {
    private String word;
    private int num;

    public SortBean() {
    }

    public SortBean(String word, int num) {
        this.word = word;
        this.num = num;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    //排序
    public int compareTo(SortBean o) {
        //按字典顺序排
        int res = this.word.compareTo(o.word);
        //如果大小相同  比较num
        if(res==0){
            return this.num - o.num;
        }
        return res;
    }

    @Override
    public String toString() {
        return "SortBean{" +
                "word='" + word + '\'' +
                ", num=" + num +
                '}';
    }

    //实现序列化
    public void write(DataOutput out) throws IOException {

        out.writeUTF(word);
        out.writeInt(num);
    }

    //实现反序列化
    public void readFields(DataInput in) throws IOException {
        this.word=in.readUTF();
        this.num=in.readInt();
    }
}

Mapper:

package sortserializedemo;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MyMapper extends Mapper<LongWritable, Text,SortBean, NullWritable> {

    public void map(LongWritable longWritable, Text text, OutputCollector<SortBean, NullWritable> outputCollector, Reporter reporter) throws IOException {
        //分隔文本内容 取出K2
        String[] split = text.toString().split("\t");
        //包装
        SortBean sortBean = new SortBean(split[0], Integer.parseInt(split[1]));
        //K2 sortBean V2 为空 后边基于K2进行操作
        outputCollector.collect(sortBean,NullWritable.get());
    }
}

Reducer:

package sortserializedemo;

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

import java.io.IOException;

public class MyReducer extends Reducer<SortBean, NullWritable,SortBean,NullWritable> {

    @Override
    protected void reduce(SortBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        context.write(key, NullWritable.get());
    }
}

Main:

package sortserializedemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import partitiondemo.MyMapper;
import partitiondemo.MyPartitioner;
import partitiondemo.MyReducer;
import partitiondemo.ReducerMain;

public class ReduceMain {

    public static void main(String[] args) throws Exception{
        //创建job
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, "my-partition");
        //指定job所在jar包
        job.setJarByClass(ReducerMain.class);
        //指定文件读取方式 路径
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path("hdfs://192.168.40.150:9000/test.txt"));
        //指定mapper
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);


        //指定reducer
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //指定输出方式类以及输出路径 目录必须不存在
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, new Path("hdfs://192.168.40.150:9000/lgy_test/res"));

        //将job提交到yarn集群
        boolean bl = job.waitForCompletion(true);
        System.exit(bl?0:1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整全套资源下载地址:https://download.csdn.net/download/qq_27595745/65977804 【完整课程列表】 大数据与云计算教程课件 优质大数据课程 01.Hadoop简介与安装入门(共29页).pptx 大数据与云计算教程课件 优质大数据课程 02.MapReduce(共23页).pptx 大数据与云计算教程课件 优质大数据课程 03.Hadoop YARN(共25页).pptx 大数据与云计算教程课件 优质大数据课程 04.MapReduce Eclipse开发插件(共20页).pptx 大数据与云计算教程课件 优质大数据课程 05.Hadoop入门数据分析实战(共57页).pptx 大数据与云计算教程课件 优质大数据课程 06.HDFS(共38页).pptx 大数据与云计算教程课件 优质大数据课程 07.HDFS Shell命令(共21页).pptx 大数据与云计算教程课件 优质大数据课程 08.HDFS文件接口(共41页).pptx 大数据与云计算教程课件 优质大数据课程 09.MapReduce序列化(共29页).pptx 大数据与云计算教程课件 优质大数据课程 10.MapReduce MP过程进阶(共42页).pptx 大数据与云计算教程课件 优质大数据课程 11.MapReduce IO操作(共61页).pptx 大数据与云计算教程课件 优质大数据课程 12.序列化框架(共28页).pptx 大数据与云计算教程课件 优质大数据课程 13.深入MapReduce应用开发(共21页).pptx 大数据与云计算教程课件 优质大数据课程 14.Hadoop集群配置(共6页).pptx 大数据与云计算教程课件 优质大数据课程 15.Hive(共46页).pptx 大数据与云计算教程课件 优质大数据课程 16.Hive操作(共43页).pptx 大数据与云计算教程课件 优质大数据课程 17.Hive查询(共32页).pptx 大数据与云计算教程课件 优质大数据课程 18.HBase(共43页).pptx 大数据与云计算教程课件 优质大数据课程 19.Pig(共33页).pptx 大数据与云计算教程课件 优质大数据课程 20.Pig Latin(共36页).pptx 大数据与云计算教程课件 优质大数据课程 21.Pig模式与函数(共64页).pptx 大数据与云计算教程课件 优质大数据课程 22.Zookeeper(共28页).pptx 大数据与云计算教程课件 优质大数据课程 23.Zookeeper服务(共47页).pptx 大数据与云计算教程课件 优质大数据课程 24.使用Zookeeper构建应用(共34页).pptx 大数据与云计算教程课件 优质大数据课程 25.Sqoop(共19页).pptx 大数据与云计算教程课件 优质大数据课程 26.深入Sqoop的导入(共29页).pptx 大数据与云计算教程课件 优质大数据课程 27.深入Sqoop导出(共19页).pptx 大数据与云计算教程课件 优质大数据课程 28.Flume(共33页).pptx 大数据与云计算教程课件 优质大数据课程 29.Kafka(共30页).pptx 大数据与云计算教程课件 优质大数据课程 30.Kafka开发(共34页).pptx 大数据与云计算教程课件 优质大数据课程 31.Strom(共14页).pptx 大数据与云计算教程课件 优质大数据课程 32.Spark入门之Scala(共173页).pptx 大数据与云计算教程课件 优质大数据课程 33.Spark入门(共40页).pptx 大数据与云计算教程课件 优质大数据课程 34.SparkSQL(共15页).pptx 大数据与云计算教程课件 优质大数据课程 35.Oozie(共41页).pptx 大数据与云计算教程课件 优质大数据课程 36.Impala(共20页).pptx 大数据与云计算教程课件 优质大数据课程 37.Solr(共38页).pptx 大数据与云计算教程课件 优质大数据课程 38.Lily(共23页).pptx 大数据与云计算教程课件 优质大数据课程 39.Titan(共20页).pptx 大数据与云计算教程课件 优质大数据课程 40.Neo4j(共50页).pptx 大数据与云计算教程课件 优质大数据课程 41.Elasticsearch(共17页).pptx

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值