如何将 Hadoop Path 对象转换为 Java File 对象 [英] How to convert a Hadoop Path object into a Java File object

查看:49
本文介绍了如何将 Hadoop Path 对象转换为 Java File 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将有效且现有的 Hadoop 路径对象更改为有用的 Java 文件对象.有没有一种很好的方法来做到这一点,或者我是否需要大刀阔斧地提交代码?更明显的方法不起作用,它似乎是一个常见的代码

Is there a way to change a valid and existing Hadoop Path object into a useful Java File object. Is there a nice way of doing this or do I need to bludgeon to code into submission? The more obvious approaches don't work, and it seems like it would be a common bit of code

void func(Path p) {
  if (p.isAbsolute()) {
     File f = new File(p.toURI());
  }
}

这不起作用,因为 Path::toURI() 返回hdfs"标识符,而 Java 的 File(URI uri) 构造函数只识别file"标识符.

This doesn't work because Path::toURI() returns the "hdfs" identifier and Java's File(URI uri) constructor only recognizes the "file" identifier.

有没有办法让路径和文件一起工作?

Is there a way to get Path and File to work together?

**

好的,举个具体的有限例子怎么样.

Ok, how about a specific limited example.

Path[] paths = DistributedCache.getLocalCacheFiles(job);

DistributedCache 应该提供文件的本地化副本,但它返回一个路径.我假设 DistributedCache 制作文件的本地副本,它们位于同一磁盘上.鉴于这个有限的例子,其中 hdfs 希望不在等式中,有没有办法让我可靠地将路径转换为文件?

DistributedCache is supposed to provide a localized copy of a file, but it returns a Path. I assume that DistributedCache make a local copy of the file, where they are on the same disk. Given this limited example, where hdfs is hopefully not in the equation, is there a way for me to reliably convert a Path into a File?

**

推荐答案

我不知道.

据我所知,Hadoop 中的 Path 代表其分布式文件系统中节点的标识符.这是与 java.io.File 不同的抽象,后者代表本地文件系统上的一个节点.Path 甚至不太可能具有行为相同的 File 表示,因为底层模型根本不同.

To my understanding, a Path in Hadoop represents an identifier for a node in their distributed filesystem. This is a different abstraction from a java.io.File, which represents a node on the local filesystem. It's unlikely that a Path could even have a File representation that would behave equivalently, because the underlying models are fundamentally different.

因此缺乏翻译.根据您的断言,File 对象是[更多] 有用的",您是否需要此类的对象以便使用现有的库方法?由于上述原因,这不会很好地工作.如果它是您自己的库,您可以重写它以与 Hadoop Paths 一起工作,然后将任何 Files 转换为 Path 对象(此方向有效,因为 Paths 是文件的严格超集).如果它是第三方库,那么你就不走运了;该方法的作者没有考虑分布式文件系统的影响,只编写了该方法来处理普通的旧本地文件.

Hence the lack of translation. I presume by your assertion that File objects are "[more] useful", you want an object of this class in order to use existing library methods? For the reasons above, this isn't going to work very well. If it's your own library, you could rewrite it to work cleanly with Hadoop Paths and then convert any Files into Path objects (this direction works as Paths are a strict superset of Files). If it's a third party library then you're out of luck; the authors of that method didn't take into account the effects of a distributed filesystem and only wrote that method to work on plain old local files.

这篇关于如何将 Hadoop Path 对象转换为 Java File 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆