Hadoop的FlieSystem类的使用
1.使用FileSystem类需要导入jar包
解压hadoop-2.7.7.tar.gz
复制如下三个jar包和lib下所有jar包到项目文件下的lib文件
2.查看文件信息
@Test
public void readListFiles() throws Exception {
// 1 创建配置信息对象
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),configuration, "root"); // 思考:为什么返回迭代器,而不是List之类的容器
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println(fileStatus.getPath().getName()); //路径
System.out.println(fileStatus.getBlockSize()); //块
System.out.println(fileStatus.getPermission()); //权限
System.out.println(fileStatus.getLen()); //文件大小
System.out.println(fileStatus.isFile()); //是不是一个文件
System.out.println(fileStatus.isDirectory()); //是不是一个目录 BlockLocation[] blockLocations = fileStatus.getBlockLocations(); for (BlockLocation bl : blockLocations) { System.out.println("block-offset:" + bl.getOffset()); String[] hosts = bl.getHosts(); for (String host : hosts) {
System.out.println(host);
}
} System.out.println("----------------------------");
}
}
3.文件下载(get)
@Test
public void download() {
Configuration conf=new Configuration();
try
{
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),conf);
FSDataInputStream in = fileSystem.open(new Path("/upload.txt"));
FileOutputStream out = new FileOutputStream(new File("d://lib//updoad.txt"));
byte[]b=new byte[1024];
int len=0;
while((len=in.read(b))!=-1) {
out.write(b,0,len);
}
in.close();
out.close();
} catch (IOException | URISyntaxException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
} }
4.上传文件(create)
@Test
public void upload() {
Configuration conf=new Configuration();
try
{
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),conf);
FSDataOutputStream out = fileSystem.create(new Path("/jetbrains-agent.jar"));
FileInputStream in=new FileInputStream(new File("d:\\jetbrains-agent.jar"));
byte[]b=new byte[10240];
int len=0;
while((len=in.read(b))!=-1) {
out.write(b,0,len);
}
in.close();
out.close();
} catch (IOException | URISyntaxException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
5.重命名(rename)
@Test
public void mv() {
Configuration conf=new Configuration();
try
{
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),conf);
fileSystem.rename(new Path("/hdp01"), new Path("/HDP01"));
fileSystem.close();
} catch (IOException | URISyntaxException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
6.文件夹删除
@Test
public void deleteAtHDFS() throws Exception{
// 1 创建配置信息对象
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),configuration, "root"); //2 删除文件夹 ,如果是非空文件夹,参数2是否递归删除,true递归
fs.delete(new Path("hdfs://192.168.0.xxx:9000/upload/output"), true);
}
7.创建文件夹
@Test
public void mkdirAtHDFS() throws Exception{
// 1 创建配置信息对象
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),configuration, "root"); //2 创建目录
fs.mkdirs(new Path("hdfs://192.168.0.xxx:9000/upload/output"));
}
8.遍历所有文件状态
@Test
public void findAtHDFS() throws Exception, IllegalArgumentException, IOException{ // 1 创建配置信息对象
Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.0.xxx:9000"),configuration, "root"); // 2 获取查询路径下的文件状态信息
FileStatus[] listStatus = fs.listStatus(new Path("/")); // 3 遍历所有文件状态
for (FileStatus status : listStatus) {
if (status.isFile()) {
System.out.println("f--" + status.getPath().getName());
} else {
System.out.println("d--" + status.getPath().getName());
}
}
}
Hadoop的FlieSystem类的使用的更多相关文章
- Hadoop之TaskInputOutputContext类
在MapReduce过程中,每一个Job都会被分成若干个task,然后再进行处理.那么Hadoop是怎么将Job分成若干个task,并对其进行跟踪处理的呢?今天我们来看一个*Context类——Tas ...
- Hadoop之TaskAttemptContext类和TaskAttemptID类
先来看看TaskAttemptContext的类图 : Figure1:TaskAttemptContext类图 用户向Hadoop提交Job(作业),Job在JobTracker对象的控制下执行.J ...
- hadoop中Text类 与 java中String类的区别
hadoop 中 的Text类与java中的String类感觉上用法是相似的,但两者在编码格式和访问方式上还是有些差别的,要说明这个问题,首先得了解几个概念: 字符集: 是一个系统支持的所有抽象字符的 ...
- Hadoop中Writable类之四
1.定制Writable类型 Hadoop中有一套Writable实现,例如:IntWritable.Text等,但是,有时候可能并不能满足自己的需求,这个时候,就需要自己定制Writable类型. ...
- Hadoop中Writable类之三
1.BytesWritable <1>定义 ByteWritable是对二进制数据组的封装.它的序列化格式为一个用于指定后面数据字节数的整数域(4个字节),后跟字节本身. 举个例子,假如有 ...
- Hadoop中Writable类之二
1.ASCII.Unicode.UFT-8 在看Text类型的时候,里面出现了上面三种编码,先看看这三种编码: ASCII是基于拉丁字母的一套电脑编码系统.它主要用于显示现代英语和其他西欧语言.它是现 ...
- hadoop之mapper类妙用
1. Mapper类 首先 Mapper类有四个方法: (1) protected void setup(Context context) (2) Protected void map(KEYIN k ...
- Hadoop中Writable类
1.Writable简单介绍 在前面的博客中,经常出现IntWritable,ByteWritable.....光从字面上,就可以看出,给人的感觉是基本数据类型 和 序列化!在Hadoop中自带的or ...
- 琐碎-关于hadoop的GenericOptionsParser类
GenericOptionsParser 命令行解析器 是hadoop框架中解析命令行参数的基本类.它能够辨别一些标准的命令行参数,能够使应用程序轻易地指定namenode,jobtracker,以及 ...
随机推荐
- Result Maps collection already contains value for com.xxx.x.dao.xxxMapper.Bas
springboot启动时候,报错如下: Result Maps collection already contains value for com.xxx.xx.dao.xxxxxMapper.Ba ...
- Django——整体结构/MVT设计模式
MVT设计模式 Models 封装数据库,对数据进行增删改查; Views 进行业务逻辑的处理 Templates 进行前端展示 前端展示看到的是模板 --> 发起 ...
- 二十、JavaScript之对象
一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...
- 109-PHP类成员初始化值
<?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...
- C#中类的字段或属性不被序列化成JSON或XML
将一个类序列化成JSON或XML时,如果某个字段或属性不想被序列化,则可以使用以下Attribute: 1.[Newtonsoft.Json.JsonIgnore]特性:使用Newtonsoft.Js ...
- 详解BurpSuite软件 请求包 HTTP (9.23 第十天)
HTTP协议基础 HTTP:HyperText Transfer Protocol,超文本传输协议 1.协议特点: 简单快速,请求方式get post head等8中请求方式 无连接(一次请求就断开) ...
- POJ 3368:Frequent values
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14764 Accepted: 5361 ...
- 【redis】redis底层数据结构原理--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表等
redis有五种数据类型string.list.hash.set.zset(字符串.哈希.列表.集合.有序集合)并且自实现了简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等数据结构.red ...
- 【转】CSS实现自适应分隔线的N种方法
1.伪元素+transform:translateX(-100%); 主要原理是设置文本居中text-align: center;,然后给定两个伪元素,分别绝对定位,那么此时伪元素也是跟随着水平居中的 ...
- JS高级学习笔记(9) 之 转:前端路由跳转基本原理
原文链接: 前端路由跳转基本原理 前述 前端三大框架Angular.React和Vue都推行单页面应用SPA开发模式,这是因为在路由切换时,替换DOM Tree中发生修改的DOM部分,来减少原来因为多 ...
