本次开发使用java 处理csv 文件上传到服务器本地,再由服务器本地经过Hdfs 导入Hive 表中,步骤如下:
一、CsvEncodingConverter方法
package com.ys.common.hadoop;
import java.io.*;
import java.nio.charset.StandardCharsets;
import org.mozilla.intl.chardet.nsDetector;
import org.springframework.web.multipart.MultipartFile;
/**
* CsvEncodingConverter 类用于检测和转换 CSV 文件的编码格式。
*/
public class CsvEncodingConverter {
/**
* 检测给定 MultipartFile 中 CSV 文件的编码格式。
*
* @param file 要检测的 CSV 文件
* @return CSV 文件的编码格式,如果无法确定,则返回 null
* @throws RuntimeException 如果在检测文件编码时发生错误
*/
public static String findEncoding(MultipartFile file) {
try {
return guessFileEncoding(file);
} catch (IOException e) {
throw new RuntimeException("Error while detecting file encoding", e);
}
}
/**
* 通过检测 MultipartFile 中的 CSV 文件来猜测其编码格式。
*
* @param file 要检测的 CSV 文件
* @return CSV 文件的编码格式,如果无法确定,则返回 null
* @throws IOException 如果在检测文件编码时发生错误
*/
private static String guessFileEncoding(MultipartFile file) throws IOException {
try (InputStream inputStream = file.getInputStream()) {
nsDetector detector = new nsDetector();
byte[] buf = new byte[1024];
int len;
boolean isAscii = false;
while ((len = inputStream.read(buf, 0, buf.length)) != -1) {
isAscii = detector.isAscii(buf, len);
if (isAscii) {
return "ASCII";
}
detector.DoIt(buf, len, false);
}
detector.DataEnd();
String[] probableCharsets = detector.getProbableCharsets();
if (probableCharsets.length > 0 && !"nomatch".equals(probableCharsets[0])) {
return probableCharsets[0];
} else {
// 如果无法识别编码,返回默认编码 gb18030
return "gb18030";
}
} catch (IOException e) {
throw new IOException("检测文件编码时出错", e);
}
}
/**
* 将给定 MultipartFile 中的 CSV 文件转换为 UTF-8 编码,并保存到指定路径。
*
* @param file 要转换的 CSV 文件
* @param sourceEncoding 原文件的编码格式
* @param newFilePath 转换后的文件保存路径
* @throws RuntimeException 如果在转换和保存文件时发生错误
*/
public static void convertAndSaveToFile(MultipartFile file, String sourceEncoding, String newFilePath) {
try (InputStream inputStream = file.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, sourceEncoding));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFilePath), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
// 处理不支持的字符集异常,可以选择使用默认字符集或其他支持的字符集
e.printStackTrace();
}
}
}
二、方法调用
// 检测文件编码
String currentEncoding = CsvEncodingConverter.findEncoding(file);
logger.warn("currentEncoding:" + currentEncoding);
// 上传文件到本地服务器
CsvEncodingConverter.convertAndSaveToFile(file, currentEncoding, uploadFilePath);
参考网址:https://blog.csdn.net/qq_33505051/article/details/86736039
先检查csv文件的编码格式,如果获取失败,则默认读取文件编码格式为:gb18030b,再转化为UTF-8 编码格式,关于其他导入到Hive代码,有问题欢迎留言讨论。