java实现对Hadoop的操作
作者:Even710 发布时间:2021-10-05 16:30:37
标签:Java,Hadoop
基本操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
@RunWith(JUnit4.class)
@DisplayName("Test using junit4")
public class HadoopClientTest {
private FileSystem fileSystem = null;
@BeforeEach
public void init() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "1");
configuration.set("dfs.blocksize", "64m");
fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000"), configuration, "root");
}
/**
* 从本地复制文件到Hadoop
*
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void copyFileFromLocal() throws URISyntaxException, IOException, InterruptedException {
// 上传文件
fileSystem.copyFromLocalFile(new Path("C:\\Users\\Administrator\\Desktop\\win10激活.txt"), new Path("/even1"));
// 关闭流,报错winUtils,因为使用了linux的tar包,如果windows要使用,则需要编译好这个winUtils包才能使用
fileSystem.close();
}
/**
* 从Hadoop下载文件到本地,下载需要配置Hadoop环境,并添加winutils到bin目录
*
* @throws URISyntaxException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void copyFileToLocal() throws URISyntaxException, IOException, InterruptedException {
// 下载文件
fileSystem.copyToLocalFile(new Path("/win10激活.txt"), new Path("E:/"));
// 关闭流,报错winUtils,因为使用了linux的tar包,如果windows要使用,则需要编译好这个winUtils包才能使用
fileSystem.close();
}
/**
* 创建文件夹
*
* @throws IOException
*/
@Test
public void hdfsMkdir() throws IOException {
// 调用创建文件夹方法
fileSystem.mkdirs(new Path("/even1"));
// 关闭方法
fileSystem.close();
}
/**
* 移动文件/修改文件名
*/
public void hdfsRename() throws IOException {
fileSystem.rename(new Path(""), new Path(""));
fileSystem.close();
}
/**
* 删除文件/文件夹
*
* @throws IOException
*/
@Test
public void hdfsRm() throws IOException {
// fileSystem.delete(new Path(""));
// 第二个参数表示递归删除
fileSystem.delete(new Path(""), true);
fileSystem.close();
}
/**
* 查看hdfs指定目录的信息
*
* @throws IOException
*/
@Test
public void hdfsLs() throws IOException {
// 调用方法返回远程迭代器,第二个参数是把目录文件夹内的文件也列出来
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus locatedFileStatus = listFiles.next();
System.out.println("文件路径:" + locatedFileStatus.getPath());
System.out.println("块大小:" + locatedFileStatus.getBlockSize());
System.out.println("文件长度:" + locatedFileStatus.getLen());
System.out.println("副本数量:" + locatedFileStatus.getReplication());
System.out.println("块信息:" + Arrays.toString(locatedFileStatus.getBlockLocations()));
}
fileSystem.close();
}
/**
* 判断是文件还是文件夹
*/
@Test
public void findHdfs() throws IOException {
// 1,展示状态信息
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
// 2,遍历所有文件
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isFile())
System.out.println("是文件:" + fileStatus.getPath().getName());
else if (fileStatus.isDirectory())
System.out.println("是文件夹:" + fileStatus.getPath().getName());
}
fileSystem.close();
}
}
文件读写
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.Before;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@RunWith(JUnit4.class)
@DisplayName("this is read write test!")
public class HadoopReadWriteTest {
FileSystem fileSystem = null;
Configuration configuration = null;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
// 1,加载配置
configuration = new Configuration();
// 2,构建客户端
fileSystem = FileSystem.get(new URI("hdfs://hd-even-01:9000/"), configuration, "root");
}
@Test
public void testReadData() throws IOException {
// 1,获取hdfs文件流
FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));
// 2,设置一次获取的大小
byte[] bytes = new byte[1024];
// 3,读取数据
while (open.read(bytes) != -1)
System.out.println(Arrays.toString(bytes));
open.close();
fileSystem.close();
}
/**
* 使用缓存流
*
* @throws IOException
*/
@Test
public void testReadData1() throws IOException {
FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));
// 使用缓冲流会快点
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(open, StandardCharsets.UTF_8));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
open.close();
fileSystem.close();
}
/**
* 指定偏移量来实现只读部分内容
*/
@Test
public void readSomeData() throws IOException {
FSDataInputStream open = fileSystem.open(new Path("/win10激活.txt"));
// 指定开始的index
open.seek(14);
// 指定读的多少
byte[] bytes = new byte[5];
while (open.read(bytes) != -1)
System.out.println(new String(bytes));
open.close();
fileSystem.close();
}
/**
* 流方式写数据
* @throws IOException
*/
@Test
public void writeData() throws IOException {
// 1,获取输出流
FSDataOutputStream out = fileSystem.create(new Path("/win11.txt"), false);
// 2,获取需要写的文件输入流
FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt"));
byte[] b = new byte[1024];
int read = 0;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
in.close();
out.close();
fileSystem.close();
}
/**
* 直接写字符串
*/
@Test
public void writeData1() throws IOException {
// 1,创建输出流
FSDataOutputStream out = fileSystem.create(new Path("/aibaobao.txt"), false);
// 2,写数据
out.write("wochaoaibaobao".getBytes());
// 3,关闭流
IOUtils.closeStream(out);
fileSystem.close();
}
/**
* IOUtils方式上传
*
* @throws IOException
*/
@Test
public void putToHdfs() throws IOException {
// 1,获取输入流
FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\xixi.txt"));
// 2,获取输出流
FSDataOutputStream out = fileSystem.create(new Path("/haddopPut.txt"), false);
// 3,拷贝
IOUtils.copyBytes(in, out, configuration);
// 4,关闭流
IOUtils.closeStream(in);
IOUtils.closeStream(out);
fileSystem.close();
}
/**
* IOUtils方式下载
* @throws IOException
*/
@Test
public void getFromHdfs() throws IOException {
// 1,获取输入流
FSDataInputStream open = fileSystem.open(new Path("/haddopPut.txt"));
// 2,获取输出流
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\haddopPut.txt"));
// 3,拷贝
IOUtils.copyBytes(open, out, configuration);
// 4,关闭流
IOUtils.closeStream(open);
IOUtils.closeStream(out);
fileSystem.close();
}
}
来源:https://blog.csdn.net/weixin_37581297/article/details/84349916
0
投稿
猜你喜欢
- 对单表进行增删改查是项目中不可避免的需求,Mybatis的通用Mapper插件使这些操作变得简单添加maven依赖在对应工程的pom.xml
- 在使用Java web开发的后端工程师们大多会使用Maven作为项目构建以及编译的工具,微服务和大中台当道的今天,更加关注maven的细节是
- 一、using语句using可以算是.NET中新的语法元素,它清楚地说明一个通常比较占用资源的对象何时开始使用和何时被手动释放。当using
- 前言JDK 1.5 之前 synchronized 的性能是比较低的,但在 JDK 1.5 中,官方推出一个重量级功能 Lock,一举改变了
- 短网址,忽然一下子就冒出来的东西,长长的一个URL,提交过去,出来就只有短短的一个URL了,看起来似乎挺神奇,其实简单分析一下,明白其中的原
- 01-前言:什么是循环依赖?首先,我们先明确下依赖的定义。 如果一个 Bean bar 的属性,引用了容器中的另外一个 Bean foo,那
- jar包就指第三方提供的开源的API,这些API不属于JDK的,需要通过导入才能使用。添加和导入的区别注意:本文里的 导入 和 添加 jar
- static关键字在Java中,static是静态修饰关键字。用于修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性
- 系列文章已完成,目录如下:jdk-logging log4j logback日志系统实现机制原理介绍commons-lo
- 这篇文章主要介绍了java io读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- @RequestBody的作用@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),所以只能发送
- 目录1、需求2、问题2、获取1)导入依赖为了获取客户端类型、操作系统类型、ip、port2)封装获取body字符串的工具类3) * 类4)继
- 一,描写叙述 在多线程下编程的时候。大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取
- 前言加密配置是一个很常见的需求,在spring boot生态中,已经有非常多的第三方starter实现了,博主所在公司也有这种强制要求,一些
- 通常来说,多线程的并发及条件断点的debug是很难完成的,或许本篇文章会给你提供一个友好的调试方法。让你在多线程开发过程中的调试更加的有的放
- 1、Date日期输出可读性较差Date date = new Date();System.out.println(date);打印输出的结果
- 三层架构将整个业务应用划分为:(1)界面UI层(2)业务逻辑层(3)数据访问层对于复杂的系统分层可以让结构更加清晰,模块更加独立,便于维护。
- 今天在码代码的时候突然想到这个问题,觉得有点困惑。在网上也翻阅不少帖子其中有一个帖子给了我一个思路,其实也是解释了基础概念。概念一:try
- 1. 抽象类关键字:abstract类:用来描述一类具体的事物抽象类:抽象的、模糊的、不具体的类在Java的普通类中是不允许多继承的,原因是
- 1.super介绍我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。用于访问父类的属性,方法,构造器2.super