Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
courses:mapreduce-tutorial:step-27 [2012/01/28 19:04] straka |
courses:mapreduce-tutorial:step-27 [2012/01/31 14:39] (current) straka |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== MapReduce Tutorial : Custom data types ====== | + | ====== MapReduce Tutorial : Running multiple Hadoop jobs in one source file ====== |
- | 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. | + | The Java API offers possibility to submit multiple Hadoop job in one source file. A job can be submitted either using |
+ | * [[http:// | ||
+ | * [[http:// | ||
- | ===== BERIntWritable | + | ===== Exercise 1 ===== |
+ | Improve the [[.: | ||
- | We want to implement BERIntWritable, | + | ===== Exercise 2 ===== |
- | The new class must implement | + | Implement |
- | <code java> | ||
- | public class BERIntWritable implements Writable { | ||
- | private int value; | ||
- | public void readFields(DataInput in) throws IOException { | + | ---- |
- | value = 0; | + | |
- | byte next; | + | <html> |
- | while (((next = in.readByte()) & 0x80) != 0) { | + | <table style=" |
- | value = (value | + | <tr> |
- | } | + | <td style="text-align:left; width: 33%; "></html>[[step-26|Step 26]]: Compression and job configuration.< |
- | value = (value | + | <td style=" |
- | } | + | <td style="text-align:right; width: 33%; "></html>[[step-28|Step 28]]: Custom data types.<html></td> |
- | + | </tr> | |
- | public void write(DataOutput out) throws IOException { | + | </table> |
- | int highest_pos | + | </html> |
- | while (highest_pos | + | |
- | while (highest_pos | + | |
- | out.writeByte(0x80 | ((value | + | |
- | highest_pos | + | |
- | } | + | |
- | out.writeByte(value & 0x7F); | + | |
- | } | + | |
- | </code> | + | |
- | Accessory methods '' | + | |
- | <code java> | + | |
- | public int get() { return value; } | + | |
- | public void set(int value) { this.value | + | |
- | public String toString() { return String.valueOf(value); } | + | |
- | } | + | |
- | </code> | + | |
- | Remark: If the '' | + | |
- | + | ||
- | 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 | + | |
- | <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); | + | |
- | } | + | |
- | } | + | |
- | </code> | + | |
- | + | ||
- | ===== PairWritable<A, B> ===== | + | |
- | + | ||
- | 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; } | + | |
- | } | + | |
- | </code> | + | |
- | Remark: Remark: If the '' | + | |
- | + | ||
- | We did not define '' | + | |
- | <code java> | + | |
- | public static class PairWritableComparable<A extends WritableComparable, | + | |
- | | + | |
- | 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 int compareTo(Object other) { | + | |
- | PairWritableComparable<A, B> otherPair = (PairWritableComparable< | + | |
- | int cmpFirst = first.compareTo(otherPair.getFirst()); | + | |
- | if (cmpFirst < 0) return -1; | + | |
- | if (cmpFirst > 0) return 1; | + | |
- | return second.compareTo(otherPair.getSecond()); | + | |
- | } | + | |
- | + | ||
- | 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 PairWritableComparable(A first, B second) { this.first = first; this.second = second; } | + | |
- | } | + | |
- | </code> | + | |
- | Remark: If the '' | + | |