Java 使用Hadoop在datanode上写入临时文件时遇到问题

Java 使用Hadoop在datanode上写入临时文件时遇到问题,java,hadoop,hdfs,yarn,hadoop2,Java,Hadoop,Hdfs,Yarn,Hadoop2,我想在我的程序中创建一个文件。但是,我不希望将此文件写入HDFS,而是写入执行map操作的datanode文件系统 我尝试了以下方法: public void map(Object key, Text value, Context context) throws IOException, InterruptedException { // do some hadoop stuff, like counting words String path = "newFil

我想在我的程序中创建一个文件。但是,我不希望将此文件写入HDFS,而是写入执行
map
操作的datanode文件系统

我尝试了以下方法:

public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
    // do some hadoop stuff, like counting words
    String path = "newFile.txt";
    try {
        File f = new File(path);
        f.createNewFile();
    } catch (IOException e) {
        System.out.println("Message easy to look up in the logs.");
        System.err.println("Error easy to look up in the logs.");
        e.printStackTrace();
        throw e;
    }
}
通过一个绝对路径,我得到文件应该在哪里。但是,对于相对路径,无论是在运行程序的控制台中还是在作业日志中,此代码都不会产生任何错误。但是,我无法找到应该创建的文件(现在,我正在处理本地集群)


知道在哪里可以找到文件或错误消息吗?如果确实有错误消息,我应该如何继续将文件写入datanodes的本地文件系统?

newFile.txt是一个相对路径,因此该文件将相对于映射任务进程的工作目录显示。这将落在NodeManager用于容器的目录下。这是warn-site.xml中的配置属性
warn.nodemanager.local dirs
,或从/tmp下的warn-default.xml继承的默认值:

<property>
  <description>List of directories to store localized files in. An 
    application's localized file directory will be found in:
    ${yarn.nodemanager.local-dirs}/usercache/${user}/appcache/application_${appid}.
    Individual containers' work directories, called container_${contid}, will
    be subdirectories of this.
  </description>
  <name>yarn.nodemanager.local-dirs</name>
  <value>${hadoop.tmp.dir}/nm-local-dir</value>
</property>
这些目录是容器执行的暂存空间,所以它们不是持久性所依赖的。后台线程定期删除已完成容器的这些文件。可以通过在warn-site.xml中设置配置属性
warn.nodemanager.delete.debug delay sec
来延迟清理:

<property>
  <description>
    Number of seconds after an application finishes before the nodemanager's 
    DeletionService will delete the application's localized file directory
    and log directory.

    To diagnose Yarn application problems, set this property's value large
    enough (for example, to 600 = 10 minutes) to permit examination of these
    directories. After changing the property's value, you must restart the 
    nodemanager in order for it to have an effect.

    The roots of Yarn applications' work directories is configurable with
    the yarn.nodemanager.local-dirs property (see below), and the roots
    of the Yarn applications' log directories is configurable with the 
    yarn.nodemanager.log-dirs property (see also below).
  </description>
  <name>yarn.nodemanager.delete.debug-delay-sec</name>
  <value>0</value>
</property>

应用程序完成后的秒数,节点管理器
DeletionService将删除应用程序的本地化文件目录
和日志目录。
若要诊断纱线应用程序问题,请将此属性的值设置为大
足够(例如,600=10分钟)允许检查这些
目录。更改属性值后,必须重新启动
节点管理器,以使其生效。
纱线应用程序工作目录的根可以通过
纱线.nodemanager.local-dirs属性(见下文)和根
纱线应用程序的日志目录的
纱线.nodemanager.log-dirs属性(另请参见下文)。
warn.nodemanager.delete.debug-delay-sec
0
但是,请记住,此配置仅用于解决问题,以便您可以更轻松地查看目录。不建议将其作为永久性生产配置。如果应用程序逻辑依赖于删除延迟,那么这可能会导致尝试访问目录的应用程序逻辑与尝试删除目录的节点管理器之间出现竞争条件。保留旧容器执行中的文件也有可能使本地磁盘空间混乱

日志消息将转到映射任务日志的stdout/stderr,但我怀疑执行没有命中这些日志消息。相反,我怀疑您正在成功地创建该文件,但要么它不容易查找(目录结构将有一些不可预测的东西,如由Thread管理的应用程序ID和容器ID),要么在您访问该文件之前,该文件已被清理


如果您将代码更改为使用指向其他目录的绝对路径,那么这将有所帮助。然而,我并不期望这种方法在实际操作中很好地工作。由于Hadoop是分布式的,您可能很难找到成百上千个集群中的哪个节点包含您想要的本地文件。相反,您最好先写入HDFS,然后将本地需要的文件拉到启动作业的节点。

Hadoop是分布式的,因此假设您在500节点集群上运行此操作,namenode会在某个datanode上随机运行。因此,我更喜欢使用namenode或edgenode作为创建file@KSNidhin我知道这一点,但这里的目标是创建临时文件,从外部工具读取它们,然后删除它们。感谢您的回答,我将立即检查我的配置文件。FWIW,这里的目标是将一些数据写入文件,使该文件被其他应用程序解析并恢复结果(所有操作都在相同的
map
操作中,因此不需要持久性(无论如何不会持续太久)。我找到了该目录,但据我所知,它会在作业完成后立即被清理。您知道如何禁用此清理吗?有一个名为
Thread.nodemanager.delete.debug delay sec
的配置属性可以控制清理。我编辑了我的答案,以包含对此属性的进一步讨论。请注意警告:这只建议用于故障排除,而不是在生产中经常使用。
<property>
  <description>
    Number of seconds after an application finishes before the nodemanager's 
    DeletionService will delete the application's localized file directory
    and log directory.

    To diagnose Yarn application problems, set this property's value large
    enough (for example, to 600 = 10 minutes) to permit examination of these
    directories. After changing the property's value, you must restart the 
    nodemanager in order for it to have an effect.

    The roots of Yarn applications' work directories is configurable with
    the yarn.nodemanager.local-dirs property (see below), and the roots
    of the Yarn applications' log directories is configurable with the 
    yarn.nodemanager.log-dirs property (see also below).
  </description>
  <name>yarn.nodemanager.delete.debug-delay-sec</name>
  <value>0</value>
</property>