Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
courses:mapreduce-tutorial:step-27 [2012/01/27 20:11] straka vytvořeno |
courses:mapreduce-tutorial:step-27 [2012/01/28 18:45] straka |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== MapReduce Tutorial : ====== | + | ====== MapReduce Tutorial : Custom data types ====== |
+ | |||
+ | An important feature of the Java API is that custom data and format types can be provided. In this step we implement two custom data types. | ||
+ | |||
+ | ===== BERIntWritable ===== | ||
+ | |||
+ | We want to implement BERIntWritable, | ||
+ | |||
+ | The new class must implement the [[http:// | ||
+ | |||
+ | <code java> | ||
+ | public class BERIntWritable implements Writable { | ||
+ | private int value; | ||
+ | |||
+ | public void readFields(DataInput in) throws IOException { | ||
+ | value = 0; | ||
+ | |||
+ | byte next; | ||
+ | while (((next = in.readByte()) & 0x80) != 0) { | ||
+ | value = (value << 7) | (next & 0x7F); | ||
+ | } | ||
+ | value = (value << 7) | next; | ||
+ | } | ||
+ | |||
+ | public void write(DataOutput out) throws IOException { | ||
+ | int highest_pos = 28; | ||
+ | while (highest_pos > 0 && (value & (0x7F << highest_pos)) == 0) highest_pos -= 7; | ||
+ | while (highest_pos > 0) { | ||
+ | out.writeByte(0x80 | ((value >> highest_pos) & 0x7F)); | ||
+ | highest_pos -= 7; | ||
+ | } | ||
+ | out.writeByte(value & 0x7F); | ||
+ | } | ||
+ | </ | ||
+ | Accessory methods '' | ||
+ | <code java> | ||
+ | public int get() { return value; } | ||
+ | public void set(int value) { this.value = value; } | ||
+ | public String toString() { return String.valueOf(value); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Such implementation can be used as a type of //values//. If we wanted to use it as a type of //keys//, we need to implement [[http:// | ||
+ | <code java> | ||
+ | public class BERIntWritable implements WritableComparable { | ||
+ | ... //Same as before | ||
+ | |||
+ | public int compareTo(Object other) { | ||
+ | int otherValue = ((BERIntWritable)other).get(); | ||
+ | return value < otherValue ? -1 : (value == otherValue ? 0 : 1); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== PairWritable< |