【HDFS API编程】第一个应用程序的开发-创建文件夹
/**
* 使用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:
操作成功。
- WinCE应用程序开发---创建文件或文件夹
- VS2010/MFC编程入门之一(Ribbon界面开发:创建Ribbon样式的应用程序框架)
- ios开发笔记 (1) 创建第一个ios 应用程序
- flex开发(1)---创建您的第一个应用程序
- wp7开发之创建第一个Silverlight应用程序
- wp7开发之创建第一个Silverlight应用程序
- 一行代码不用敲,15分钟轻松搞定GIS开发,GISer福音,教你如何从下载到使用webappbuilder创建第一个应用程序(不用申请有使用期限的企业账号哦!)
- VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)
- IOS 入门开发之创建第一个应用程序
- 菜鸟学Windows Phone 8开发(1)——创建第一个应用程序
- VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)
- 【转载】IOS 入门开发之创建第一个应用程序(一)
- 菜鸟学Windows Phone 8开发(1)——创建第一个应用程序
- Java开发环境的安装和第一个应用程序的创建
- Android开发之 Windows环境下通过Eclipse创建的第一个安卓应用程序(图文详细步骤)
- wp7开发之创建第一个Silverlight应用程序
- (转载)VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)
- C#编程基础(下)第二课:创建Windows应用程序的方法,窗体的基本属性和方法、基本控件的使用
- Linux下应用程序开发:QT国际化编程 -- 你的Linux技术站,技术支持站
- PHP开发API 应用程序接口