引入jar包
<properties>
<hadoop.version>2.7.1</hadoop.version>
</properties>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
连接Hadoop客户端,去下面的API工具类修改对象位置
1.SNN配置连接方式
1.application.yml
hdfs:
path: hdfs://node1:9000
username: root
2.Java API
@Value("${hdfs.path}")
private String path;
@Value("${hdfs.username}")
private String username;
public Configuration getConf() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", path);
return conf;
}
2.HA配置连接方式
1.application.yml
hdfs:
path: hdfs://yc #hdfs://node1:9000
username: root
nameservices: yc
namenodes: nn1,nn2
node1: node1:8020
node2: node2:8020
provider: org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider
2.Java API
@Value("${hdfs.path}")
private String path;
@Value("${hdfs.username}")
private String username;
@Value("${hdfs.nameservices}")
private String nameservices;
@Value("${hdfs.namenodes}")
private String namenodes;
@Value("${hdfs.node1}")
private String node1;
@Value("${hdfs.node2}")
private String node2;
@Value("${hdfs.provider}")
private String provider;
public Configuration getConf() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", path);
conf.set("dfs.nameservices", nameservices);
conf.set("dfs.ha.namenodes.yc", namenodes);
conf.set("dfs.namenode.rpc-address.yc.nn1", node1);
conf.set("dfs.namenode.rpc-address.yc.nn2", node2);
conf.set("dfs.client.failover.proxy.provider.yc", provider);
return conf;
}
API工具类
1.HdfsService
package com.yc.T1_API.service;
import org.apache.hadoop.fs.BlockLocation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
public interface HdfsService {
public Map<String, String> getConfInfoAsMap() throws InterruptedException, IOException, URISyntaxException;
public boolean mkdir(String path) throws InterruptedException, IOException, URISyntaxException;
public boolean existFile(String path) throws InterruptedException, IOException, URISyntaxException;
public List<Map<String, Object>> readPathInfo(String path) throws InterruptedException, IOException, URISyntaxException;
public void createFile(String path, MultipartFile file) throws IOException, URISyntaxException, InterruptedException;
public String readFile(String path) throws InterruptedException, IOException, URISyntaxException;
public List<Map<String, String>> listStatus(int type) throws InterruptedException, IOException, URISyntaxException;
public List<Map<String, String>> listStatus(String type) throws InterruptedException, IOException, URISyntaxException;
public Map<String, String> getFileInfo(String path) throws InterruptedException, IOException, URISyntaxException;
public List<Map<String, String>> listFile(String path) throws InterruptedException, IOException, URISyntaxException;
public boolean renameFile(String oldName, String newName) throws InterruptedException, IOException, URISyntaxException;
public boolean deleteFile(String path) throws InterruptedException, IOException, URISyntaxException;
public void uploadFile(String path, String uploadpath) throws InterruptedException, IOException, URISyntaxException;
public ResponseEntity<InputStreamResource> downloadFile(String path, String fileName) throws InterruptedException, IOException, URISyntaxException;
public ResponseEntity<byte[]> downloadDirectory(String path, String fileName) throws IOException;
public void copyFile(String sourcePath, String targetPath) throws InterruptedException, IOException, URISyntaxException;
public byte[] openFileToBytes(String path) throws InterruptedException, IOException, URISyntaxException;
public BlockLocation[] getFileBlockLocations(String path) throws InterruptedException, IOException, URISyntaxException;
}
2.HdfsServiceImpl
package com.yc.service;
import com.yc.util.MyFileUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.*;
import org.apache.zookeeper.common.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service
public class HdfsServiceImpl implements HdfsService {
@Value("${hdfs.path}")
private String path;
@Value("${hdfs.username}")
private String username;
@Value("${hdfs.nameservices}")
private String nameservices;
@Value("${hdfs.namenodes}")
private String namenodes;
@Value("${hdfs.node1}")
private String node1;
@Value("${hdfs.node2}")
private String node2;
@Value("${hdfs.provider}")
private String provider;
private final int bufferSize = 1024 * 1024 * 64;
public Configuration getConf() {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", path);
conf.set("dfs.nameservices", nameservices);
conf.set("dfs.ha.namenodes.yc", namenodes);
conf.set("dfs.namenode.rpc-address.yc.nn1", node1);
conf.set("dfs.namenode.rpc-address.yc.nn2", node2);
conf.set("dfs.client.failover.proxy.provider.yc", provider);
return conf;
}
public FileSystem getFileSystem() throws URISyntaxException, IOException, InterruptedException {
FileSystem fs = FileSystem.get(new URI(path), getConf(), username);
return fs;
}
@Override
public Map<String, String> getConfInfoAsMap() throws InterruptedException, IOException, URISyntaxException {
FileSystem fs = getFileSystem();
Configuration conf = getConf();
Iterator<Map.Entry<String, String>> ite = conf.iterator();
Map<String, String> map = new HashMap<>();
while (ite.hasNext()) {
Map.Entry<String, String> entry = ite.next();
map.put(entry.getKey(), entry.getValue());
}
return map;
}
@Override
public boolean existFile(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return false;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
return fs.exists(srcpath);
}
@Override
public boolean mkdir(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return false;
}
if (existFile(path)) {
return true;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
return fs.mkdirs(srcpath);
}
@Override
public List<Map<String, Object>> readPathInfo(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FileStatus[] statusList = fs.listStatus(srcpath);
List<Map<String, Object>> res = new ArrayList<>();
if (statusList != null && statusList.length > 0) {
for (FileStatus status : statusList) {
Map<String, Object> map = new HashMap<>();
map.put("filePath", status.getPath());
map.put("fileStatus", status.toString());
res.add(map);
}
return res;
}
return null;
}
@Override
public void createFile(String path, MultipartFile file) throws IOException, URISyntaxException, InterruptedException {
if (StringUtils.isEmpty(path) || file.getBytes() == null) {
return;
}
FileSystem fs = getFileSystem();
String fileName = file.getOriginalFilename();
Path newpath = null;
if ("/".equals(path)) {
newpath = new Path(path + fileName);
} else {
newpath = new Path(path + "/" + fileName);
}
FSDataOutputStream outputStream = fs.create(newpath);
outputStream.write(file.getBytes());
outputStream.close();
fs.close();
}
@Override
public String readFile(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FSDataInputStream inputStream = fs.open(srcpath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String lineTxt = "";
StringBuffer sb = new StringBuffer();
while ((lineTxt = reader.readLine()) != null) {
sb.append(lineTxt);
}
return sb.toString();
}
@Override
public List<Map<String, String>> listFile(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
RemoteIterator<LocatedFileStatus> filesList = fs.listFiles(srcpath, true);
List<Map<String, String>> resList = new ArrayList<>();
while (filesList.hasNext()) {
LocatedFileStatus next = filesList.next();
Map<String, String> map = new HashMap<>();
map.put("fileName", next.getPath().getName());
map.put("filePath", next.getPath().toString());
resList.add(map);
}
fs.close();
return resList;
}
@Override
public Map<String, String> getFileInfo(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FileStatus fileStatus = fs.getFileStatus(srcpath);
return fileStatusToMap(fileStatus);
}
@Override
public boolean renameFile(String oldName, String newName) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
return false;
}
FileSystem fs = getFileSystem();
Path oldpath = new Path(oldName);
Path newpath = new Path(newName);
return fs.rename(oldpath, newpath);
}
@Override
public boolean deleteFile(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return false;
}
if (!existFile(path)) {
return false;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
return fs.delete(srcpath, true);
}
@Override
public void uploadFile(String path, String uploadpath) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path) || StringUtils.isEmpty(uploadpath)) {
return;
}
FileSystem fs = getFileSystem();
Path oldpath = new Path(path);
Path newpath = new Path(uploadpath);
fs.copyFromLocalFile(false, oldpath, newpath);
fs.close();
}
@Override
public ResponseEntity<InputStreamResource> downloadFile(String path, String fileName) throws InterruptedException, IOException, URISyntaxException {
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FSDataInputStream in = fs.open(srcpath);
return MyFileUtil.downloadFile(in,fileName);
}
public Map<String, String> fileStatusToMap(FileStatus fileStatus) {
Map<String, String> map = new HashMap<>();
Path path = fileStatus.getPath();
map.put("fileName", path.getName());
map.put("filePath", path.toUri().toString());
map.put("relativePath", path.toUri().toString().substring(this.path.length()));
map.put("parenPath", path.getParent().toUri().toString().substring(this.path.length()));
map.put("owner", fileStatus.getOwner());
map.put("group", fileStatus.getGroup());
map.put("isFile", fileStatus.isFile() + "");
map.put("duplicates", fileStatus.getReplication() + "");
map.put("size", MyFileUtil.formatFileSize(fileStatus.getLen()));
map.put("rights", fileStatus.getPermission().toString());
map.put("modifyTime", MyFileUtil.formatTime(fileStatus.getModificationTime()));
return map;
}
public void search(Path srcpath, List<Map<String, String>> list, Pattern pattern) throws InterruptedException, IOException, URISyntaxException {
FileSystem fs = getFileSystem();
FileStatus[] fileStatuses = fs.listStatus(srcpath);
if (fileStatuses != null && fileStatuses.length > 0) {
for (FileStatus fileStatus : fileStatuses) {
boolean res = fileStatus.isFile();
if (!res) {
search(fileStatus.getPath(), list, pattern);
} else {
boolean bool = pattern.matcher(fileStatus.getPath().getName()).find();
if (bool) {
Map<String, String> map = this.fileStatusToMap(fileStatus);
list.add(map);
}
}
}
}
}
@Override
public List<Map<String, String>> listStatus(int type) throws InterruptedException, IOException, URISyntaxException {
String path = "/";
Path srcpath = new Path(path);
List<Map<String, String>> resList = new ArrayList<>();
String reg = null;
if (type == 1) {
reg = ".+(.jpeg|.jpg|.png|.bmp|.gif)$";
} else if (type == 2) {
reg = ".+(.txt|.rtf|.doc|.docx|.xls|.xlsx|.html|.htm|.xml)$";
} else if (type == 3) {
reg = ".+(.mp4|.avi|.wmv)$";
} else if (type == 4) {
reg = ".+(.mp3|.wav)$";
} else if (type == 5) {
reg = "^\\S+\\.*$";
}
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
search(srcpath, resList, pattern);
return resList;
}
@Override
public List<Map<String, String>> listStatus(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FileStatus[] fileStatuses = fs.listStatus(srcpath);
if (fileStatuses == null || fileStatuses.length <= 0) {
return null;
}
List<Map<String, String>> resList = new ArrayList<>();
for (FileStatus fileStatus : fileStatuses) {
Map<String, String> map = fileStatusToMap(fileStatus);
resList.add(map);
}
fs.close();
return resList;
}
public void compress(String baseDir, ZipOutputStream zipOutputStream, FileSystem fs) {
try {
FileStatus[] fileStatuses = fs.listStatus(new Path(baseDir));
String[] strs = baseDir.split("/");
String lastName = strs[strs.length - 1];
for (int i = 0; i < fileStatuses.length; i++) {
String name = fileStatuses[i].getPath().toString();
name = name.substring(name.indexOf("/" + lastName));
if (fileStatuses[i].isFile()) {
Path path = fileStatuses[i].getPath();
FSDataInputStream in = fs.open(path);
zipOutputStream.putNextEntry(new ZipEntry(name.substring(1)));
IOUtils.copyBytes(in, zipOutputStream, this.bufferSize);
in.close();
} else {
zipOutputStream.putNextEntry(new ZipEntry(fileStatuses[i].getPath().getName() + "/"));
compress(fileStatuses[i].getPath().toString(), zipOutputStream, fs);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public ResponseEntity<byte[]> downloadDirectory(String path, String fileName) throws IOException {
ByteArrayOutputStream out = null;
try {
FileSystem fs = this.getFileSystem();
out = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(out);
compress(path, zos, fs);
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
byte[] bs = out.toByteArray();
out.close();
return MyFileUtil.downloadFile(in, fileName);
}
@Override
public void copyFile(String sourcePath, String targetPath) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(sourcePath) || StringUtils.isEmpty(targetPath)) {
return;
}
FileSystem fs = getFileSystem();
Path oldpath = new Path(sourcePath);
Path newpath = new Path(targetPath);
FSDataInputStream in = null;
FSDataOutputStream out = null;
try {
in = fs.open(oldpath);
out = fs.create(newpath);
IOUtils.copyBytes(in, out, bufferSize, false);
} finally {
in.close();
out.close();
fs.close();
}
}
@Override
public byte[] openFileToBytes(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
byte[] res = null;
try {
FSDataInputStream in = fs.open(srcpath);
InputStream iss = in.getWrappedStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bs = new byte[10 * 1024];
int len = 0;
while ((len = iss.read(bs, 0, bs.length)) != -1) {
baos.write(bs, 0, len);
}
baos.flush();
res = baos.toByteArray();
} finally {
fs.close();
}
return res;
}
@Override
public BlockLocation[] getFileBlockLocations(String path) throws InterruptedException, IOException, URISyntaxException {
if (StringUtils.isEmpty(path)) {
return null;
}
if (!existFile(path)) {
return null;
}
FileSystem fs = getFileSystem();
Path srcpath = new Path(path);
FileStatus fileStatus = fs.getFileStatus(srcpath);
return fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
}
}
3.MyFileUtil
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyFileUtil {
public static String formatTime(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:sss");
Date date = new Date(time);
return simpleDateFormat.format(date);
}
public static String formatFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongSize = "0B";
if (fileS == 0) {
return wrongSize;
}
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "MB";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "GB";
}
return fileSizeString;
}
public static ResponseEntity<InputStreamResource> downloadFile(InputStream in, String fileName) {
try {
byte[] bytes = new byte[in.available()];
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");
httpHeaders.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
httpHeaders.add("Pragma", "no-cache");
httpHeaders.add("Expires", "0");
httpHeaders.add("Content-Language", "UTF-8");
return ResponseEntity.ok().headers(httpHeaders).contentLength(bytes.length)
.contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(in));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ResponseEntity<byte[]> downloadDirectory(byte[] bytes, String fileName) {
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Cache-Control", "no-cache, no-store, must-revalidate");
httpHeaders.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
httpHeaders.add("Pragma", "no-cache");
httpHeaders.add("Expires", "0");
httpHeaders.add("Content-Language", "UTF-8");
return ResponseEntity.ok().headers(httpHeaders).contentLength(bytes.length)
.contentType(MediaType.parseMediaType("application/octet-stream")).body(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getFileName() {
Date d = new Date();
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(d) + ".zip";
}
}