当前位置: 代码迷 >> java >> Java Hadoop FileSystem对象到File对象
  详细解决方案

Java Hadoop FileSystem对象到File对象

热度:57   发布时间:2023-07-25 19:57:30.0

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

 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();
  相关解决方案