【发布时间】:2022-01-05 17:07:04
【问题描述】:
我需要将 csv 文件解析为 xml 并将其写入 hdfs。我成功地完成了第一部分,但在写作时出错。这是代码。
private static void writeToXml(String inputPath, String outputPath) throws IOException, JSchException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://nn");
FileSystem fileSystem = FileSystem.get(configuration);
Path iPath = new Path(inputPath);
Path oPath = new Path(outputPath);
FSDataInputStream inputStream = fileSystem.open(iPath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet");
AtomicReference<Integer> row = new AtomicReference<>(0);
try (Stream<String> stream = bufferedReader.lines()) {
stream.forEach(line -> {
Row currentRow = sheet.createRow(row.getAndSet(row.get() + 1));
String[] nextLine = line.split(";");
Stream.iterate(0, i -> i + 1).limit(nextLine.length).forEach(i -> {
currentRow.createCell(i).setCellValue(nextLine[i]);
});
});
FSDataOutputStream outputStream = fileSystem.create(oPath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
outputStream.write(out.toByteArray());
outputStream.flush();
}
}
因为这个错误而失败。
org.apache.oozie.action.hadoop.JavaMainException: java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.byteArray(I)[B
Caused by: java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.byteArray(I)[B
at org.apache.commons.io.output.AbstractByteArrayOutputStream.needNewBuffer(AbstractByteArrayOutputStream.java:104)
at org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.<init>(UnsynchronizedByteArrayOutputStream.java:51)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.syncWithDataSource(POIFSFileSystem.java:779)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.writeFilesystem(POIFSFileSystem.java:756)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1387)
at path.to.package.Main.writeToXml(Main.java:81)
at path.to.package.Main.main(Main.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.oozie.action.hadoop.JavaMain.run(JavaMain.java:57)
... 15 more
在这一行。
workbook.write(out);
编辑我尝试编写的另一个片段。失败并出现同样的错误。
FSDataOutputStream outputStream = fileSystem.create(oPath);
workbook.write(outputStream);
outputStream.flush();
我做错了什么?
【问题讨论】:
-
您的类路径上是否有旧版本的 Commons IO?
-
@Gagravarr 我正在使用 commons-io 版本 2.11.0。这是最后一个版本。
-
hadoop 本身可能依赖于旧版本的 commons-io
-
问题描述中的堆栈跟踪包含 poi 和 commons-io 的未着色包名称
标签: java apache-poi hdfs