歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hadoop涉及GBK編碼的文件

Hadoop涉及GBK編碼的文件

日期:2017/3/1 9:56:57   编辑:Linux編程

Hadoop源代碼中涉及編碼問題時都是寫死的utf-8,但是不少情況下,也會遇到輸入文件和輸出文件需要GBK編碼的情況。

輸入文件為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<K, V> extends FileOutputFormat<K, V>,把TextOutputFormat的源碼拷過來,然後把裡面寫死的utf-8編碼改成GBK編碼。最後,在run程序中,設置job.setOutputFormatClass(GBKFileOutputFormat.class);

更多Hadoop相關信息見Hadoop 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=13

Copyright © Linux教程網 All Rights Reserved