Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: HDFS copy directory

Tags:

java

hadoop

hdfs

Is there a simple way to copy a HDFS directory to another directory in Java?

For example, how would I move the contents of /user/abc/pudding to /user/def/pudding?

I'm looking for some HDFS equivalent to UNIX's cp command which I can do programmatically with Java.

Note: I'm aware of FileSystem but it only seems to allow me to copy from my local machine to HDFS?

like image 208
user3177168 Avatar asked Apr 25 '26 12:04

user3177168


1 Answers

Try one of the copy methods in FileUtil. For example:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
Configuration conf = new Configuration();  // if necessary
FileSystem fileSystem = FileSystem.get(conf);  // if necessary

FileUtil.copy(
    fileSystem, new Path("/path/to/src"),
    fileSystem, new Path("/path/to/dst"),
    false,  // move if true
    conf
);
like image 80
Paul M Avatar answered Apr 27 '26 01:04

Paul M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!