Java API


HDFS完整和详细的接口可以直接参考官方网站上的描述:http://hadoop.apache.org/docs/r2.7.2/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有助于通过使用远程迭代的多个请求获取子文件和文件夹信息,从而避免在获取大量子文件和文件夹信息时,用户界面变慢。 |

      SmallFS常用接口

      SmallFS的Java类SmallFileSystem常用接口如表6所示。

      表6 类SmallFileSystem常用接口说明

      | 接口 | 说明 | | :--- | :--- | | public void close() | 使用完毕后关闭连接。 | | public void copyFromLocalFile(boolean delSrc, boolean overwrite, Path src,Path dst) throws IOException | 通过该接口可将本地文件上传到SmallFileSystem的指定位置上,其中src和dst均为文件的完整路径。 | | public void copyFromLocalFile(boolean delSrc, boolean overwrite, Path[] srcs, Path dst) throws IOException | 通过该接口可将多个本地文件上传到SmallFileSystem的指定位置上,其中srcs和dst均为文件的完整路径。 | | public void copyToLocalFile(boolean delSrc, Path src, Path dst, boolean useRawLocalFileSystem) throws IOException | 通过该接口可将SmallFileSystem的指定文件下载到本地文件,其中src和dst均为文件的完整路径。 | | public FSDataOutputStream create(Path path, FsPermission permission,boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException | 通过该接口可以在指定路径path下创建文件。 | | public FileSystem[] getChildFileSystems() | 通过该接口可得到SmallFileSystem的所有子文件系统。 | | public long getDefaultBlockSize() | 通过该接口可得到SmallFileSystem的默认Block大小。 | | public short getDefaultReplication() | 通过该接口可得到SmallFileSystem的默认备份数。 | | public BlockLocation[] getFileBlockLocations(Path path, long start, long len) throws IOException | 通过该接口可以得到指定文件path的Block位置。 | | public Path getHomeDirectory() | 通过该接口可以得到原始路径。 | | public String getScheme() | 通过该接口可以得到SmallFileSystem的Schema。 | | public FsServerDefaults getServerDefaults() throws IOException | 通过该接口可以得到SmallFileSystem的默认配置。 | | public void initialize(URI name, Configuration conf) throws IOException | 通过该接口可以进行SmallFileSystem初始化。 | | public void setOwner(Path path, String username, String groupname) throws IOException | 通过该接口可以对指定path(文件或者路径)设置owner。该参数对应的用户名和组名称不能为空。说明:已合并文件不支持该接口。 | | public void setPermission(Path p, FsPermission permission) throws IOException | 通过该接口可以对指定path(文件或者路径)设置文件权限。说明:已合并文件不支持。 | | public boolean setReplication(Path path, short replication) throws IOException | 通过该接口可以对指定path(文件或者路径)设置备份数。说明:已合并文件不支持。 | | public void setTimes(Path path, long mtime, long atime) throws IOException | 通过该接口可以对指定path(文件或者路径)设置修改时间和access时间。说明:已合并文件不支持。 | | public boolean delete(Path path, boolean recursive) throws IOException | 通过该接口可以删除SmallFileSystem上指定文件path(文件或者路径)。 | | public FileStatus getFileStatus(Path path) throws IOException | 通过该接口可以获取该文件系统中指定分区的FsStatus对象,该对象记录着该分区的总容量、使用容量、剩余容量。 | | public URI getUri() | 获取SmallFileSystem文件系统的默认URI。 | | public Path getWorkingDirectory() | 通过该接口可以获取SmallFileSystem当前工作目录。 | | public FileStatus[] listStatus(Path path) throws IOException | 如果指定的路径为一个目录,则通过该接口可以罗列该目录下的文件及目录状态。 | | public boolean mkdirs(Path path, FsPermission permission) throws IOException | 通过该接口可以创建指定路径的文件或者文件夹。 | | public FSDataInputStream open(Path path, int bufferSize) throws IOException | 通过该接口可以打开SmallFileSystem上指定文件的输出流,并可通过FSDataInputStream类提供接口进行文件的读出,其中path为文件的完整路径。 | | public boolean rename(Path src, Path dst) throws IOException | 通过该接口可为指定的SmallFileSystem文件重命名,其中src和dst均为文件的完整路径。说明:已合并文件不支持。 | | public void setWorkingDirectory(Path path) | 在SmallFileSystem中,重写了该方法,使其不支持设置成其他工作目录。 | | public Configuration getConf() | 通过该接口可以得到SmallFileSystem的配置。 | | public Path getInitialWorkingDirectory() | 通过该接口可以得到SmallFileSystem的初始运行目录。 | | public FsStatus getStatus(Path path) throws IOException | 通过该接口可以获取文件或目录的FsStatus对象,该对象记录着该文件或目录的各种状态信息,其中包括修改时间、文件目录等等。 | | public FSDataOutputStream createNonRecursive(Path path, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException | 通过该接口可以在指定路径path下创建文件,如果父目录不存在,将创建失败。说明:该接口目前已不建议使用,请使用create接口。 | | public FSDataOutputStream append(Path path) throws IOException | 通过该接口可以对SmallFileSystem上的指定文件path进行追加内容。说明:已合并文件不支持。 | | public boolean truncate(Path path, long newLength) throws IOException | 通过该接口可以对SmallFileSystem上的指定文件path进行内容裁剪。说明:已合并文件不支持。 | | public FsServerDefaults getServerDefaults(Path path) throws IOException | 通过该接口可以获取指定路径的目标文件系统的FsServerDefaults对象,该对象记录着该文件系统的一些配置信息,例如:块大小、备份数、垃圾保留时间等。 | | public long getUsed() throws IOException | 通过该接口可以获得filesystem中所有文件的大小总和。 | | public long getDefaultBlockSize(Path path) | 通过该接口可以得到指定文件path的默认Block大小。 | | public short getDefaultReplication(Path path) | 通过该接口可以得到指定文件path的默认备份数。 |

results matching ""

    No results matching ""