您的位置:首页 > 大数据 > Hadoop

【HDFS API编程】第一个应用程序的开发-创建文件夹

2019-04-19 17:42 706 查看

/**
 * 使用Java API操作HDFS文件系统
 * 关键点:
 * 1)创建 Configuration
 * 2)获取 FileSystem
 * 3)...剩下的就是 HDFS API的操作了
*/

先上代码

public class HDFSApp {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();

FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration,"hadoop");

Path path = new Path("/hdfsapi/test");
boolean result = fileSystem.mkdirs(path);
System.out.println(result);
}
}

对于方法的源码是我们在学习的时候必须的,我们可以按住Ctrl然后点击对应的方法类名进去进行源码的下载。

首先要对HDFS进行操作我们就必须获取FileSystem,

Ctrl点进FileSystem源码,我们能看到最顶部的注释介绍(附上了中文翻译):

 

/****************************************************************
* An abstract base class for a fairly generic filesystem.  It
* may be implemented as a distributed filesystem, or as a "local"
* one that reflects the locally-connected disk.  The local version
* exists for small Hadoop instances and for testing.
*
* <p>
*
* All user code that may potentially use the Hadoop Distributed
* File System should be written to use a FileSystem object.  The
* Hadoop DFS is a multi-machine system that appears as a single
* disk.  It's useful because of its fault tolerance and potentially
* very large capacity.
*
* <p>
* The local implementation is {@link LocalFileSystem} and distributed
* implementation is DistributedFileSystem.
*****************************************************************/
/**********************************************

*一个相当通用的文件系统的抽象基类。它可以实现为分布式文件系统,也可以实现为“本地”
*反映本地连接磁盘的磁盘。本地版本
*存在于小型Hadoop实例和测试中。
*所有可能使用Hadoop分布式的用户代码
*应该写入文件系统以使用文件系统对象。这个Hadoop DFS是一个多机系统,显示为单个磁盘。它是有用的,因为它的容错性和潜在的容量很大。
*< P>
*本地实现是@link localfilesystem和分布式的
*实现是分布式文件系统。
***********************************************/

 

 

 

写作的开门见山,第一句话能够概括全文中心---> 这是 一个相当通用的文件系统的抽象基类。,所以我们使用Java API操作HDFS文件系统需要获取FileSystem。

使用FileSystem的get方法获取fileSystem,FileSystem有三种get方法:

不知道怎么用?哪里不会Ctrl点哪里!

首先看只有一个参数的get方法:get(Configuration conf):

/**
* Returns the configured filesystem implementation.
* @param conf the configuration to use
*/
/**
*返回配置的文件系统实现。
*@参数conf 要使用的配置
*/
public static FileSystem get(Configuration conf) throws IOException {
return get(getDefaultUri(conf), conf);
}

从源码的介绍中可以知道 Configuration 是某种配置,具体是什么?Ctrl点一点:

Provides access to configuration parameters.//提供对配置参数的访问。

源码中configuration的注释介绍第一句已经介绍了这是对配置参数的访问,所以,使用Java API操作HDFS文件系统需要创建configuration:Configuration configuration = new Configuration();

跳过两个参数的get方法,我们来看包含三个参数的get方法,Ctrl 点进去:

 

/**
* Get a filesystem instance based on the uri, the passed
* configuration and the user
* @param uri of the filesystem
* @param conf the configuration to use
* @param user to perform the get as
* @return the filesystem instance
* @throws IOException
* @throws InterruptedException
*/
/**
*获取基于URI的文件系统实例,配置和用户
*@参数uri 文件系统的uri
*@参数conf 要使用的配置
*@参数user 要执行get as的用户
*@返回文件系统实例
*@引发IOException
*@引发InterruptedException
*/
public static FileSystem get(final URI uri, final Configuration conf,
final String user) throws IOException, InterruptedException {
String ticketCachePath =
conf.get(CommonConfigurationKeys.KERBEROS_TICKET_CACHE_PATH);
UserGroupInformation ugi =
UserGroupInformation.getBestUGI(ticketCachePath, user);
return ugi.doAs(new PrivilegedExceptionAction<FileSystem>() {
@Override
public FileSystem run() throws IOException {
return get(uri, conf);
}
});
}

 

重点看方法上面的注释,介绍了方法的功能、参数解释、返回值以及可能引发的异常。

我们要对HDFS操作,是不是需要知道我们要对哪个HDFS操作(URI),是不是要指定用什么样的配置参数进行操作(configuration),是不是需要确认是用什么用户进行操作(user),这样我们才能够准确地使用正确的用户以及正确的配置访问正确的HDFS。

此时我们拿到的fileSystem已经是我们需要的fileSystem了,那么剩下的操作就是对HDFS API的操作了,例如我们创建一个文件夹:

使用fileSystem的mkdirs方法,mkdirs方法不知道怎么用?Ctrl点!

 

 

/**
* Call {@link #mkdirs(Path, FsPermission)} with default permission.
*/
public boolean mkdirs(Path f) throws IOException {
return mkdirs(f, FsPermission.getDirDefault());
}

 

我们可以看到mkdir方法接收的参数是一个Path路径,返回值是boolean类型判断是否创建成功,所以我们可以写:

Path path = new Path("/hdfsapi/test");//创建一个Path
boolean result = fileSystem.mkdirs(path); System.out.println(result);//输出result查看是否创建成功
此时我们可以通过终端控制台进行查看文件夹是否创建成功:



当然也可以通过浏览器查看是否创建成功:输入 地址IP:50070 回车, 例如 192.168.42.110:50070 回车,
进去点击Utilities下拉列表的Browse the fiel system,点Go!就能看到:

点进去hdfsapi:

操作成功。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐