How to Copy File from HDFS in Hadoop Quickly
To copy a file from HDFS to your local system, use the
hadoop fs -copyToLocal command followed by the HDFS source path and the local destination path. Alternatively, you can use hadoop fs -get which works the same way.Syntax
The basic syntax to copy a file from HDFS to local is:
hadoop fs -copyToLocal <source_path_in_HDFS> <destination_path_in_local>- Or equivalently,
hadoop fs -get <source_path_in_HDFS> <destination_path_in_local>
Here, source_path_in_HDFS is the path of the file in HDFS you want to copy, and destination_path_in_local is where you want to save it on your local machine.
bash
hadoop fs -copyToLocal /user/hadoop/input/file.txt /home/user/file.txt
Example
This example copies a file named data.txt from the HDFS directory /user/hadoop/ to the local directory /home/user/.
bash
hadoop fs -copyToLocal /user/hadoop/data.txt /home/user/data.txt
Common Pitfalls
- Make sure the source file exists in HDFS; otherwise, you will get a "File not found" error.
- Ensure you have the right permissions to read the file in HDFS and write to the local destination.
- Do not forget to specify the full path for both source and destination to avoid confusion.
- Using
hadoop fs -copyToLocalwill overwrite the local file without warning if it exists.
bash
Wrong: hadoop fs -copyToLocal data.txt /home/user/ Right: hadoop fs -copyToLocal /user/hadoop/data.txt /home/user/data.txt
Quick Reference
| Command | Description |
|---|---|
| hadoop fs -copyToLocal | Copy file from HDFS to local filesystem |
| hadoop fs -get | Same as copyToLocal, copies file from HDFS to local |
| hadoop fs -copyFromLocal | Copy file from local to HDFS |
| hadoop fs -put | Same as copyFromLocal, copies local file to HDFS |
Key Takeaways
Use 'hadoop fs -copyToLocal' or 'hadoop fs -get' to copy files from HDFS to local.
Always specify full paths for source and destination to avoid errors.
Check file existence and permissions before copying.
Copying will overwrite local files without warning.
Use 'hadoop fs -copyFromLocal' or 'hadoop fs -put' to copy files from local to HDFS.