java访问HDFS文件系统

package com.db.hadoop.hdfs;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.apache.log4j.Logger;


public class HDFSUtil {
private static final Logger LOG = Logger.getLogger(HDFSUtil.class);  //日志记录器对象
private static FileSystem fileSystem;   //文件系统对象

static{ 
Configuration conf = new Configuration();  //hadoop的配置文件对象
URI uri = URI.create("hdfs://master:9000/");  //hadoop分布式文件系统master的uri
try {
fileSystem = FileSystem.get(uri, conf, "hadoop"); //根据配置个远程地址创建文件系统
LOG.debug("创建文件系统对象成功。。。。。。。。");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
LOG.error("创建文件系统对象失败。。。。。。。。。");

}


/*查询操作
*打开指定上的文件,导出内容到指定位置
*@param inHdfsPath:Hdfs上的文件
*@param out:导出的位置,当为null时在控制台输出
*/
public static void readFile(String inHdfsPath, OutputStream out){
FSDataInputStream in = null;
try {
in = fileSystem.open(new Path(inHdfsPath));
LOG.debug("打开文件系统上的文件成功。。。。。");
} catch (IOException | IllegalArgumentException e) {
LOG.error("打开文件系统上的文件失败。。。。。",e);
throw new RuntimeException("打开文件系统上的文件失败.......");
}

try {
IOUtils.copyBytes(in, out, 4096, true);
LOG.debug("拷贝文件数据流成功。。。。。。。");
}catch (IOException e) {
LOG.error("拷贝文件数据流失败。。。。。。。");
}
}


/*取到文件系统上的文件数据信息
*@param inHdfsPath:HDFS上的文件
*@return 返回元数据信息对象
*/
public static FileStatus getFileStatus(String isHdfsPath){
try {
return fileSystem.getFileStatus(new Path(isHdfsPath));
} catch (IOException | IllegalArgumentException e) {
LOG.error("取到文件系统上面的文件信息失败",e);
throw new RuntimeException("取到文件系统上面的文件信息失败......");

}


/*取到文件系统上的文件元数据信息的具体详情
*@param inHdfsPath:Hdfs上的文件
*@return 返回元数据信息的具体详情对象
*/
public static BlockLocation[] getBlockLocation(String inHdfsPath){
FileStatus fileStatus = getFileStatus(inHdfsPath);
try {
return fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
} catch (IOException e) {
LOG.error("取到文件系统上面的文件详情信息失败",e);
throw new RuntimeException("取到文件系统上面的文件详情信息失败.......");
}

}


/*创建文件
*@param dirPath:要创建的文件的目录
*@return 是否创建成功
*/
public static boolean createDir(String dirPath){
try {
if(fileSystem.exists(new Path(dirPath))){
LOG.debug("在文件系统中存在该目录");
return true;
}else{
//默认目录权限755 ,默认文件权限是644
return fileSystem.mkdirs(new Path(dirPath));
}
} catch (IOException | IllegalArgumentException e) {
LOG.error("在文件系统中创建目录失败",e);
throw new RuntimeException("在文件系统中创建目录失败.....");
}
}


/*删除文件
*@param filePath:要删除的文件
*@param recursive:要删除的目录文件
*@return 是否成功
*/
public static boolean del(String filePath, boolean recursive){
try {
return fileSystem.delete(new Path(filePath), recursive);
} catch (IOException | IllegalArgumentException e) {
LOG.error("删除文件系统上面的文件失败",e);
throw new RuntimeException("删除文件系统上面的文件失败......");

}


/*保存文件
*@param saveFilePath:保存文件的路径
*@param content:要保存的内容
*@param isAppend:是否追加 
*return 是否成功
*/
public static boolean save(String saveFilePath, String content, boolean isAppend){
FSDataOutputStream out = null;
try {
if(fileSystem.exists(new Path(saveFilePath))){
//文件存在,追加内容
out = fileSystem.append(new Path(saveFilePath), 4096, new Progressable(){
@Override
public void progress() {
System.out.println(">>");

}
});
}else{
//文件不存在
out = fileSystem.create(new Path(saveFilePath), true, 4096, new Progressable(){
@Override
public void progress() {
System.out.println("....");

}

});
}
ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
IOUtils.copyBytes(in, out, 4096, true);
System.out.println("写入数据完成........");
return true;
} catch (IOException | IllegalArgumentException e) {
LOG.error("保存到数据文件系统上失败",e);
throw new RuntimeException("保存到数据文件系统上失败.....");
}

}


/*从文件系统下载文件
*@param srcPath:要下载的文件
*@param dstPath:要下载到的文件系统路径
*@return 是否成功
*/
public static boolean download(String srcPath, String dstPath){
try {
fileSystem.copyToLocalFile(new Path(srcPath), new Path(dstPath));
return true;
} catch (IOException | IllegalArgumentException e) {
LOG.error("从文件系统上下载文件失败",e);
throw new RuntimeException("从文件系统上下载文件失败.....");
}
}


/*上传文件到文件系统
*@param srcPath:要上传的文件
*@param dstPath:要上传到的文件系统路径
*@param overwrite:是否覆盖文件
*@return 是否成功
*/
public static boolean upload(String srcPath,String dstPath,boolean overwrite){
try {
fileSystem.copyFromLocalFile(new Path(srcPath), new Path(dstPath));
return true;
} catch (IOException | IllegalArgumentException e) {
LOG.error("上传文件到文件系统上失败",e);
throw new RuntimeException("上传文件到文件系统上失败.....");
}

}


/*字节数据的单位转换
*@param num 字节数大小
*@return 带单位的字节数大小
*/
public static String showSize(double num){
//  B 1024KB  1024MB  1024GB  1024PB  1024ZB  1024EB
if(num < Math.pow(1024, 1)){
return num + "B";
}else if(num > Math.pow(1024, 1) && num < Math.pow(1024, 2)){
return num / Math.pow(1024, 1) + "KB";
}else if(num > Math.pow(1024, 2) && num < Math.pow(1024, 3)){
return num / Math.pow(1024, 2) + "MB";
}else if(num > Math.pow(1024, 3) && num < Math.pow(1024, 4)){
return num / Math.pow(1024, 3) + "GB";
}else if(num > Math.pow(1024, 4) && num < Math.pow(1024, 5)){
return num / Math.pow(1024, 4) + "PB";
}else if(num > Math.pow(1024, 5) && num < Math.pow(1024, 6)){
return num / Math.pow(1024, 5) + "ZB";
}else if(num > Math.pow(1024, 6) && num < Math.pow(1024, 7)){
return num / Math.pow(1024, 6) + "EB";
}else{
return "很大很大很大";
}
}


/*打印文件系统上面的文件信息
*@param isHdfsPath:文件的路径
*/
public static FileStatus showFileStatus(String isHdfsPath){
FileStatus fileinfo = getFileStatus(isHdfsPath);
System.out.println(fileinfo);
System.out.println("文件大小:" + showSize(fileinfo.getLen()));
System.out.println("问价是否为目录:" + fileinfo.isDirectory());
System.out.println("文件的相同份数:" + fileinfo.getReplication()); //文件备份数最多为512个
System.out.println("文件的拆分的块的大小:" + fileinfo.getBlockSize()); 
System.out.println("文件的最后修改时间:" + showDate(fileinfo.getModificationTime()));
System.out.println("文件的操作权限:" + fileinfo.getPermission());
System.out.println("文件的拥有者:" + fileinfo.getOwner());
System.out.println("文件的所属组:" + fileinfo.getGroup());
try {
System.out.println("文件的关联:" + (fileinfo.isSymlink() ? fileinfo.getSymlink() : "没有关联文件"));
} catch (IOException e) {
e.printStackTrace();
}
return fileinfo;
}


/*转换毫秒数成日期字符串
*@param time 毫秒数
*@return 日期字符串
*/
public static String showDate(long time){
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值