> 文章列表 > JavaClient With HDFS

JavaClient With HDFS

JavaClient With HDFS

序言

在使用Java创建连接HDFS的客户端时,可以设置很多参数,具体有哪些参数呢,只要是在部署HDFS服务中可以设置的参数,都是可以在连接的时候设置. 我没有去验证所有的配置是否都可以验证,只是推测cuiyaonan2000@163.com

依据 

创建HDFS的构造函数如下所示:

 网上比较常用的是get() 方式.,  同时还提供了3种,无论哪种 Configuration都是必须要填写的,

根据它们的实现其实只传递一个Configuration就可以了,像HDFS的路径URI 和操作用户的Name 都可以放置在Configuration中

首先要知道

在搭建HDFS服务中使用dfs.replication来配置 每个block的备份数量.

这里主要是想说的是想上传文件的备份数量其实也是可以设置放进Configuration中的cuiyaonan2000@163.com. 我们打个断点查看创建后的Configuration中的参数就可以明白.

首先我们看updatingResource是我们在初始化Configuration中会初始化的Map.里面记录了HDFS的配置信息默认从哪里去取 如果我们没有设置的话.

 但是当我们使用configuration.set(key,value) 修改过后,则不会显示源文件的名称,而是显示programatically 表示连接程序做了修改,以连接程序的设置为主,不再从服务器上的文件中读取cuiyaonan2000@163.com

比如我们修改了fs.defaultFS的值,则变为如下所示:

 原来是

那当我们如果设置了fs.defaultFS则会在如下属性中体现.

如上的定义如下所示

private Properties properties;/*** Stores the mapping of key to the resource which modifies or loads * the key most recently*/
private Map<String, String[]> updatingResource;

UserGroupInformation

只有用户登录的设置有点区别,但是不耽误我们使用,具体实现可以了解如下的源码

  /*** 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*/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>() {@Overridepublic FileSystem run() throws IOException {return get(uri, conf);}});}

UserGroupInformation官网的API地址:https://hadoop.apache.org/docs/stable/api/org/apache/hadoop/security/UserGroupInformation.html