Hadoop的自定制数据类型
一般有两个办法,一种较为简单的是针对值,另外一种更为完整的是对于键和值都适应的方法:
1、实现Writable接口:
<ol class="snippet-num" style="list-style: none; padding: 0px 0px 0px 4em; "><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_comment" style="color: rgb(171, 171, 171); font-style: italic; ">/* DataInput and DataOutput 类是java.io的类 */</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">interface</span><span class="sh_normal"> </span><span class="sh_classname">Writable</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFields</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataInput</span><span class="sh_normal"> </span>in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">write</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataOutput</span><span class="sh_normal"> </span>out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li></ol>
下面是一个小例子:
<ol class="snippet-num" style="list-style: none; padding: 0px 0px 0px 4em; "><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">class</span><span class="sh_normal"> </span><span class="sh_classname">Point3D</span> <span class="sh_usertype">implement</span><span class="sh_normal"> </span>Writable <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">Point3D</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fx<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fy<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fz<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fx<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fy<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>z <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fz<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">Point3D</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFields</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataInput</span><span class="sh_normal"> </span>in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">throws</span> IOException <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> z <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">write</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataOutput</span><span class="sh_normal"> </span>out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">throws</span> IOException <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_usertype">String</span><span class="sh_normal"> </span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> <span class="sh_string" style="color: rgb(43, 131, 186); ">", "</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> <span class="sh_string" style="color: rgb(43, 131, 186); ">", "</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li></ol>
2、对于键来说,需要指定排序规则(呃,这句话可能有点C++风格?),对此,Java版Hadoop的办法是实现WritableComparable这个泛型接口,WritableComparable,顾名思义了,一半是Writable,一半是Comparable,有点啰嗦,但简明,据说Java程序员们打字快写?~~
<ol class="snippet-num" style="list-style: none; padding: 0px 0px 0px 4em; "><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">interface</span><span class="sh_normal"> </span><span class="sh_classname">WritableComparable</span><span class="sh_symbol" style="color: rgb(0, 0, 222); "><</span>T<span class="sh_symbol" style="color: rgb(0, 0, 222); ">></span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFields</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataInput</span><span class="sh_normal"> </span>in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">write</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataOutput</span><span class="sh_normal"> </span>out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">int</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">compareTo</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">T</span><span class="sh_normal"> </span>other<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li></ol>
先给出下面的简单例子,再做说明和扩展。
<ol class="snippet-num" style="list-style: none; padding: 0px 0px 0px 4em; "><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">class</span><span class="sh_normal"> </span><span class="sh_classname">Point3D</span> <span class="sh_usertype">inplements</span><span class="sh_normal"> </span>WritableComparable <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">Point3D</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fx<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fy<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> fz<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fx<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fy<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>z <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> fz<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">Point3D</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">,</span> <span class="sh_number" style="color: rgb(13, 163, 68); ">0</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>0f<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFields</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataInput</span><span class="sh_normal"> </span>in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">throws</span> IOException <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> z <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> in<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">readFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">();</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">write</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">DataOutput</span><span class="sh_normal"> </span>out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">throws</span> IOException <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> out<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">writeFloat</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_usertype">String</span><span class="sh_normal"> </span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> <span class="sh_string" style="color: rgb(43, 131, 186); ">", "</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> <span class="sh_string" style="color: rgb(43, 131, 186); ">", "</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">toString</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">distanceFromOrigin</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">float</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> Math<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">sqrt</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span> x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">*</span>x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span> y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">*</span>y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">+</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">*</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">int</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">compareTo</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">Point3D</span><span class="sh_normal"> </span>other<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">compareTo</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">distanceFromOrigin</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(),</span> </li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> other<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">distanceFromOrigin</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">());</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">boolean</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">equals</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">Object</span><span class="sh_normal"> </span>o<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">if</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">!(</span>o <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">instanceof</span> Point3D<span class="sh_symbol" style="color: rgb(0, 0, 222); ">))</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">false</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_usertype">Point3D</span><span class="sh_normal"> </span>other <span class="sh_symbol" style="color: rgb(0, 0, 222); ">=</span> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>Point3D<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> o<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>x <span class="sh_symbol" style="color: rgb(0, 0, 222); ">==</span> o<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>x </li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">&&</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>y <span class="sh_symbol" style="color: rgb(0, 0, 222); ">==</span> o<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>y </li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">&&</span> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">this</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>z <span class="sh_symbol" style="color: rgb(0, 0, 222); ">==</span> o<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">;</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_comment" style="color: rgb(171, 171, 171); font-style: italic; ">/* 实现 hashCode() 方法很重要</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_comment" style="color: rgb(171, 171, 171); font-style: italic; "> * Hadoop的Partitioners会用到这个方法,后面再说 </span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_comment" style="color: rgb(171, 171, 171); font-style: italic; "> */</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">public</span> <span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">int</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">hashCode</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">()</span> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">{</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_keyword" style="color: rgb(10, 127, 109); font-weight: bold; ">return</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">floatToIntBits</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>x<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">^</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">floatToIntBits</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>y<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_symbol" style="color: rgb(0, 0, 222); ">^</span> Float<span class="sh_symbol" style="color: rgb(0, 0, 222); ">.</span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">floatToIntBits</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span>z<span class="sh_symbol" style="color: rgb(0, 0, 222); ">);</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "> <span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_cbracket" style="color: rgb(0, 0, 222); ">}</span></li></ol>
自定义Hadoop数据类型后,需要明确告诉Hadoop来使用它们。这是 JobConf 所能担当的了。使用setOutputKeyClass() / setOutputValueClass()方法即可:
<ol class="snippet-num" style="list-style: none; padding: 0px 0px 0px 4em; "><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">setOutputKeyClass</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">Class<T></span><span class="sh_normal"> </span>theClass<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> </li><li style="margin: 0px; padding: 0px 0px 0px 1.5em; list-style: decimal-leading-zero outside none; font-family: 'courier new'; "><span class="sh_type" style="color: rgb(196, 38, 56); font-weight: bold; ">void</span> <span class="sh_function" style="color: rgb(0, 0, 0); font-weight: bold; ">setOutputValueClass</span><span class="sh_symbol" style="color: rgb(0, 0, 222); ">(</span><span class="sh_usertype">Class<T></span><span class="sh_normal"> </span>theClass<span class="sh_symbol" style="color: rgb(0, 0, 222); ">)</span> </li></ol>
通常(默认条件下),这个函数对Map和Reduce阶段的输出都起到作用,当然也有专门的 setMapOutputKeyClass() / setReduceOutputKeyClass() 接口。