zoukankan      html  css  js  c++  java
  • hadoopMR自定义输入类型

    hadoop中的输入输出数据类型:

    BooleanWritable:标准布尔型数值

    ByteWritable:单字节数值

    DoubleWritable:双字节数值

    FloatWritable:浮点数

    常用的:

    IntWritable:整型数

    LongWritable:长整型数

    Text:使用UTF8格式存储的文本

    NullWritable:当<key, value>中的key或value为空时使用

    hadoop中的数据类型都实现了writable接口,以便这些类型的数据可以在网络传输和文件存储

    自定义数据类型:

    ①实现WritableComparable接口,实现write和readFields方法,完成可序列化和反序列化

    ②如果该数据类型需要有比较顺序时,实现CompareTo()方法

    ③必须声明一个无参构造方法,为了方便反射,创建对象

    package com.neworigin.temperature;

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

    import org.apache.hadoop.io.WritableComparable;

    public class Weather implements WritableComparable <Weather> {
    private int year;
    private int month;
    private int day;
    private int wd;

    public int getYear() {
    return year;
    }
    public void setYear(int year) {
    this.year = year;
    }
    public int getMonth() {
    return month;
    }
    public void setMonth(int month) {
    this.month = month;
    }
    public int getDay() {
    return day;
    }
    public void setDay(int day) {
    this.day = day;
    }
    public int getWd() {
    return wd;
    }
    public void setWd(int wd) {
    this.wd = wd;
    }
    public void write(DataOutput out) throws IOException {
    out.writeInt(year);
    out.writeInt(month);
    out.writeInt(day);
    out.writeInt(wd);

    }
    public void readFields(DataInput in) throws IOException {
    year=in.readInt();
    month=in.readInt();
    day=in.readInt();
    wd=in.readInt();
    }
    //将年月日按升序排序
    public int compareTo(Weather w) {
    int c1 = Integer.compare(this.year,w.getYear());
    if (c1==0)
    {
    int c2 = Integer.compare(this.month,w.getMonth());
    if (c2==0)
    {
    return Integer.compare(this.wd,w.getWd());
    }
    return c2;
    }
    return c1;
    }

    }

  • 相关阅读:
    centos安装字体
    【C++ Primer | 15】访问控制与继承、继承中的类作用域
    树与二叉树 | 哈夫曼树
    【C++ Primer | 10】泛型算法
    【APUE | 03】文件I/O
    二叉树
    图论算法
    【深度探索C++对象模型 | 02】构造函数语意学
    【APUE | 08】进程控制
    c++重点理解
  • 原文地址:https://www.cnblogs.com/chengdonghui/p/7831127.html
Copyright © 2011-2022 走看看