2 HDFS的API操作

2.1 HDFS文件上传(测试参数优先级)

  1.编写源代码

        // 文件上传
@Test
public void testPut() throws Exception { Configuration conf = new Configuration();
conf.set("dfs.replication", "2");
// 1.获取fs对象
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); // 2.执行上传API
fs.copyFromLocalFile(new Path("D:\\Ztest\\yema.png"), new Path("/diyo/dashen/dengzhiyong/yema3.png")); // 3.关闭资源
fs.close();
System.out.println("上传over");
}

  2.将hdfs-site.xml拷贝到项目的根目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>

  3.参数优先级

  参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置

2.2 HDFS文件下载

        // 文件下载
@Test
public void testGet() throws Exception {
     //1 获取文件系统 
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop");

     //2 执行下载操作
// fs.copyToLocalFile(new Path("/diyo/dashen/dengzhiyong/yema3.png"), new Path("D:\\Ztest\\yema2.png"));
// delSrc是否删除源,路径,路径,useRawLocalFileSystem是否使用本地校验true(不产生crc校验)
fs.copyToLocalFile(false, new Path("/diyo/dashen/dengzhiyong/yema3.png"), new Path("D:\\Ztest\\yema3.png"),
true);

     //3 关闭资源
fs.close(); System.out.println("下载over");
}

2.3 HDFS文件夹删除

    // 文件/文件夹删除
@Test
public void testRmdir() throws Exception {
Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); // 删除(recursive:true :递归删除)
fs.delete(new Path("/diyo/dashen/dengzhiyong/yema3.png"), true);
fs.close();
System.out.println("删除over");
}

2.4 HDFS文件名更改

    // 更改文件名
@Test
public void testReName() throws Exception {
Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); fs.rename(new Path("/diyo/dashen/dengzhiyong/yema2.png"), new Path("/diyo/dashen/dengzhiyong/yema3.png")); fs.close();
System.out.println("重命名over");
}

2.5 HDFS文件详情查看

    // 查看文件详情:名称、权限、长度、块信息
@Test
public void testListFile() throws Exception { Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/Diyo"), true);
while (listFiles.hasNext()) { // 迭代器 : 有没有文件信息
LocatedFileStatus fileStatus = listFiles.next(); // 如果有,拿到信息
// 名称
String name = fileStatus.getPath().getName();
System.out.println("name:\t" + name);
// 权限
FsPermission permission = fileStatus.getPermission();
System.out.println("permission:\t" + permission);
// 长度
long len = fileStatus.getLen();
System.out.println("len:\t" + len);
// 分组
String group = fileStatus.getGroup();
System.out.println("group:\t" + group); // 块信息(数组是因为有多个副本)
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
System.out.println("blockLocation:\t" + blockLocation);
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println("host:\t" + host);
}
}
System.out.println("-----------------");
}
}

2.6 HDFS文件和文件夹判断

    // 文件和文件夹的判断
@Test
public void testListStatus() throws Exception { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isFile()) {
System.out.println("文件-:" + fileStatus.getPath().getName());
}
if (fileStatus.isDirectory()) {
System.out.println("文件夹r:/" + fileStatus.getPath().getName());
fs.listFiles(fileStatus.getPath(), true);
}
} /*
* RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),
* true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus =
* listFiles.next(); // fileStatus.getPath();
*
* FileStatus[] listStatus = fs.listStatus(fileStatus.getPath());
*
* for (FileStatus status : listStatus) { if (status.isFile()) {
* System.out.println("文件-:" + status.getPath().getName()); } else {
* System.out.println("文件夹d:" + status.getPath().getName()); } } }
*/
fs.close();
System.out.println("判断over");
}

2.7 HDFS查看文件内容目录结构

    //查看文件内容
@Test
public void testCatFileContext() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop");
FSDataInputStream fdis = fs.open(new Path("/xsync"));
int len = 0;
while((len = fdis.read())!=-1) {
System.out.print((char)len);
}
} //查看目录结构
@Test
public void showTree() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop");
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus sta : listStatus) {
if (sta.isFile() && sta.getLen() > 0) {
showDetail(sta);
// System.out.println("------------");
}else if (sta.isDirectory()) {
showDetail(sta); }
} }
private void showDetail(FileStatus sta) {
System.out.println
(sta.getPath()+"\t"+
sta.getLen()+"\t"+
sta.getOwner()+"\t"+
sta.getAccessTime());
}

3 HDFS的I/O流操作

3.1 HDFS文件上传

  1.需求:把本地文件上传到HDFS根目录

  2.编写代码

@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 创建输入流
FileInputStream fis = new FileInputStream(new File("e:/banhua.txt")); // 3 获取输出流
FSDataOutputStream fos = fs.create(new Path("/banhua.txt")); // 4 流对拷
IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}

3.2 HDFS文件下载

  1.需求:从HDFS上下载banhua.txt文件到本地e盘上

  2.编写代码

// 文件下载
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/banhua.txt")); // 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("e:/banhua.txt")); // 4 流的对拷
IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}

3.3 定位文件读取

  1.需求:分块读取HDFS上的大文件,比如根目录下的/hadoop-2.7.2.tar.gz

  2.编写代码

  (1)下载第一块

@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz")); // 3 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part1")); // 4 流的拷贝
byte[] buf = new byte[1024]; for(int i =0 ; i < 1024 * 128; i++){
fis.read(buf);
fos.write(buf);
} // 5关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fs.close();
}

  (2)下载第二块

@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu"); // 2 打开输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-2.7.2.tar.gz")); // 3 定位输入数据位置
fis.seek(1024*1024*128); // 4 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part2")); // 5 流的对拷
IOUtils.copyBytes(fis, fos, configuration); // 6 关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}

  3)合并文件

  在Window命令窗口中进入到目录E:\,然后执行如下命令,对数据进行合并

  type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1

  合并完成后,将hadoop-2.7.2.tar.gz.part1重新命名为hadoop-2.7.2.tar.gz。解压发现该tar

个人代码:

package com.diyo.hdfs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URI; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test; public class HDFSIO { // 从本地上传到HDFS
@Test
public void testputFileToHDFS() throws Exception { // 1 获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); // 2 获取输入流
FileInputStream fis = new FileInputStream("D:/Ztest/yema.png"); // 3 获取输出流
FSDataOutputStream fos = fs.create(new Path("/newyama.png")); // 4 流的对拷
IOUtils.copyBytes(fis, fos, conf); // 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
System.out.println("over");
} // 从HDFS下载到本地
@Test
public void testgetFileFromHDFS() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); FSDataInputStream fis = fs.open(new Path("/newyama.png")); FileOutputStream fos = new FileOutputStream("d:/Ztest/newyema.png"); IOUtils.copyBytes(fis, fos, conf); IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
System.out.println("over");
} // 定位文件读取(下载第一块)
@Test
public void testReadFileSeek1() throws Exception { Configuration conf = new Configuration();
// 获取对象
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); // 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.0.tar.gz")); // 获取输出流
FileOutputStream fos = new FileOutputStream("d:/Ztest/hadoop-3.1.0.tar.gz.part1"); // 流的对拷
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fis.read(buf);
fos.write(buf);
} // 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close(); System.out.println("over");
} // 定位文件读取(下载第二块)
@Test
public void testReadFileSeek2() throws Exception { Configuration conf = new Configuration();
// 获取对象
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop"); // 获取输入流
FSDataInputStream fis = fs.open(new Path("/hadoop-3.1.0.tar.gz")); // 设置指定读取的起点
fis.seek(1024*1024*128); // 获取输出流
FileOutputStream fos = new FileOutputStream("d:/Ztest/hadoop-3.1.0.tar.gz.part2"); // 流的对拷
IOUtils.copyBytes(fis, fos, conf); //关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close(); System.out.println("over");
}
}

客户端操作 2 HDFS的API操作 3 HDFS的I/O流操作的更多相关文章

  1. 还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下

    Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda ...

  2. JDK8 Steam流操作

    原文:https://github.com/niumoo/jdk-feature/blob/master/src/main/java/net/codingme/feature/jdk8/Jdk8Str ...

  3. 超强的Lambda Stream流操作

    原文:https://www.cnblogs.com/niumoo/p/11880172.html 在使用 Stream 流操作之前你应该先了解 Lambda 相关知识,如果还不了解,可以参考之前文章 ...

  4. Hadoop基础-HDFS的API常见操作

    Hadoop基础-HDFS的API常见操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本文主要是记录一写我在学习HDFS时的一些琐碎的学习笔记, 方便自己以后查看.在调用API ...

  5. HDFS03 HDFS的API操作

    HDFS的API操作 目录 HDFS的API操作 客户端环境准备 1.下载windows支持的hadoop 2.配置环境变量 3 在IDEA中创建一个Maven工程 HDFS的API实例 用客户端远程 ...

  6. HDFS Java API 常用操作

    package com.luogankun.hadoop.hdfs.api; import java.io.BufferedInputStream; import java.io.File; impo ...

  7. HDFS shell操作及HDFS Java API编程

    HDFS shell操作及HDFS Java API编程 1.熟悉Hadoop文件结构. 2.进行HDFS shell操作. 3.掌握通过Hadoop Java API对HDFS操作. 4.了解Had ...

  8. hadoop hdfs java api操作

    package com.duking.util; import java.io.IOException; import java.util.Date; import org.apache.hadoop ...

  9. HDFS常用API操作 和 HDFS的I/O流操作

    前置操作 创建maven工程,修改pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

随机推荐

  1. DOM练习 选择框、表格添加、变色

    多个选择框,三个按钮,显示:全选.反选.不选 html部分,建立五个多选框,三个按钮 <input type="checkbox"> <input type=&q ...

  2. 萌新学渗透之Hack The Box_Beep

    我将我的walkthrough过程用视频解说的形式记载 视频地址https://www.bilibili.com/video/BV1VT4y1j7dg 一是因为看我视频的后来者应该都是刚入门的新手,视 ...

  3. 编写优美Android注释的常用语法

    编写优美Android注释的常用语法   短期目标是定期能出一篇简文,希望自己能坚持下去~~~~( ̄_, ̄ )   附上Android君 今天要分享的是关于Android注释系统的一些强大功能!! 实 ...

  4. Python Tuple(元组) tuple()方法

    描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- ...

  5. PHP localtime() 函数

    ------------恢复内容开始------------ 实例 以一个数值数组和一个关联数组的形式输出本地时间: <?phpprint_r(localtime());echo "& ...

  6. 4.28 省选模拟赛模拟赛 最佳农场 二维卷积 NTT

    第一次遇到二维卷积 不太清楚是怎么做的. 40分暴力比对即可. 对于行为或者列为1时 容易想到NTT做快速匹配.然后找答案即可. 考虑这是一个二维的比对过程. 设\(f_{i,j}\)表示以i,j为右 ...

  7. 4.22 省选模拟赛 三元组 manacher 回文自动机

    容易发现可以枚举j 那么只需要计算出 l~j这段是回文串的l的和 以及j+1~r这段是回文串的r的和. 可以manacher 之后想要求出以j为右端点的回文串左端点的和 这个东西我们通过某个点为中心的 ...

  8. Ploya定理学习笔记

    由于自己的作息极其不规律导致比赛被打爆了 但是有的时候状态其实还行. 关于Ploya定理其实特别有意思 这里粘一个[dalao的blog](https://blog.csdn.net/lyc16355 ...

  9. Python selenium 三种消息框处理和浏览器(页面跳转)句柄处理

    1. alert: 警告框,只是一个提示信息,只有一个确定按钮,起提示用户的作用: 2.confirm: 确认框,确定和取消按钮会带来不同的结果.点击确定会执行操作,点击取消按钮会取消操作: 3.pr ...

  10. Python最全pdf学习书籍资料分享

    本人学习Python两年时间,期间统计了一些比较好的学习资料 1.基础资料  下载地址: 链接:https://pan.baidu.com/s/1sjtyYayBbQLsrUdaXWmzkg提取码:1 ...