gpt4 book ai didi

Java Hadoop FileSystem 对象到 File 对象

转载 作者:可可西里 更新时间:2023-11-01 16:52:23 27 4
gpt4 key购买 nike

我有一个将文件上传到 S3 的 Java 代码示例

 File f = new File("/home/myuser/test");

TransferManager transferManager = new TransferManager(credentials);
MultipleFileUpload upload = transferManager.uploadDirectory("mybucket","test_folder",f,true);

我实际上想从 HDFS 上传到 S3。我不想做任何复杂的事情,所以我想知道我是否可以使用我已有的代码。那么有没有办法将Hadoop FileSystem 对象转换为File 对象呢?像这样:

FileSystem fs = ... // file system from hdfs path
File f = fs.toFile()

谢谢,塞尔维亚语

最佳答案

如果要使用 File 类,除了将 HDFS 文件下载到本地文件系统之外别无他法。原因是 File 只能表示硬盘上的本地文件。但是,从 Java 7 开始,您可以使用 Path 对象获取 HDFS 文件的输入流:

Configuration conf = new Configuration
// set the hadoop config files
conf.addResource(new Path("HADOOP_DIR/conf/core-site.xml"));
conf.addResource(new Path("HADOOP_DIR/conf/hdfs-site.xml"));

Path path = new Path("hdfs:///home/myuser/test")
FileSystem fs = path.getFileSystem(conf);
FSDataInputStream inputStream = fs.open(path)
// do what ever you want with the stream

fs.close();

关于Java Hadoop FileSystem 对象到 File 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com