怎样在hdfs上创建多级目录文件夹_【HDFS API编程】第一个应用程序的开发-创建文件夹...

/**

* 使用Java API操作HDFS文件系统

* 关键点:

* 1)创建 Configuration

* 2)获取 FileSystem

* 3)...剩下的就是 HDFS API的操作了

*/

先上代码

1 public classHDFSApp {2 public static void main(String[] args) throwsException {3 Configuration configuration = newConfiguration();4

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

7 Path path = new Path(\"/hdfsapi/test\");8 boolean result =fileSystem.mkdirs(path);9 System.out.println(result);10 }11 }

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

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

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

1 /****************************************************************2 * An abstract base class for a fairly generic filesystem. It3 * may be implemented as a distributed filesystem, or as a \"local\"4 * one that reflects the locally-connected disk. The local version5 * exists for small Hadoop instances and for testing.6 *7 *

8 *9 * All user code that may potentially use the Hadoop Distributed10 * File System should be written to use a FileSystem object. The11 * Hadoop DFS is a multi-machine system that appears as a single12 * disk. It\'s useful because of its fault tolerance and potentially13 * very large capacity.14 *15 *

16 * The local implementation is {@linkLocalFileSystem} and distributed17 * implementation is DistributedFileSystem.18 *****************************************************************/

19 /**********************************************20

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

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

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

75550a49925038b6fb2089655dc39350.png

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

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

1 /**

2 * Returns the configured filesystem implementation.3 *@paramconf the configuration to use4 */

5 /**

6 *返回配置的文件系统实现。7 *@参数conf 要使用的配置8 */

9 public static FileSystem get(Configuration conf) throwsIOException {10 returnget(getDefaultUri(conf), conf);11 }

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

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

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

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

1 /**

2 * Get a filesystem instance based on the uri, the passed3 * configuration and the user4 *@paramuri of the filesystem5 *@paramconf the configuration to use6 *@paramuser to perform the get as7 *@returnthe filesystem instance8 *@throwsIOException9 *@throwsInterruptedException10 */

11 /**

12 *获取基于URI的文件系统实例,配置和用户13 *@参数uri 文件系统的uri14 *@参数conf 要使用的配置15 *@参数user 要执行get as的用户16 *@返回文件系统实例17 *@引发IOException18 *@引发InterruptedException19 */

20 public static FileSystem get(final URI uri, finalConfiguration conf,21 final String user) throwsIOException, InterruptedException {22 String ticketCachePath =

23 conf.get(CommonConfigurationKeys.KERBEROS_TICKET_CACHE_PATH);24 UserGroupInformation ugi =

25 UserGroupInformation.getBestUGI(ticketCachePath, user);26 return ugi.doAs(new PrivilegedExceptionAction() {27 @Override28 public FileSystem run() throwsIOException {29 returnget(uri, conf);30 }31 });32 }

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

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

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

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

1 /**

2 * Call {@link#mkdirs(Path, FsPermission)} with default permission.3 */

4 public boolean mkdirs(Path f) throwsIOException {5 returnmkdirs(f, FsPermission.getDirDefault());6 }

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

Path path = new Path(\"/hdfsapi/test\");//创建一个Path

boolean result =fileSystem.mkdirs(path); System.out.println(result);//输出result查看是否创建成功

此时我们可以通过终端控制台进行查看文件夹是否创建成功:

5bba8cafbc93031ac5afeae92c8f428e.png

465c2b63aff9ed68d4c30433d56a011e.png

当然也可以通过浏览器查看是否创建成功:输入 地址IP:50070 回车, 例如 192.168.42.110:50070 回车,

进去点击Utilities下拉列表的Browse the fiel system,点Go!就能看到:

0632cd2b7b07a843d432bd8eaa82ff85.png

点进去hdfsapi:

fb3aa72a08c77379119752cd67b2db8f.png

操作成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值