将目录从本地系统复制到hdfs java代码

ix0qys7i  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(339)

我在尝试使用java代码将目录从本地系统复制到hdfs时遇到问题。我可以移动单个文件,但无法找到一种方法来移动包含子文件夹和文件的整个目录。有人能帮我吗?提前谢谢。

igsr9ssn

igsr9ssn1#

就用这个 FileSystem 的copyfromlocalfile方法。如果源路径是本地目录,则会将其复制到hdfs目标:

...
Configuration conf = new Configuration();
conf.addResource(new Path("/home/user/hadoop/conf/core-site.xml"));
conf.addResource(new Path("/home/user/hadoop/conf/hdfs-site.xml"));

FileSystem fs = FileSystem.get(conf);
fs.copyFromLocalFile(new Path("/home/user/directory/"), 
  new Path("/user/hadoop/dir"));
...
zu0ti5jz

zu0ti5jz2#

下面是读写hdfs的完整工作代码。这需要两个论点
输入路径(本地/hdfs)
输出路径(hdfs)
我用了cloudera沙盒。

package hdfsread;

 import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;

 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.IOUtils;

 public class ReadingAFileFromHDFS {

     public static void main(String[] args) throws IOException {
         String uri = args[0];
         InputStream in = null;
         Path pt = new Path(uri);
         Configuration myConf = new Configuration();
         Path outputPath = new Path(args[1]);

         myConf.set("fs.defaultFS","hdfs://quickstart.cloudera:8020");
         FileSystem fSystem = FileSystem.get(URI.create(uri),myConf);
         OutputStream os = fSystem.create(outputPath);
         try{
             InputStream is = new BufferedInputStream(new FileInputStream(uri));
             IOUtils.copyBytes(is, os, 4096, false);
         }
         catch(IOException e){
             e.printStackTrace();
         }
         finally{
             IOUtils.closeStream(in);
         }
     }
}

相关问题