更新时间:2022-09-08 GMT+08:00
分享

Java API

HDFS完整和详细的接口可以直接参考官方网站描述:

http://hadoop.apache.org/docs/r3.1.1/api/index.html

HDFS常用接口

HDFS常用的Java类有以下几个:

  • FileSystem:是客户端应用的核心类。常用接口参见表1
  • FileStatus:记录文件和目录的状态信息。常用接口参见表2
  • DFSColocationAdmin:管理colocation组信息的接口。常用接口参见表3
  • DFSColocationClient:操作colocation文件的接口。常用接口参见表4
    • 系统中不保留文件与LocatorId的映射关系,只保留节点与LocatorId的映射关系。当文件使用Colocation接口创建时,系统会将文件创建在LocatorId所对应的节点上。文件创建和写入要求使用Colocation相关接口。
    • 文件写入完毕后,后续对该文件的相关操作不限制使用Colocation接口,也可以使用开源接口进行操作。
    • DFSColocationClient类继承于开源的DistributedFileSystem类,包含其常用接口。建议使用DFSColocationClient进行Colocation相关文件操作。
表1 类FileSystem常用接口说明

接口

说明

public static FileSystem get(Configuration conf)

Hadoop类库中最终面向用户提供的接口类是FileSystem,该类是个抽象类,只能通过该类的get方法得到具体类。get方法存在几个重载版本,常用的是这个。

public FSDataOutputStream create(Path f)

通过该接口可在HDFS上创建文件,其中f为文件的完整路径。

public void copyFromLocalFile(Path src, Path dst)

通过该接口可将本地文件上传到HDFS的指定位置上,其中src和dst均为文件的完整路径。

public boolean mkdirs(Path f)

通过该接口可在HDFS上创建文件夹,其中f为文件夹的完整路径。

public abstract boolean rename(Path src, Path dst)

通过该接口可为指定的HDFS文件重命名,其中src和dst均为文件的完整路径。

public abstract boolean delete(Path f, boolean recursive)

通过该接口可删除指定的HDFS文件,其中f为需要删除文件的完整路径,recuresive用来确定是否进行递归删除。

public boolean exists(Path f)

通过该接口可查看指定HDFS文件是否存在,其中f为文件的完整路径。

public FileStatus getFileStatus(Path f)

通过该接口可以获取文件或目录的FileStatus对象,该对象记录着该文件或目录的各种状态信息,其中包括修改时间、文件目录等等。

public BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len)

通过该接口可查找指定文件在HDFS集群上块的位置,其中file为文件的完整路径,start和len来标识查找文件的块的范围。

public FSDataInputStream open(Path f)

通过该接口可以打开HDFS上指定文件的输出流,并可通过FSDataInputStream类提供接口进行文件的读出,其中f为文件的完整路径。

public FSDataOutputStream create(Path f, boolean overwrite)

通过该接口可以在HDFS上创建指定文件的输入流,并可通过FSDataOutputStream类提供的接口进行文件的写入,其中f为文件的完整路径,overwrite为true时表示如果文件已经存在,则重写文件;如果为false,当文件已经存在时,则抛出异常。

public FSDataOutputStream append(Path f)

通过该接口可以打开HDFS上已经存在的指定文件的输入流,并可通过FSDataOutputStream类提供的接口进行文件的写入,其中f为文件的完整路径。

表2 类FileStatus常用接口说明

接口

说明

public long getModificationTime()

通过该接口可查看指定HDFS文件的修改时间。

public Path getPath()

通过该接口可查看指定HDFS中某个目录下所有文件。

表3 类DFSColocationAdmin常用接口说明

接口

说明

public Map<String, List<DatanodeInfo>> createColocationGroup(String groupId,String file)

根据文件file中的locatorIds信息,创建group。file为文件路径。

public Map<String, List<DatanodeInfo>> createColocationGroup(String groupId,List<String> locators)

使用内存中List的locatorIds信息,创建group。

public void deleteColocationGroup(String groupId)

删除group。

public List<String> listColocationGroups()

返回colocation所有组信息,返回的组Id数组按创建时间排序。

public List<DatanodeInfo> getNodesForLocator(String groupId, String locatorId)

获取该locator中所有节点列表。

表4 类DFSColocationClient常用接口说明

接口

说明

public FSDataOutputStream create(Path f, boolean overwrite, String groupId,String locatorId)

用colocation模式,创建一个FSDataOutputStream,从而允许用户在f路径写文件。

f为HDFS路径。

overwrite表示如果文件已存在是否允许覆盖。

用户指定文件所属的groupId和locatorId必须已经存在。

public FSDataOutputStream create(final Path f, final FsPermission permission, final EnumSet<CreateFlag> cflags, final int bufferSize, final short replication, final long blockSize, final Progressable progress, final ChecksumOpt checksumOpt, final String groupId, final String locatorId)

功能与FSDataOutputStream create(Path f, boolean overwrite, String groupId,String locatorId)相同,只是允许用户自定义checksum选项。

public void close()

使用完毕后关闭连接。

表5 HDFS客户端WebHdfsFileSystem接口说明

接口

说明

public RemoteIterator<FileStatus> listStatusIterator(final Path)

该API有助于通过使用远程迭代的多个请求获取子文件和文件夹信息,从而避免在获取大量子文件和文件夹信息时,用户界面变慢。

基于API的Glob路径模式以获取LocatedFileStatus和从FileStatus打开文件

在DistributedFileSystem中添加了以下API,以获取具有块位置的FileStatus,并从FileStatus对象打开文件。这些API将减少从客户端到Namenode的RPC调用的数量。

表6 FileSystem API接口说明

Interface接口

Description说明

public LocatedFileStatus[] globLocatedStatus(Path, PathFilter, boolean) throws IOException

返回一个LocatedFileStatus对象数组,其对应文件路径符合路径过滤规则。

public FSDataInputStream open(FileStatus stat) throws IOException

如果stat对象是LocatedFileStatusHdfs的实例,该实例已具有位置信息,则直接创建InputStream而不联系Namenode。

分享:

    相关文档

    相关产品