package com.xhao.hadoop.util;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@RestController
public class HadoopUtils {
@Autowired
private FileSystem fileSystem;
/**
* 创建文件夹
*
* @param path
* @return
* @throws Exception
*/
@PostMapping("mkdir")
public boolean mkdir(String path) throws Exception {
boolean flag = fileSystem.mkdirs(new Path(path));
return flag;
}
/**
* 新建文件,遇到同名的直接覆盖
*
* @param path
* @param content
* @throws Exception
*/
@PostMapping("create")
public void create(String path, String content) throws Exception {
FSDataOutputStream out = fileSystem.create(new Path(path),
new Progressable() {
public void progress() {
System.out.println('+');
}
});
out.write(content.getBytes());
out.flush();
out.close();
}
@PostMapping("cat")
public void cat() throws Exception {
FSDataInputStream in = fileSystem.open(new Path("/testJavaApi/hello.txt"));
IOUtils.copyBytes(in, System.out, 1024);
}
/**
* @param oldName 全路径 /test/olename.txt
* @param newName
* @throws Exception
*/
@PostMapping("rename")
public void rename(String oldName, String newName) throws Exception {
Path oldPath = new Path(oldName);
Path newPath = new Path(newName);
boolean isSuccess = fileSystem.rename(oldPath, newPath);
System.out.println(isSuccess);
}
/*
* 上传文件到hdfs
*/
@PostMapping
public void copyFromLocal(String source, String dest) throws Exception {
Path localPath = new Path(source);
Path hdfsPath = new Path(dest);
fileSystem.copyFromLocalFile(localPath, hdfsPath);
}
/*
* 上传文件到hdfs,输入输出流
*/
@PostMapping("copyFromLocalwithPro")
public void copyFromLocalwithPro(String filePath) throws Exception {
Path hdfsPath = new Path("/testJavaApi/tongyi.exe");
InputStream in = new BufferedInputStream(
new FileInputStream(new File("D:\\EasyConnectInstaller.exe")));
FSDataOutputStream out = fileSystem.create(hdfsPath, new Progressable() {
public void progress() {
System.out.println("=");
}
});
IOUtils.copyBytes(in, out, 2048);
IOUtils.closeStream(out);
IOUtils.closeStream(in);
}
/*
* 下载文件到本地
*/
public void copyToLocalFile() throws Exception {
Path localPath = new Path("D:\\hello1.txt");
Path hdfsPath = new Path("/testJavaApi/hello.txt");
fileSystem.copyToLocalFile(hdfsPath, localPath);
}
public void delete() throws Exception {
Path f = new Path("/testJavaApi/aaa.txt");
fileSystem.delete(f, true);
}
public void listFile() throws Exception {
Path f = new Path("/testJavaApi");
FileStatus[] files = fileSystem.listStatus(f);
for (FileStatus file : files) {
String isDir = file.isDirectory() ? "文件夹" : "文件";
short rep = file.getReplication();
long len = file.getLen();
Path path = file.getPath();
System.out.println(isDir + "\t" + rep + "\t" + len + "\t" + path);
}
}
//下载第一块block,只拷贝128M
public void getBlock0() throws Exception, Exception {
//获取输入流
FSDataInputStream fin = fileSystem.open(new Path("/hadoop-2.7.7.tar.gz"));
//获取输出流
FileOutputStream fout = new FileOutputStream(new File("e:/hadoop.tar.gz"));
//循环读取128M
byte[] buf = new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
fin.read(buf);
fout.write(buf);
}
fout.close();
fin.close();
}
//下载第二块block
public void getBlock1() throws Exception, Exception {
//获取输入流
FSDataInputStream fin = fileSystem.open(new Path("/hadoop-2.7.7.tar.gz"));
//获取输出流
FileOutputStream fout = new FileOutputStream(new File("e:/hadoop.tar.gz.part1"));
//指定读取的起点
fin.seek(1024 * 1024 * 128);
IOUtils.copyBytes(fin, fout, new Configuration());
fout.close();
fin.close();
}
}
hadoop Java API
版权申诉
94 浏览量
2022-06-30
15:23:05
上传
评论
收藏 12KB ZIP 举报
xhaoDream
- 粉丝: 19
- 资源: 17
最新资源
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- sdk.config
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
- 基于matlab实现配电网潮流 经典33节点 前推回代法潮流计算 回代电流 前推电压 带注释.rar
- 基于matlab实现模拟退火遗传算法的车辆调度问题研究,用MATLAB语言加以实现.rar
- 基于matlab实现蒙特卡洛的的移动传感器节点定位算法仿真代码.rar
- 华中数控系统818用户说明书
- 基于matlab实现卡尔曼滤波器完成多传感器数据融合 对多个机器人的不同传感器数据进行融合估计足球精确位置.rar
- 基于matlab实现进行简单车辆识别-车辆检测.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈