JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案
使用JAVA api讀取HDFS文件亂碼踩坑
想寫(xiě)一個(gè)讀取HFDS上的部分文件數(shù)據(jù)做預(yù)覽的接口,根據(jù)網(wǎng)上的博客實(shí)現(xiàn)后,發(fā)現(xiàn)有時(shí)讀取信息會(huì)出現(xiàn)亂碼,例如讀取一個(gè)csv時(shí),字符串之間被逗號(hào)分割
- 英文字符串a(chǎn)aa,能正常顯示
- 中文字符串“你好”,能正常顯示
- 中英混合字符串如“aaa你好”,出現(xiàn)亂碼
查閱了眾多博客,解決方案大概都是:使用xxx字符集解碼。抱著不信的想法,我依次嘗試,果然沒(méi)用。
解決思路
因?yàn)镠DFS支持6種字符集編碼,每個(gè)本地文件編碼方式又是極可能不一樣的,我們上傳本地文件的時(shí)候其實(shí)就是把文件編碼成字節(jié)流上傳到文件系統(tǒng)存儲(chǔ)。那么在GET文件數(shù)據(jù)時(shí),面對(duì)不同文件、不同字符集編碼的字節(jié)流,肯定不是一種固定字符集解碼就能正確解碼的吧。
那么解決方案其實(shí)有兩種
- 固定HDFS的編解碼字符集。比如我選用UTF-8,那么在上傳文件時(shí)統(tǒng)一編碼,即把不同文件的字節(jié)流都轉(zhuǎn)化為UTF-8編碼再進(jìn)行存儲(chǔ)。這樣的話在獲取文件數(shù)據(jù)的時(shí)候,采用UTF-8字符集解碼就沒(méi)什么問(wèn)題了。但這樣做的話仍然會(huì)在轉(zhuǎn)碼部分存在諸多問(wèn)題,且不好實(shí)現(xiàn)。
- 動(dòng)態(tài)解碼。根據(jù)文件的編碼字符集選用對(duì)應(yīng)的字符集對(duì)解碼,這樣的話并不會(huì)對(duì)文件的原生字符流進(jìn)行改動(dòng),基本不會(huì)亂碼。
我選用動(dòng)態(tài)解碼的思路后,其難點(diǎn)在于如何判斷使用哪種字符集解碼。參考下面的內(nèi)容,獲得了解決方案
java檢測(cè)文本(字節(jié)流)的編碼方式
需求:
某文件或者某字節(jié)流要檢測(cè)他的編碼格式。
實(shí)現(xiàn):
基于jchardet
<dependency> <groupId>net.sourceforge.jchardet</groupId> <artifactId>jchardet</artifactId> <version>1.0</version> </dependency>
代碼如下:
public class DetectorUtils {
private DetectorUtils() {
}
static class ChineseCharsetDetectionObserver implements
nsICharsetDetectionObserver {
private boolean found = false;
private String result;
public void Notify(String charset) {
found = true;
result = charset;
}
public ChineseCharsetDetectionObserver(boolean found, String result) {
super();
this.found = found;
this.result = result;
}
public boolean isFound() {
return found;
}
public String getResult() {
return result;
}
}
public static String[] detectChineseCharset(InputStream in)
throws Exception {
String[] prob=null;
BufferedInputStream imp = null;
try {
boolean found = false;
String result = Charsets.UTF_8.toString();
int lang = nsPSMDetector.CHINESE;
nsDetector det = new nsDetector(lang);
ChineseCharsetDetectionObserver detectionObserver = new ChineseCharsetDetectionObserver(
found, result);
det.Init(detectionObserver);
imp = new BufferedInputStream(in);
byte[] buf = new byte[1024];
int len;
boolean isAscii = true;
while ((len = imp.read(buf, 0, buf.length)) != -1) {
if (isAscii)
isAscii = det.isAscii(buf, len);
if (!isAscii) {
if (det.DoIt(buf, len, false))
break;
}
}
det.DataEnd();
boolean isFound = detectionObserver.isFound();
if (isAscii) {
isFound = true;
prob = new String[] { "ASCII" };
} else if (isFound) {
prob = new String[] { detectionObserver.getResult() };
} else {
prob = det.getProbableCharsets();
}
return prob;
} finally {
IOUtils.closeQuietly(imp);
IOUtils.closeQuietly(in);
}
}
}
測(cè)試:
String file = "C:/3737001.xml";
String[] probableSet = DetectorUtils.detectChineseCharset(new FileInputStream(file));
for (String charset : probableSet) {
System.out.println(charset);
}
Google提供了檢測(cè)字節(jié)流編碼方式的包。那么方案就很明了了,先讀一些文件字節(jié)流,用工具檢測(cè)編碼方式,再對(duì)應(yīng)進(jìn)行解碼即可。
具體解決代碼
pom
<dependency> <groupId>net.sourceforge.jchardet</groupId> <artifactId>jchardet</artifactId> <version>1.0</version> </dependency>
從HDFS讀取部分文件做預(yù)覽的邏輯
// 獲取文件的部分?jǐn)?shù)據(jù)做預(yù)覽
public List<String> getFileDataWithLimitLines(String filePath, Integer limit) {
FSDataInputStream fileStream = openFile(filePath);
return readFileWithLimit(fileStream, limit);
}
// 獲取文件的數(shù)據(jù)流
private FSDataInputStream openFile(String filePath) {
FSDataInputStream fileStream = null;
try {
fileStream = fs.open(new Path(getHdfsPath(filePath)));
} catch (IOException e) {
logger.error("fail to open file:{}", filePath, e);
}
return fileStream;
}
// 讀取最多l(xiāng)imit行文件數(shù)據(jù)
private List<String> readFileWithLimit(FSDataInputStream fileStream, Integer limit) {
byte[] bytes = readByteStream(fileStream);
String data = decodeByteStream(bytes);
if (data == null) {
return null;
}
List<String> rows = Arrays.asList(data.split("\\r\\n"));
return rows.stream().filter(StringUtils::isNotEmpty)
.limit(limit)
.collect(Collectors.toList());
}
// 從文件數(shù)據(jù)流中讀取字節(jié)流
private byte[] readByteStream(FSDataInputStream fileStream) {
byte[] bytes = new byte[1024*30];
int len;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
while ((len = fileStream.read(bytes)) != -1) {
stream.write(bytes, 0, len);
}
} catch (IOException e) {
logger.error("read file bytes stream failed.", e);
return null;
}
return stream.toByteArray();
}
// 解碼字節(jié)流
private String decodeByteStream(byte[] bytes) {
if (bytes == null) {
return null;
}
String encoding = guessEncoding(bytes);
String data = null;
try {
data = new String(bytes, encoding);
} catch (Exception e) {
logger.error("decode byte stream failed.", e);
}
return data;
}
// 根據(jù)Google的工具判別編碼
private String guessEncoding(byte[] bytes) {
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
String encoding = detector.getDetectedCharset();
detector.reset();
if (StringUtils.isEmpty(encoding)) {
encoding = "UTF-8";
}
return encoding;
}
以上就是JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案的詳細(xì)內(nèi)容,更多關(guān)于JAVA讀取HDFS的文件亂碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java算法實(shí)戰(zhàn)之排一億個(gè)隨機(jī)數(shù)
我們?cè)谏钪薪?jīng)常遇見(jiàn)一些這樣的需求,隨機(jī)點(diǎn)名、公司年會(huì)抽獎(jiǎng)、微信拼手氣紅包等,還有一些游戲比如打地鼠小游戲、俄羅斯方塊等,這些場(chǎng)景中都會(huì)用到一種算法:隨機(jī),這篇文章主要給大家介紹了關(guān)于Java算法實(shí)戰(zhàn)之排一億個(gè)隨機(jī)數(shù)的相關(guān)資料,需要的朋友可以參考下2021-11-11
Springboot啟動(dòng)執(zhí)行特定代碼的方式匯總
這篇文章主要介紹了Springboot啟動(dòng)執(zhí)行特定代碼的幾種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java使用TarsosDSP庫(kù)實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換
在音頻處理領(lǐng)域,Java雖然有原生的音頻處理類庫(kù),但其功能相對(duì)基礎(chǔ),而TarsosDSP是一個(gè)強(qiáng)大的開(kāi)源音頻處理庫(kù),提供了豐富的功能,本文將介紹如何在Java中結(jié)合使用TarsosDSP庫(kù),來(lái)實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換,需要的朋友可以參考下2025-04-04
Java使用Collections.sort()排序的示例詳解
這篇文章主要介紹了Java使用Collections.sort()排序的示例詳解,Collections.sort(list, new PriceComparator());的第二個(gè)參數(shù)返回一個(gè)int型的值,就相當(dāng)于一個(gè)標(biāo)志,告訴sort方法按什么順序來(lái)對(duì)list進(jìn)行排序。對(duì)此感興趣的可以了解一下2020-07-07
Java中String類getBytes()方法詳解與完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Java中String類getBytes()方法詳解與完整實(shí)例的相關(guān)資料,getBytes()是Java編程語(yǔ)言中將一個(gè)字符串轉(zhuǎn)化為一個(gè)字節(jié)數(shù)組byte[]的方法,需要的朋友可以參考下2023-10-10
java面向?qū)ο缶幊讨匾拍罾^承和多態(tài)示例解析
這篇文章主要為大家介紹了java面向?qū)ο缶幊痰膬蓚€(gè)重要概念繼承和多態(tài)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java中繼承thread類與實(shí)現(xiàn)Runnable接口的比較
這篇文章主要介紹了Java中繼承thread類與實(shí)現(xiàn)Runnable接口的比較的相關(guān)資料,需要的朋友可以參考下2017-06-06

