java - Java HDF fsDataOutputStream 写入失败创建空文件
问题描述
我在 hadoop 上写小文件时遇到了一个奇怪的问题。下面是示例程序
public void writeFile(Configuration conf, String message, String filename) throws Exception {
FSDataOutputStream fsDataOutputStream = null;
DistributedFileSystem fs = null;
try {
fs = (DistributedFileSystem) FileSystem.get(URI.create(properties.getHadoop().getRawLocation()), conf);
Path hdfswritepath = new Path(properties.getHadoop().getRawLocation() + "/" + filename + ".json");
fsDataOutputStream = fs.create(hdfswritepath);
fsDataOutputStream.write(message.getBytes());
fsDataOutputStream.close();
fsDataOutputStream.hsync();
} catch (IllegalArgumentException | IOException e) {
System.out.println("Got Exception");
e.printStackTrace();
throw e;
} finally {
fs.close();
System.out.println("clean up done");
}
}
上面的代码是在 hadoop 位置创建空文件。这是我尝试过的一些物品
- 客户端和hadoop服务器之间没有防火墙
- 从本地复制到 hadoop 正在工作。
问题是仅创建 0 字节文件。
为此,我得到了以下例外。
09:12:02,129 INFO [org.apache.hadoop.hdfs.DFSClient] (Thread-118) Exception in createBlockOutputStream: java.net.ConnectException: Connection timed out: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206)
at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:531)
at org.apache.hadoop.hdfs.DFSOutputStream.createSocketForPipeline(DFSOutputStream.java:1533)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.createBlockOutputStream(DFSOutputStream.java:1309)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.nextBlockOutputStream(DFSOutputStream.java:1262)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:448)
解决方案
我能够通过
conf.set("dfs.client.use.datanode.hostname", "true");
推荐阅读
- c++ - 实例化新类时未调用方法
- java - 使用 java API 编写 apache 箭头格式时是否存在大小限制/问题
- ios - signInWithPhoneNumber() 在 react-native 中不起作用
- android - 提供的手机格式不正确
- forms - 如何以 xamarin 形式反序列化视图模型中的对象
- laravel - 将帖子保存在数据库中后如何获取帖子的ID?
- javascript - 如何更改 Set 中项目的值?
- java - 如何在运行时启用/禁用 JMS 侦听器?我可以创建一个在运行时启用/禁用侦听器的 api 吗?
- ios - Firebase 聊天备份/导出,如 SWIFT 中的 whatsapp
- asp.net-mvc - 如何将分组的 Viewbag 绑定到 MVC 视图?