Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying directory from local system to hdfs java code

Tags:

java

hadoop

hdfs

I'm having a problem trying to copy a directory from my local system to HDFS using java code. I'm able to move individual files but can't figure out a way to move an entire directory with sub-folders and files. Can anyone help me with that? Thanks in advance.

like image 623
Chirag Avatar asked Oct 08 '12 14:10

Chirag


People also ask

How do I copy a file from local directory to HDFS?

In order to copy a file from the local file system to HDFS, use Hadoop fs -put or hdfs dfs -put, on put command, specify the local-file-path where you wanted to copy from and then HDFS-file-path where you wanted to copy to. If the file already exists on HDFS, you will get an error message saying “File already exists”.

How do I copy an entire directory in Hadoop?

You can use the cp command in Hadoop. This command is similar to the Linux cp command, and it is used for copying files from one directory to another directory within the HDFS file system. You can see the below example.

Which command do you use to upload from the local file system to HDFS?

You can copy (upload) a file from the local filesystem to a specific HDFS using the fs put command. The specified file or directory is copied from your local filesystem to the HDFS. You can copy (download) a file from the a specific HDFS to your local filesystem using the fs get command.


1 Answers

Just use the FileSystem's copyFromLocalFile method. If the source Path is a local directory it will be copied to the HDFS destination:

...
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"));
...   
like image 96
Lorand Bendig Avatar answered Oct 14 '22 13:10

Lorand Bendig