Java程序中不通过hadoop jar的方式访问hdfs
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.2.0</version>
</dependency>
FileSystem fileSystem = null;
try {
Configuration conf = new Configuration();
fileSystem = FileSystem.get(conf);
BufferedReader bufferedReader = = new BufferedReader(new InputStreamReader(fsInputStream));
/**
* 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 {
<property>
<name>fs.defaultFS</name>
<value>hdfs://ns1</value>
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/util/PlatformName
at org.apache.hadoop.security.UserGroupInformation.getOSLoginModuleName(UserGroupInformation.java:303)
at org.apache.hadoop.security.UserGroupInformation.<clinit>(UserGroupInformation.java:348)
at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:2590)
at org.apache.hadoop.fs.FileSystem$Cache$Key.<init>(FileSystem.java:2582)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2448)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:367)
at com.xxxx.HdfsMain.main(HdfsMain.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.PlatformName
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 7 more
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>2.2.0</version>
</dependency>
Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2421)
at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2428)
at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:88)
at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2467)
at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2449)
at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:367)
public static Class<? extends FileSystem> getFileSystemClass(String scheme,
Configuration conf) throws IOException {
if (!FILE_SYSTEMS_LOADED) {
loadFileSystems();
}
Class<? extends FileSystem> clazz = null;
if (conf != null) {
clazz = (Class<? extends FileSystem>) conf.getClass("fs." + scheme + ".impl", null);
}
if (clazz == null) {
clazz = SERVICE_FILE_SYSTEMS.get(scheme);
}
if (clazz == null) {
throw new IOException("No FileSystem for scheme: " + scheme);
}
return clazz;
}
conf.set("fs.hdfs.impl", DistributedFileSystem.class.getName());
<property>
<name>dfs.nameservices</name>
<value>ns1</value>
</property> <property>
<name>dfs.ha.namenodes.ns1</name>
<value>nn1,nn2</value>
</property> <property>
<name>dfs.namenode.rpc-address.ns1.nn1</name>
<value>xxx1.cn:8020</value>
</property> <property>
<name>dfs.namenode.rpc-address.ns1.nn2</name>
<value>xxx2.cn:8020</value>
</property>
fileSystem = FileSystem.get(URI.create("hdfs://xxx1.cn:8020"), conf)
hdfs haadmin -getServiceState nn1
standby hdfs haadmin -getServiceState nn2
active

String uri = “/file/config";
Configuration conf = new Configuration();
conf.addResource(new Path("/home/xxxx/hadoop/etc/hadoop/core-site.xml"));
conf.addResource(new Path("/home/xxxx/hadoop/etc/hadoop/hdfs-site.xml"));
fileSystem = FileSystem.get(conf);
FileStatus[] fs = fileSystem.listStatus(new Path(uri));
for (FileStatus f : fs) {
System.out.println(f.getPath());
}
Java程序中不通过hadoop jar的方式访问hdfs的更多相关文章
- java程序中获取kerberos登陆hadoop
本文由作者周梁伟授权网易云社区发布. 一般我们在使用kbs登陆hadoop服务时都直接在shell中调用kinit命令来获取凭证,这种方式简单直接,只要获取一次凭证之后都可以在该会话过程中重复访问.但 ...
- Java程序中调用Python脚本的方法
在程序开发中,有时候需要Java程序中调用相关Python脚本,以下内容记录了先关步骤和可能出现问题的解决办法. 1.在Eclipse中新建Maven工程: 2.pom.xml文件中添加如下依赖包之后 ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...
- Derby安装,创建数据库,在Java程序中使用Derby
1,下载并安装Derby: 下载地址:http://db.apache.org/derby /derby_downloads.html,下载最新版本. 我用的是10.5.3.0. 解压缩到任意文件夹, ...
- eclipse java项目中明明引入了jar包 为什么项目启动的时候不能找到jar包 项目中已经 引入了 com.branchitech.app 包 ,但时tomcat启动的时候还是报错? java.lang.ClassNotFoundException: com.branchitech.app.startup.AppStartupContextListener java.lang.ClassN
eclipse java项目中明明引入了jar包 为什么项目启动的时候不能找到jar包 项目中已经 引入了 com.branchitech.app 包 ,但时tomcat启动的时候还是报错?java. ...
- Linux上从Java程序中调用C函数
原则上来说,"100%纯Java"的解决方法是最好的,但有些情况下必须使用本地方法.特别是在以下三种情况: 需要访问Java平台无法访问的系统特性和设备: 通过基准测试,发现Jav ...
- 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案
方案特点: 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案,简化软件开发流程,减少各应用系统相同模块的重复开发工作,提高系统稳定性和可靠性. 基于HTTP协议的开发接口 使用特点在网页 ...
- 如何在java程序中调用linux命令或者shell脚本
转自:http://blog.sina.com.cn/s/blog_6433391301019bpn.html 在java程序中如何调用linux的命令?如何调用shell脚本呢? 这里不得不提到ja ...
- java程序中默认浮点形值常量是什么类型的?如何区分不同类型的浮点型整数值常量?
java程序中默认浮点形值常量是什么类型的 默认的所有的浮点型数值都是double型
随机推荐
- 起thread时,运行报错terminate called without an active exception
I am getting a C++ error with threading: terminate called without an active exception Aborted How to ...
- keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'
在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor w ...
- Qt jsoncpp 对象拷贝、删除、函数调用 demo
/*************************************************************************************************** ...
- js之留言字数限制
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- nginx在使用proxy_pass的情况下开启error_page
error_page用于指定特定错误发生时要显示的url,但是如果请求经proxy_pass处理后,如何使error_page对upstream产生的错误进行处理呢? 方法很简单. 保持之前的erro ...
- java中守护线程的一些概念和用法
网上的资料中,守护线程的功能一般都是“只要当前JVM实例中尚存任何一个非守护线程没有结束,守护线程就全部工作:只有当最后一个非守护线程结束是,守护线程随着JVM一同结束工作,Daemon作用是为其他线 ...
- ResultSet.TYPE_SCROLL_SENSITIVE到底发生了什么?
转自:https://blog.csdn.net/axman/article/details/3984103 这个问题我在几年前说过,但今天再次从CSDN上看到有人问这个问题,可以看出,真正懂这个问题 ...
- 《DSP using MATLAB》第7章开始 Example7.1
FIR低通滤波器的设计要求示意图:
- LeetCode 760. Find Anagram Mappings
原题链接在这里:https://leetcode.com/problems/find-anagram-mappings/description/ 题目: Given two lists Aand B, ...
- 关于浏览器和IIS基础的简单理解
浏览器 输入域名或者IP地址,按回车访问后:发生了什么??IIS是如何工作的?为什么能这么工作?? 1 浏览器和IIS 分别是两个应用程序:浏览器访问网址实际就是 两个应用程序的数据交互往来: ...