Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
courses:mapreduce-tutorial:step-27 [2012/01/28 18:23] straka |
courses:mapreduce-tutorial:step-27 [2012/01/28 18:57] straka |
||
---|---|---|---|
Line 7: | Line 7: | ||
We want to implement BERIntWritable, | We want to implement BERIntWritable, | ||
- | The new class must implement the [[http:// | + | The new class must implement the [[http:// |
<code java> | <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< | ===== PairWritable< | ||
+ | |||
+ | As another example, we implement a type consisting of two user-defined '' | ||
+ | <code java> | ||
+ | public static class PairWritable< | ||
+ | private A first; | ||
+ | private B second; | ||
+ | |||
+ | public void readFields(DataInput in) throws IOException { | ||
+ | first.readFields(in); | ||
+ | second.readFields(in); | ||
+ | } | ||
+ | |||
+ | public void write(DataOutput out) throws IOException { | ||
+ | first.write(out); | ||
+ | second.write(out); | ||
+ | } | ||
+ | |||
+ | public A getFirst() { return first; } | ||
+ | public B getSecond() { return second; } | ||
+ | public void setFirst(A first) { this.first = first; } | ||
+ | public void setSecond(B first) { this.second = second; } | ||
+ | public PairWritable(A first, B second) { this.first = first; this.second = second; } | ||
+ | } | ||
+ | </ | ||