java 读取hadoop文件,用Java读取HDFS和本地文件

作者探讨了一种方法,通过检查文件路径的前缀来决定是使用HDFS还是本地文件系统,询问了是否有更高效的方式来处理HDFS和本地路径,以便于代码简洁。展示了如何从Path直接获取FileSystem,减少了条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I want to read file paths irrespective of whether they are HDFS or local. Currently, I pass the local paths with the prefix file:// and HDFS paths with the prefix hdfs:// and write some code as the following

Configuration configuration = new Configuration();

FileSystem fileSystem = null;

if (filePath.startsWith("hdfs://")) {

fileSystem = FileSystem.get(configuration);

} else if (filePath.startsWith("file://")) {

fileSystem = FileSystem.getLocal(configuration).getRawFileSystem();

}

From here I use the API's of the FileSystem to read the file.

Can you please let me know if there is any other better way than this?

解决方案

Does this make sense,

public static void main(String[] args) throws IOException {

Configuration conf = new Configuration();

conf.addResource(new Path("/hadoop/projects/hadoop-1.0.4/conf/core-site.xml"));

conf.addResource(new Path("/hadoop/projects/hadoop-1.0.4/conf/hdfs-site.xml"));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the file path...");

String filePath = br.readLine();

Path path = new Path(filePath);

FileSystem fs = path.getFileSystem(conf);

FSDataInputStream inputStream = fs.open(path);

System.out.println(inputStream.available());

fs.close();

}

You don't have to put that check if you go this way. Get the FileSystem directly from Path and then do whatever you feel like.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值