---
title: "Java程序中不通过hadoop jar的方式访问hdfs"
author: "fireinwind"
date: "2018-06-26T18:28:27Z"
tags: []
categories: ["开源软件"]
slug: "java-hadoop-jar-hdfs"
draft: false
lastmod: "2018-06-26T18:28:27Z"
id: 2425656
url: "https://fireinwind.iteye.com/blog/2425656"
---

转自http://brandnewuser.iteye.com/blog/2208709

 

### [Java程序中不通过hadoop jar的方式访问hdfs](http://brandnewuser.iteye.com/blog/2208709)

**博客分类：**

- [系统使用](http://brandnewuser.iteye.com/category/305126)
- [hadoop&&storm](http://brandnewuser.iteye.com/category/317258)
- [开源分析](http://brandnewuser.iteye.com/category/305125)

[hadoop](http://www.iteye.com/blogs/tag/hadoop)[hdfs](http://www.iteye.com/blogs/tag/hdfs)[hadoop jar](http://www.iteye.com/blogs/tag/hadoop%20jar) 

 

 

一般情况下，我们使用Java访问hadoop distributed file system(hdfs)使用hadoop的相应api，添加以下的pom.xml依赖（这里以hadoop2.2.0版本为例）：

 

Xml代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

01. &lt;dependency&gt;
02. &lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;
03. &lt;artifactId&gt;hadoop-common&lt;/artifactId&gt;
04. &lt;version&gt;2.2.0&lt;/version&gt;
05. &lt;/dependency&gt;
06. &lt;dependency&gt;
07. &lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;
08. &lt;artifactId&gt;hadoop-mapreduce-client-core&lt;/artifactId&gt;
09. &lt;version&gt;2.2.0&lt;/version&gt;
10. &lt;/dependency&gt;

  

 

在其中使用FileSystem就可以访问hdfs：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. FileSystem fileSystem = null;
2. try {
3. Configuration conf = new Configuration();
4. fileSystem = FileSystem.get(conf);
5. BufferedReader bufferedReader =  = new BufferedReader(new InputStreamReader(fsInputStream));

 

 

但是需要注意的一点就是，只能在配置了hadoop环境的服务器上，通过hadoop jar的方式启动，hadoop jar的方式启动实际上调用的是org.apache.hadoop.util。RunJar作为入口。

 

RunJar会自动将此环境中的hadoop classpath设置，并初始化hadoop的环境变量等信息。

 

但是这种方式只能够写一个shell脚本来实现调用，其中使用hadoop jar的方式来调用，但是如果我们想要在比较特殊的环境下启动java进程，譬如在web server下（例如Tomcat）调用获取hdfs的信息，则不能够成功；或比如通过java -jar/-cp的方式来进行调用，得到的FileSystem.get(conf)并不是我们想要的。比如如果在本机执行该方法，就会得到本机磁盘相关文件系统，只有通过hadoop jar的方式调用返回DistributedFileSystem。

 

FileSystem类中还有一个方法，可以根据相应的URI来得到对应的FileSystem：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

01. /\**
02. * Get a filesystem instance based on the uri, the passed
03. * configuration and the user
04. * @param uri of the filesystem
05. * @param conf the configuration to use
06. * @param user to perform the get as
07. * @return the filesystem instance
08. * @throws IOException
09. * @throws InterruptedException
10. \*/
11. public static FileSystem get(final URI uri, final Configuration conf,
12. final String user) throws IOException, InterruptedException {

 

 

从hadoop file system环境中查找对应hadoop defaultFS配置：

 

Xml代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. &lt;property&gt;
2. &lt;name&gt;fs.defaultFS&lt;/name&gt;
3. &lt;value&gt;hdfs://ns1&lt;/value&gt;

  

 

就会曝出以下的错误，提示缺少一个类定义：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

01. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/util/PlatformName
02. at org.apache.hadoop.security.UserGroupInformation.getOSLoginModuleName(UserGroupInformation.java:303)
03. at org.apache.hadoop.security.UserGroupInformation.&lt;clinit&gt;(UserGroupInformation.java:348)
04. at org.apache.hadoop.fs.FileSystem$Cache$Key.&lt;init&gt;(FileSystem.java:2590)
05. at org.apache.hadoop.fs.FileSystem$Cache$Key.&lt;init&gt;(FileSystem.java:2582)
06. at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2448)
07. at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:367)
08. at com.xxxx.HdfsMain.main(HdfsMain.java:21)
09. Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.PlatformName
10. at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
11. at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
12. at java.security.AccessController.doPrivileged(Native Method)
13. at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
14. at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
15. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
16. at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
17. ... 7 more

 

 

通过在网上查找的说法，此种情况下需要额外添加hadoop-auth的相关依赖：

 

Xml代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. &lt;dependency&gt;
2. &lt;groupId&gt;org.apache.hadoop&lt;/groupId&gt;
3. &lt;artifactId&gt;hadoop-auth&lt;/artifactId&gt;
4. &lt;version&gt;2.2.0&lt;/version&gt;
5. &lt;/dependency&gt;

 

 

但是jar包添加进去后仍然不能够奏效，

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
2. at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2421)
3. at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2428)
4. at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:88)
5. at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2467)
6. at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2449)
7. at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:367)

 

 

经过查看源代码，可以看出***SERVICE\_FILE\_SYSTEMS***中并没有存在hdfs中scheme：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

01. public static Class&lt;? extends FileSystem&gt; getFileSystemClass(String scheme,
02. Configuration conf) throws IOException {
03. if (!FILE\_SYSTEMS\_LOADED) {
04. loadFileSystems();
05. }
06. Class&lt;? extends FileSystem&gt; clazz = null;
07. if (conf != null) {
08. clazz = (Class&lt;? extends FileSystem&gt;) conf.getClass("fs." + scheme + ".impl", null);
09. }
10. if (clazz == null) {
11. clazz = SERVICE\_FILE\_SYSTEMS.get(scheme);
12. }
13. if (clazz == null) {
14. throw new IOException("No FileSystem for scheme: " + scheme);
15. }
16. return clazz;
17. }

 

 

可以手动采用Configuration设置fs.\[filesystemname].impl的方式，手动将DistributedFileSystem注册进去：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. conf.set("fs.hdfs.impl", DistributedFileSystem.class.getName());

 

 

这样启动后发现仍然有坑，因为ns1并不是真实的网络地址，地址配置在hdfs-site.xml文件中，我们的NameNode是两个随时可互相切换的HA服务器：

 

 

 

 

Xml代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

01. &lt;property&gt;
02. &lt;name&gt;dfs.nameservices&lt;/name&gt;
03. &lt;value&gt;ns1&lt;/value&gt;
04. &lt;/property&gt;
05. &lt;property&gt;
06. &lt;name&gt;dfs.ha.namenodes.ns1&lt;/name&gt;
07. &lt;value&gt;nn1,nn2&lt;/value&gt;
08. &lt;/property&gt;
09. &lt;property&gt;
10. &lt;name&gt;dfs.namenode.rpc-address.ns1.nn1&lt;/name&gt;
11. &lt;value&gt;xxx1.cn:8020&lt;/value&gt;
12. &lt;/property&gt;
13. &lt;property&gt;
14. &lt;name&gt;dfs.namenode.rpc-address.ns1.nn2&lt;/name&gt;
15. &lt;value&gt;xxx2.cn:8020&lt;/value&gt;
16. &lt;/property&gt;

 

 

于是将fileSystem设置成其中的一台服务器，

 

```
fileSystem = FileSystem.get(URI.create("hdfs://xxx1.cn:8020"), conf)
```

 

这样终于成功了，注意，如果你连接上的是一个standby服务器，是不能够Read成功的！只有连接上active状态的服务器才能成功，那么如何确定哪台服务器当时是active呢，可以通过命令：

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. hdfs haadmin -getServiceState nn1
2. standby
3. hdfs haadmin -getServiceState nn2
4. active

 

 

这表示nn2是active NameNode服务器。

 

那么问题就来了，如果这样的话，当NameNode发生自动切换时，是不能够智能地发生切换操作的，如何避免这个问题？

 

在Hadoop技术内幕 hadoop common和hdfs架构设计原理与实现一书中，专门有一章节介绍hadoop hdfs configuration的设置。

![](http://dl2.iteye.com/upload/attachment/0108/3523/6133ce54-debd-336b-9c79-09e032f64198.png)  
  

 

 

从其中可以看出整个Configuration的加载过程，如果我们在构造FileSystem的时候，加载服务端中的两个核心文件，比如将代码写成下面这种，是否能够最终成功呢？

 

Java代码  [![收藏代码](http://brandnewuser.iteye.com/images/icon_star.png)]()

1. String uri = “/file/config";
2. Configuration conf = new Configuration();
3. conf.addResource(new Path("/home/xxxx/hadoop/etc/hadoop/core-site.xml"));
4. conf.addResource(new Path("/home/xxxx/hadoop/etc/hadoop/hdfs-site.xml"));
5. fileSystem = FileSystem.get(conf);
6. FileStatus\[] fs = fileSystem.listStatus(new Path(uri));
7. for (FileStatus f : fs) {
8. System.out.println(f.getPath());
9. }

 

 

答案是可以的，甚至都不需要采用带URI的函数。虽然绕了个大圈回到了原点，解决办法也非常简单，但通过这个过程的分析，可以使得我们可以熟悉整个FileSystem初始化的过程，也算是收获不小吧。