【发布时间】:2021-01-21 13:13:57
【问题描述】:
我正在尝试将 HDFS 文件从 UTF-8 转换为 ISO-8859-1。
我写了一个小的 Java 程序:
String theInputFileName="my-utf8-input-file.csv";
String theOutputFileName="my-iso8859-output-file.csv";
Charset inputCharset = StandardCharsets.UTF_8;
Charset outputCharset = StandardCharsets.ISO_8859_1;
try (
final FSDataInputStream in = theFileSystem.open(new Path(theInputFileName)) ;
final FSDataOutputStream out = theFileSystem.create(new Path(theOutputFileName))
)
{
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in, inputCharset)))
{
String line;
while ((line = reader.readLine()) != null)
{
out.write(line.getBytes(this.outputCharset));
out.write(this.lineSeparator.getBytes(this.outputCharset));
}
}
} catch (IllegalArgumentException | IOException e)
{
RddFileWriter.LOGGER.error(e, "Exception on file '%s'", theFileNameOutput);
}
这段代码使用Spark通过Hadoop集群执行(输出数据通常由RDD提供)
为了简化我的问题,我删除了 RDD/Datasets 部分以直接在 HDFS 文件上工作。
当我执行代码时:
-
Localy 在我的 DEV 计算机上:有效!,本地输出文件编码为
ISO-8859-1 - 在 EDGE 服务r 上:通过使用 HDFS 文件的 spark-submit 命令它可以工作! HDFS 输出文件编码为
ISO-8859-1 - 在 Datanode via oozie 上:它不起作用 :-( :HDFS 输出文件编码为
UTF-8而不是ISO-8859-1
我不明白哪些属性(或其他)可能导致行为变化
版本:
- Hadoop:v2.7.3
- Spark : v2.2.0
- Java:1.8
期待您的帮助。 提前致谢
【问题讨论】:
标签: java apache-spark hadoop encoding hdfs