输入文件为GBK,则只需在mapper或reducer程序中读取Text时,使用transformTextToUTF8(text, "GBK");进行一下转码,以确保都是以UTF-8的编码方式在运行。
public static Text transformTextToUTF8(Text text, String encoding) {
String value = null;
try {
value = new String(text.getBytes(), 0, text.getLength(), encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new Text(value);
}
输出文件为GBK,则重写TextOutputFormat类,public class GBKFileOutputFormat extends FileOutputFormat,把TextOutputFormat的源码拷过来,然后把里面写死的utf-8编码改成GBK编码。最后,在run程序中,设置job.setOutputFormatClass(GBKFileOutputFormat.class);