HDFS JAVA API相关的操作方法

import java.io.File;
import java.io.FileInputStream;

import org.apache.hadoop.conf.Configuration;
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;


/*包含了HDFS的基本操作*/
public class HdfsApp {
/**
* 创建文件系统*/
public static FileSystem getFileSystem() throws Exception{
Configuration conf = new Configuration();
// conf.set("fs.defaultFS", "hdfs://myhadoop:9000");
//创建一个文件系统
FileSystem fileSystem = FileSystem.get(conf);
return fileSystem;
}
/**
* 读文件*/
public static void readFile(String fileName) throws Exception{
FileSystem fileSystem = getFileSystem();
//构造一个路径对象
Path path = new Path(fileName);
//构造输入流对象FSDataInputStream
FSDataInputStream fsDataInputStream = fileSystem.open(path);
try{
//这里使用的是System.out的PrintStream输出流对象
IOUtils.copyBytes(fsDataInputStream, System.out, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(fsDataInputStream);
}
}
/**
* 写文件
* @param 文件名称*/
public static void writeFile(String sourceFile,String targetFile) throws Exception{
FileSystem fileSystem = getFileSystem();
Path path = new Path(targetFile);
FSDataOutputStream fsDataOutputStream = fileSystem.create(path);
//构造输入流对象
FileInputStream inputStream = new FileInputStream(new File(sourceFile));
try{
IOUtils.copyBytes(inputStream, fsDataOutputStream, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(fsDataOutputStream);
IOUtils.closeStream(inputStream);
System.out.println("write completed");
}
}
/**带有通配符的文件操作===通过通配符实现目录筛选
* @param 带通配符的文件路径名称
* 此外还可以通过自定义一个类实现PathFilter接口,然后在fileSystem的globStatus中传入该参数即可,
* 这样就可以留下与过滤器不匹配的文件
* */
public static void getDispInfo(String dispFileName) throws Exception{
FileSystem fileSystem = getFileSystem();
FileStatus[] fileStatus = fileSystem.globStatus(new Path(dispFileName));
//注意:FileStatus对象封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及相关的权限信息等
for(FileStatus status : fileStatus){
if(fileSystem.exists(status.getPath())){
System.out.println("存在");
System.out.println("文件路径: "+status.getPath());
System.out.println("文件备份数: "+status.getReplication());
}
else{
System.out.println("不存在");
}
}
}
/**
* 使用PathFilter实现的通配符文件匹配
* */
public static void getFileByPathFilter(String filePath) throws Exception{
FileSystem fileSystem = getFileSystem();
FileStatus[] fileStatus = fileSystem.globStatus(new Path(filePath), new MyPathFilter(".*t"));
//注意:FileStatus对象封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及相关的权限信息等
for(FileStatus status : fileStatus){
if(fileSystem.exists(status.getPath())){
System.out.println("存在");
System.out.println("文件路径: "+status.getPath());
System.out.println("文件备份数: "+status.getReplication());
}
else{
System.out.println("不存在");
}
}
}
public static void main(String[] args) throws Exception {
//FileSystem fileSystem = getFileSystem();
//String sourceFile = "E:\\大数据包\\data_test\\sourceFile.txt";
//String targetFile = "hdfs://myhadoop:9000/targetFile.txt";
/*String fileName = "/wordcount/input/wc.txt";
readFile(fileName);*/
/*String disInputStr = "hdfs://myhadoop:9000/wordcount/outspark/part*";
getDispInfo(disInputStr);*/
String myPathFilterStr = "hdfs://myhadoop:9000/wordcount/input/*";
getFileByPathFilter(myPathFilterStr);
}
}

/*自定义的PathFilter类*/
public class MyPathFilter implements PathFilter{
private final String regex;
public MyPathFilter(String regex){
this.regex = regex;
}
public boolean accept(Path path) {
// TODO Auto-generated method stub
//留下与正则表达式相匹配的文件
return path.toString().matches(regex);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值