hdfs深入:09、获取分布式文件系统客户端的几种方式
FileSystem是一个抽象类:
获取一个抽象类有两种方式:
第一种:看这个抽象类有没有提供什么方法返回他本身
第二种:找子类
具体代码如下;
/**
* 通过url注册的方式访问hdfs,了解,不会用到
* @throws Exception
*/
@Test
public void getHdfsFile() throws Exception{
// System.out.println("hello world.");
//1.注册hdfs的url,让java代码能识别hdfs的url形式
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); InputStream in = null;
FileOutputStream out = null; //定义文件访问的url地址
String url = "hdfs://node01:8020/test/input/install.log"; //打开文件输入流
in = new URL(url).openStream();
out = new FileOutputStream(new File("D:\\installOut.txt"));
IOUtils.copy(in,out); IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
} /**
* 通过FileSystem获取分布式文件系统的几种方式
* 方式一
*/
@Test
public void getFileSystem1() throws IOException {
//如果configuration不做任何配置,获取到的是本地文件系统 "file:///"
Configuration configuration = new Configuration(); //覆盖默认配置,得到分布式文件系统
configuration.set("fs.defaultFS","hdfs://node01:8020"); FileSystem fileSystem = FileSystem.get(configuration); System.out.println(fileSystem);
} /**
* 方式二
*/
@Test
public void getHdfs2() throws URISyntaxException, IOException {
//使用2个参数
//参数1是一个URI,定义了使用hdfs://这种方式来访问,就是分布式文件系统
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
System.out.println(fileSystem);
} /**
* 方式三
*/
@Test
public void getHdfs3() throws URISyntaxException, IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node01:8020/");
FileSystem fileSystem = FileSystem.newInstance(configuration);
System.out.println(fileSystem);
} /**
* 方式四
*/
@Test
public void getHdfs4() throws URISyntaxException, IOException {
FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020"), new Configuration());
System.out.println(fileSystem);
}
hdfs深入:09、获取分布式文件系统客户端的几种方式的更多相关文章
- Axis2开发WebService客户端 的3种方式
Axis2开发WebService客户端 的3种方式 在dos命令下 wsdl2java -uri wsdl的地址(网络上或者本地) -p com.whir.ezoffi ...
- GRpc添加客户端的四种方式
随着微服务的发展,相信越来越多的.net人员也开始接触GRpc这门技术,大家生成GRpc客户端的方式也各不相同,今天给大家介绍一下依据Proto文件生成Rpc客户端的四种方式 前提:需要安装4个Nug ...
- 获取机器安装.NET版本的几种方式
当调查应用程序问题时,通常需要先确认目标机器所安装的 .NET Framework 的版本.可以通过如下方式来确认版本号: 通过控制面板安装程序查询 通过查询注册表获取版本信息 通过查看安装目录获取版 ...
- Struts2中获取HttpServletRequest,HttpSession等的几种方式
转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...
- Action 中获取表单数据的三种方式
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905 冷血之心的博客) Action 中获取表单提交数据 ...
- SpringJUnit4加载类目录下(src)和WEF-INF目录下的配置文件二--获取注入的bean的二种方式
前言: spring容器以xml的形式注入bean,然后可以在类中获取,获取的形式主要有二种:第一种最简单--采用@Resource 或@Autowired关键字在加载spring文件时将bean注入 ...
- XFire构建web service客户端的五种方式
这里并未涉及到JSR 181 Annotations 的相关应用,具体的三种方式如下 ① 通过WSDL地址来创建动态客户端 ② 通过服务端提供的接口来创建客户端 ③ 使用Ant通过WSDL文件来生成客 ...
- Spring Boot获取前端页面参数的几种方式总结
Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...
- js中获取页面元素节点的几种方式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
随机推荐
- Outlook 2007 发送邮件
4 登入以投票 Hi, http://social.msdn.microsoft.com/Forums/zh-TW/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-t ...
- 我使用过的Linux命令之hexdump - ”十六“进制查看器(转载)
转载:http://codingstandards.iteye.com/blog/805778 本文链接:http://codingstandards.iteye.com/blog/805778 ...
- linux文件名乱码时删除或改名的方式(转载)
转自:http://www.linuxsa.cn/when-linux-file-name-topsy-turvy-deleted-or-renamed.html linux文件名乱码时删除或改名的方 ...
- bzoj 1006: [HNOI2008]神奇的国度【弦图+LesBFS】
参考论文:https://wenku.baidu.com/view/6f9f2223dd36a32d73758126.html 参考代码:http://hzwer.com/3500.html 虽然会写 ...
- 洛谷P3295 [SCOI2016]萌萌哒(倍增+并查集)
传送门 思路太妙了啊…… 容易才怪想到暴力,把区间内的每一个数字用并查集维护相等,然后设最后总共有$k$个并查集,那么答案就是$9*10^{k-1}$(因为第一位不能为0) 考虑倍增.我们设$f[i] ...
- 在Python解释器运行程序
在解释器中运行 ***.py文件的方法:使用import添加模块 ***.py,然后调用 ***.py中的函数 例:在zoo.py中定义hours函数 运行方法: >>> impo ...
- 题解报告:hdu 2059 龟兔赛跑
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击—— ...
- linux安装glassfish并布署
1 https://glassfish.java.net/download.html 2 准备工作:需要jdk7以上版本 Java EE 7 requires JDK 7 (or above) 下载g ...
- sdut1283Five in a Row, Again
一简单的状压题 比赛时跑偏了 ,脑子最近乱的跟浆糊似得呢.. #include <iostream> #include<cstdio> #include<cstring& ...
- Java_JDBC连接数据库
package com.accp.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pre ...