本篇博客记录Java API操作HDFS过程
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by macan on 2017/1/14.
*
* hdfs Java API Operation
*/
public class HdfsConnection {
private FileSystem fs = null;
public HdfsConnection() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master1:9000");
fs = FileSystem.get(new URI("hdfs://master1:9000"), conf, "root");
}
/**
* 删除HDFS 中指定的目录
*
* @param path 需要删除的目录
* @param is 是否进行递归删除文件
* @throws IOException
*/
public boolean delete(String path, boolean is) throws IOException {
boolean res = true;
if (exits(path)) {
res = fs.delete(new Path(path), is);
}
return res;
}
/**
* 查看文件是否存在
*
* @param path 文件路径
* @return 如果文件存在,返回true,否则返回false
* @throws IOException
*/
public boolean exits(String path) throws IOException {
boolean res = fs.exists(new Path(path));
return res;
}
/**
* 创建一个文件
* @param path 文件的绝对路径
* @param isOverride 如果文件已经存在,是否覆盖创建是否覆盖
* @return 返回写文件流
* @throws IOException
*/
public FSDataOutputStream touch(String path, boolean isOverride) throws IOException {
if (exits(path)) {
return null;
}
return fs.create(new Path(path), isOverride);
}
/**
* 创建一个新的文件,并且写入内容
* @param path 文件的绝对路径
* @param isOverride 当文件已经存在的时候,是否覆盖
* @param contex 写入的内容
* @return 如果写入成功,返回true,否则返回false
* @throws IOException
*/
public boolean touchAndWrite(String path ,boolean isOverride, String contex) throws IOException {
FSDataOutputStream outputStream = touch(path, isOverride);
if (outputStream == null){
return false;
}
outputStream.write(contex.getBytes(), 0, contex.length());
return true;
}
/**
* 创建目录
* @param path 带创建目录的路径
* @param model 权限
* @return 创建成功返回true 否则返回false
* @throws IOException
*/
public boolean mkdir(String path, String model) throws IOException {
if (exits(path)){
System.out.printf("path has been exited!");
return false;
}
FsPermission permission = new FsPermission(model);
return fs.mkdirs(new Path(path), permission);
}
/**
* 以默认全选创建目录
* @param path 待创建文件的目录
* @return 创建成功返回true,否则返回false
* @throws IOException
*/
public boolean mkdir(String path) throws IOException {
return mkdir(path, null);
}
/**
* 更改文件名
* @param path 文件所在路径
* @param newName 新名字
* @param oldName 旧名
* @return
* @throws IOException
*/
public boolean rename(String path, String newName, String oldName) throws IOException {
if (!exits(path)){
System.out.println("file is not exits!");
return false;
}
fs.renameSnapshot(new Path(path), newName, oldName);
return true;
}
/**
* 根据新旧路径进行更名
* @param oldPathName 原始文件路径
* @param newPathName 现在文件路径
* @return 成功返回true, 失败返回false
* @throws IOException
*/
public boolean rename(String oldPathName, String newPathName) throws IOException {
if (!exits(oldPathName)){
System.out.println("file is not exits.");
return false;
}
fs.rename(new Path(oldPathName), new Path(newPathName));
return true;
}
/**
* 上传文件到hdfs
* @param localPath 本地文件存储路径
* @param srcPath hdfs上路径
* @return
*/
public boolean upload(String localPath, String srcPath){
try {
fs.copyFromLocalFile(new Path(localPath), new Path(srcPath));
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
/**
* 下载hdfs文件到本地
* @param localPath 本地文件保存路径
* @param srcPath hdfs上文件路径路径
* @return
*/
public boolean download(String localPath, String srcPath){
try {
fs.copyToLocalFile(new Path(srcPath), new Path(localPath));
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
//读取hdfs目录上的文件
public FileStatus[] list(String path) throws IOException {
//获取文件列表
return fs.listStatus(new Path(path));
}
/**
* 读取指定hdfs目录上所有文件,并打印在终端
* @param path 指定目录
* @return
* @throws IOException
*/
public void listAndPrint(String path) throws IOException {
if (!exits(path)){
System.out.println("path not exits");
}
//获取文件列表
FileStatus[] status = fs.listStatus(new Path(path));
for (int i = 0; i < status.length; ++i){
System.out.println(status[i].getPath().toString());
}
}
/**
* 通过流的方式,写数据到HDFS
* @param frompath 本地文件路径
* @param topath HDFS路径
* @throws IOException
*/
public void uploadWithStream(String frompath, String topath) throws IOException {
//HDFS上的文件流
FSDataOutputStream outputStream = fs.create(new Path(topath), true);
//本地读取的文件流
FileInputStream inputStream = new FileInputStream(frompath);
//将输入文件流写到输出文件流
IOUtils.copy(inputStream, outputStream);
}
/**
* 通过流,将数据从HDFS中将数据写到本地文件
* @param frompath HDFS路径
* @param topath 本地路径
* @throws IOException
*/
public void downloadWithStream(String frompath, String topath, int size)
throws IOException {
FSDataInputStream inputStream = fs.open(new Path(frompath), size);
FileOutputStream outputStream = new FileOutputStream(topath);
IOUtils.copy(inputStream, outputStream);
}
/**
* 通过指定偏移量,读取HDFS中的文件
* @param frompath HDFS中的文件路径
* @param topath 本地存放路径
* @param offset 偏移量
* @throws IOException
*/
public void streamReadWithOffset(String frompath, String topath, int offset, int size)
throws IOException {
FSDataInputStream inputStream = fs.open(new Path(frompath), size);
inputStream.seek(offset);
FileOutputStream outputStream = new FileOutputStream(topath);
IOUtils.copy(inputStream, outputStream);
}
/**
* 获取HDFS上的文件,输出到终端
* @param frompath HDFS中的文件路径
* @throws IOException
*/
public void streamPrintInTerminal(String frompath, int size) throws IOException {
FSDataInputStream inputStream = fs.open(new Path(frompath), size);
IOUtils.copy(inputStream, System.out);
}
}