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
投稿
猜你喜欢
- 本文实例为大家分享了C语言自定义扫雷游戏的具体代码,供大家参考,具体内容如下实现过程对于用C语言实现扫雷游戏得实现,可将游戏过程分为两个板块
- 在传统的单服务架构中,一般来说,只有一个服务器,那么不存在 Session共享问题,但是在分布式/集群项目中,Session 共享则是一个必
- 本文实例为大家分享了java实现计算器功能具体代码,供大家参考,具体内容如下效果图组成结构从结构上来说,一个简单的图形界面,需要由界面组件、
- 背景最近在研究搭建spring源码调试环境时,接触到到gradle项目构建工具。由于之前习惯于maven项目的构建,故通过此文记录相关gra
- 在使用java项目时,如果没有详细的管理和辅助流程,就会像程序失去了系统的调配一样。在java中有一种专门管理项目的工具,叫做maven,除
- 我们再用支付宝支付的时候,会从底部弹上来一个对话框,让我们选择支付方式等等,今天我们就来慢慢实现这个功能效果图实现主界面很简单,就是一个按钮
- 代理模式:为其他对象提供一种代理以控制某个对象的访问。用在:在某些情况下,一个客户不想或者不能直接访问另一个对象,而代理对象可以在客户端和目
- 下面的方法返回false表示网络不通// 检测网络 public static boolean checkNetworkAvailable(
- #region 监视文件夹的变化
- 背景我们经常在网上下载一些视频教程,然而这些视频命名规则各不相同,即使对于相同类型的文件名来说,当文件数量很大且文件名全部是中文时,文件排序
- 我们平时使用的一些常见队列都是非阻塞队列,比如PriorityQueue、LinkedList(LinkedList是双向链表,它实现了De
- 问题描述在进行flutter项目开发时,我们常常会碰见我们在执行flutter run指令的情况下,出现资源下载过慢的问题,最终导致下载失败
- 本文实例为大家分享了C#实现控制台飞行棋小游戏的具体代码,供大家参考,具体内容如下using System;using System.Col
- 两种方式:1. String str = "123 456 789 111";String [] strArray =
- 前言本文主要介绍了关于java结合keytool实现非对称签名和验证的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍
- springboot 异常与重定向在spring中,有两个重定向类型:301,永久性跳转302,暂时性跳转默认调用302。1.下面先通过一个
- 在我们开发SpringBoot后端服务时,一般需要给前端统一响应格式,方便前端调试及配置错误提示等等。这篇文章讲讲实际工作中统一响应格式及统
- Map接口简介Map接口是一种双列集合,它的每个元素都包含一个键对象Key和值对象Value,键和值对象之间存在一种对应关系,称为映射。从M
- 本文实例讲述了Android图片压缩工具类。分享给大家供大家参考,具体如下:这里共享一个图片压缩工具类:package com.sanwei
- 前言本篇文章讲的是Kotlin 自定义view之实现标尺控件Ruler,以选择身高、体重等。开发中,当我们需要获取用户的身高和体重等信息时,