首页 > 解决方案 > 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 位置创建空文件。这是我尝试过的一些物品

  1. 客户端和hadoop服务器之间没有防火墙
  2. 从本地复制到 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)

标签: javahadoophdfs

解决方案


我能够通过

conf.set("dfs.client.use.datanode.hostname", "true");


推荐阅读