• 自定义MapReduce的类型


    package org.apache.hadoop.mapreduce.io;
    
    import java.io.DataInput;
    import java.io.DataOutput;
    import java.io.IOException;
    
    import org.apache.hadoop.io.WritableComparable;
    
    /***
     * customize writable eg.order
     * 
     * @author nele
     * 
     */
    public class OrderWritable implements WritableComparable<OrderWritable> {
    
        private String orderId;
    
        private float price;
    
        public OrderWritable() {
        }
    
        public OrderWritable(String orderId, float price) {
            set(orderId, price);
        }
    
        public void set(String orderId, float price) {
            this.orderId = orderId;
            this.price = price;
        }
    
        public String getOrderId() {
            return orderId;
        }
    
        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }
    
        public float getPrice() {
            return price;
        }
    
        public void setPrice(float price) {
            this.price = price;
        }
    
        public void write(DataOutput out) throws IOException {
            out.writeUTF(orderId);
            out.writeFloat(price);
        }
    
        public void readFields(DataInput in) throws IOException {
            this.orderId = in.readUTF();
            this.price = in.readFloat();
        }
    
        
        public int compareTo(OrderWritable o) {
            int comp = this.orderId.compareTo(o.orderId);
            if (comp == 0) {
                    return Float.valueOf(this.price).compareTo(Float.valueOf(o.price));
            }
            return comp;
        }
    
        @Override
        public String toString() {
            return   orderId + "	" + price;
        }
        
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((orderId == null) ? 0 : orderId.hashCode());
            result = prime * result + Float.floatToIntBits(price);
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            OrderWritable other = (OrderWritable) obj;
            if (orderId == null) {
                if (other.orderId != null)
                    return false;
            } else if (!orderId.equals(other.orderId))
                return false;
            if (Float.floatToIntBits(price) != Float.floatToIntBits(other.price))
                return false;
            return true;
        }
        
        
        
        
    
    }

    这样就可以,在mapreduce中使用。

    需要根据具体的情境具体的设计。

     
  • 相关阅读:
    Borland C++ Builder Practical learning series
    vmware打开vmx文件不能创建虚拟机的问题
    c3p0 连接数据库失败的问题
    外在 挺直背和走路的问题
    JAVAWEB tomcat服务器启动错误原因总结
    JAVAWEB 项目注册登录模块问题总结
    JAVA eclipse Maven项目红叹号解决方案
    JAVA 文件读取写入后 md5值不变的方法
    Git的安装配置(win环境)
    JAVA 静态方法和实例方法的区别 (图表)
  • 原文地址:https://www.cnblogs.com/nele/p/5177675.html
Copyright © 2020-2023  润新知