Hadoop自定义数据类型

Hadoop的自定制数据类型有两种,一种较为简单的是针对值,另外一种更为完整针对于键和值都适合 

一、针对值,实现 Writable 接口 
?
1
2
3
4
5
6
7
8
9
10
package org.apache.hadoop.io;
 
import java.io.DataOutput;
import java.io.DataInput;
import java.io.IOException;
 
public interface Writable {
   void write(DataOutput out) throws IOException;
   void readFields(DataInput in) throws IOException;
}

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
 
public class Address implements Writable {
     public String city;
     public String street;
     public int doorplate;
 
     public Address() {
         this ( "" , "" , 0 );
     }
 
     public Address(String city, String street, int doorplate) {
         this .city = city;
         this .street = street;
         this .doorplate = doorplate;
     }
 
     @Override
     public void write(DataOutput out) throws IOException {
         out.writeUTF( this .city);
         out.writeUTF( this .street);
         out.writeInt( this .doorplate);
     }
 
     @Override
     public void readFields(DataInput in) throws IOException {
         this .city = in.readUTF();
         this .street = in.readUTF();
         this .doorplate = in.readInt();
     }
 
     public String toString() {
         return this .city + "," + this .street + "," + this .doorplate;
     }
}

二、针对于键和值,需要指定排序规则,自定义类需要实现 WritableComparable 泛型接口

?
1
2
3
4
5
6
7
8
9
package org.apache.hadoop.io;
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
 
package java.lang;
import java.util.*;
public interface Comparable<T> {
     public int compareTo(T o);
}

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
 
public class Address implements WritableComparable {
     public String city;
     public String street;
     public int doorplate;
 
     public Address() {
         this ( "" , "" , 0 );
     }
 
     public Address(String city, String street, int doorplate) {
         this .city = city;
         this .street = street;
         this .doorplate = doorplate;
     }
 
     @Override
     public void write(DataOutput out) throws IOException {
         out.writeUTF( this .city);
         out.writeUTF( this .street);
         out.writeInt( this .doorplate);
     }
 
     @Override
     public void readFields(DataInput in) throws IOException {
         this .city = in.readUTF();
         this .street = in.readUTF();
         this .doorplate = in.readInt();
     }
 
     @Override
     public int compareTo(Object o) {
         Address other = (Address) o;
         int n = this .strCompareTo( this .city, other.city);
         if (n != 0 ) return n;
         n = this .strCompareTo( this .street, other.street);
         if (n != 0 ) return n;
         return ( this .doorplate < other.doorplate ? - 1 : ( this .doorplate == other.doorplate ? 0 : 1 ));
     }
 
     public String toString() {
         return this .city + "," + this .street + "," + this .doorplate;
     }
 
     public boolean equals(Object obj) {
         if ( this == obj) {
             return true ;
         }
         if (obj instanceof Address) {
             Address other = (Address) obj;
             return this .strEquals( this .city, other.city)
                     && this .strEquals( this .street, other.street)
                     && this .doorplate == other.doorplate;
         }
         return false ;
     }
 
     /**
      * 重写 hashCode() 方法很重要,Hadoop 的 Partitioners 会用到这个方法
      */
     public int hashCode() {
         return 13 * ( this .city == null ? 0 : this .city.hashCode())
                 + 67 * ( this .street == null ? 0 : this .street.hashCode())
                 + 151 * this .doorplate;
     }
 
     public boolean strEquals(String a, String b) {
         return (a == null && b == null ) || (a != null && a.equals(b));
     }
 
     public int strCompareTo(String a, String b) {
         if (a == null && b == null ) return 0 ;
         else if (a != null && b == null ) return 1 ;
         else if (a == null && b != null ) return - 1 ;
         else return a.compareTo(b);
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值