HDFS常用API操作 和 HDFS的I/O流操作
前置操作
创建maven工程,修改pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcq</groupId>
<artifactId>HDFS-001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies> </project>
在resources添加一个file:log4j.properties:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
API操作
HDFS的命令和linux极其相似,可以类比记忆,在这里列出一些java api操作:
package com.mcq; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Test; public class HDFSClient {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
// c.set("fs.defaultFS", "hdfs://hadoop103:9000");
// FileSystem fs = FileSystem.get(c);
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "mcq");
fs.mkdirs(new Path("/ppqq"));
fs.close();
System.out.println("over");
} @Test // 文件上传
public void testCopyFromLocalFile()
throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "mcq");
fs.copyFromLocalFile(new Path("d:/banzhang.txt"), new Path("/banzhang.txt"));
fs.close();
System.out.println("over");
} @Test // 文件下载
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "mcq");
fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("d:/hadoop test/banhua.txt"), true);
// 第一个false表示不剪切,最后一个true表示本地,不产生crc文件 fs.close();
System.out.println("over");
} @Test // 文件删除
public void testDelete() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "mcq");
fs.delete(new Path("/0811"), true); // 是否递归删除
fs.close();
System.out.println("over");
} @Test // 文件更名
public void testRename() throws IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "mcq");
fs.rename(new Path("/banzhang.txt"), new Path("/lala.txt"));
fs.close();
System.out.println("over");
} @Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException { // 1获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "mcq"); // 2 获取文件详情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) {
LocatedFileStatus status = listFiles.next(); // 输出详情
// 文件名称
System.out.println(status.getPath().getName());
// 长度
System.out.println(status.getLen());
// 权限
System.out.println(status.getPermission());
// 分组
System.out.println(status.getGroup()); // 获取存储的块信息
BlockLocation[] blockLocations = status.getBlockLocations(); for (BlockLocation blockLocation : blockLocations) { // 获取块存储的主机节点
String[] hosts = blockLocation.getHosts(); for (String host : hosts) {
System.out.println(host);
}
} System.out.println("-----------分割线----------");
} // 3 关闭资源
fs.close();
} @Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "mcq"); // 2 判断是文件还是文件夹
FileStatus[] listStatus = fs.listStatus(new Path("/")); for (FileStatus fileStatus : listStatus) { // 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
} // 3 关闭资源
fs.close();
}
}
I/O流操作
上面的API操作 HDFS系统都是框架封装好的,如果我们想自己实现上述API操作可以采用IO流的方式实现数据的上传和下载。
package com.mcq; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; 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.apache.hadoop.yarn.api.records.URL;
import org.junit.Test; public class HDFSIO {
//文件上传
@Test
public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "mcq"); // 2 创建输入流
FileInputStream fis = new FileInputStream(new File("d:/banzhang.txt")); // 3 获取输出流
FSDataOutputStream fos = fs.create(new Path("/xiaocao.txt")); // 4 流对拷
IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
// 文件下载
@Test
public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "mcq"); // 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/banhua.txt")); // 3 获取输出流
FileOutputStream fos = new FileOutputStream(new File("d:/banhua.txt")); // 4 流的对拷
IOUtils.copyBytes(fis, fos, configuration); // 5 关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
//定位文件读取
//(1)下载第一块
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "mcq"); // 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://hadoop103:9000"), configuration, "mcq"); // 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);
}
}
HDFS常用API操作 和 HDFS的I/O流操作的更多相关文章
- HDFS常用API(1)
一.HDFS集群API所需要jar包的maven配置信息 <dependency> <groupId>org.apache.hadoop</groupId> < ...
- HDFS连接JAVA,HDFS常用API
先在pom.xml中导入依赖包 <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/ha ...
- hdfs常用api(java)
1.下载文件到本地 public class HdfsUrlTest { static{ //注册url 让java程序识别hdfs的url URL.setURLStreamHandlerFactor ...
- HDFS常用API(2)
一.读取HDFS文件数据.将本地文件写入HDFS中文件.使用IOUtils读写数据 ** * @author: PrincessHug * @date: 2019/3/18, 17:24 * @Blo ...
- HDFS常用命令
HDFS 常用的文件操作命令 hdfs dfs -text /pub/20151019/1/4/gwmvod/mediags.moretv.com.cn/*.bz2 | wc -l hdfs dfs ...
- 还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下
Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda ...
- 超强的Lambda Stream流操作
原文:https://www.cnblogs.com/niumoo/p/11880172.html 在使用 Stream 流操作之前你应该先了解 Lambda 相关知识,如果还不了解,可以参考之前文章 ...
- 大数据(5) - HDFS中的常用API操作
一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...
- HDFS 05 - HDFS 常用的 Java API 操作
目录 0 - 配置 Hadoop 环境(Windows系统) 1 - 导入 Maven 依赖 2 - 常用类介绍 3 - 常见 API 操作 3.1 获取文件系统(重要) 3.2 创建目录.写入文件 ...
随机推荐
- Python—文件读写操作
初识文件操作 使用open()函数打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了,根据打开文件的方式不同能够执行的操作也会有相应的差异. 打开文件的方式: r, w, a, ...
- 【声明式事务】Spring声明式事务实现(三)
以MyBatis为例. 一.基于注解的声明式事务配置 1. 添加tx名字空间 xmlns:tx="http://www.springframework.org/schema/tx" ...
- 【转】 AOP(面向切面编程)、Filter(过虑器)、Interceptor(拦截器)
AOP(面向切面编程) 面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ,我们知道,面向对象的特点是继承.多态和封装.而封装就要求将功能分散到不同的对象中去,这在软 ...
- 第02组 Beta冲刺(1/5)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 初步任务分配 提交记录(全组共用) 接下来的计划 完善接口文档 还剩下哪些任务 学习软工的理论课 学习代码评估.测试 燃尽 ...
- Oracle中如何创建数据库
Oracle数据库的物理结构与MySQL以及SQLServer有着很大的不同.在使用MySQL或SQLServer时,我们不需要去关心它们的逻辑结构和物理结构. 但是在使用Oracle的时候,我们必须 ...
- 物联网架构成长之路(35)-利用Netty解析物联网自定义协议
一.前言 前面博客大部分介绍了基于EMQ中间件,通信协议使用的是MQTT,而传输的数据为纯文本数据,采用JSON格式.这种方式,大部分一看就知道是熟悉Web开发.软件开发的人喜欢用的方式.由于我也是做 ...
- Azure Automation (7) 执行Azure SQL Job
<Windows Azure Platform 系列文章目录> 之前Automation介绍的内容,是在ASM模式下自动化开关机. 本章将介绍如何在Automation中,设置开关机脚本, ...
- 原生javascript 共享onload事件
在工作时,我们给一个元素绑定了事件,如果dom还没加载完成,就执行了js代码,就不会绑定成功.常规解决方案就是用: window.onload = EventFunction; 可是如果有两个 事件, ...
- 第二十一节:Asp.Net Core MVC和WebApi路由规则的总结和对比
一. Core Mvc 1.传统路由 Core MVC中,默认会在 Startup类→Configure方法→UseMvc方法中,会有默认路由:routes.MapRoute("defaul ...
- laravel模型中非静态方法也能静态调用的原理
刚开始用laravel模型时,为了方便一直写静态方法,进行数据库操作. <?php namespace App\Models; use Illuminate\Database\Eloquent\ ...