[ Skip to the content ]

Institute of Formal and Applied Linguistics Wiki


[ Back to the navigation ]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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:34]
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, which is an ''int'' stored in the format of ''pack "w", $num''. Quoting: //The bytes represent an unsigned integer in base 128, most significant digit first, with as few digits as possible. Bit eight (the high bit) is set on each byte except the last.// 
 + 
 +The new class must implement the [[http://hadoop.apache.org/common/docs/r1.0.0/api/org/apache/hadoop/io/Writable.html|Writable]] interface, i.e., methods ''readFields'' and ''write'': 
 + 
 +<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); 
 +  } 
 +</code> 
 +Some accessory methods are also needed in order to work with the value: 
 +<code java> 
 +  public int get() { return value; } 
 +  public void set(int value) { this.value = value; } 
 +</code> 
 + 
 +===== PairWritable<A, B> ===== 

[ Back to the navigation ] [ Back to the content ]