Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move or copy file in HDFS by using JAVA API

Tags:

hadoop

hdfs

I want to copy file in THE SAME HDFS ,just like copy file from HDFS://abc:9000/user/a.txt to HDFS://abc:9000/user/123/

Can I do that by using JAVA API? Thanks

like image 267
haochi zhang Avatar asked Jul 01 '16 09:07

haochi zhang


People also ask

How to copy file from local file system to Hadoop HDFS?

The Hadoop fs shell command – put is used to copy the file from local file system to Hadoop HDFS file system. similarly HDFS also has – copyFromLocal. Below is the usage of -put command.

How to write a file to HDFS in Java?

Writing A File To HDFS – Java Program Writing a file to HDFS is very easy, we can simply execute hadoop fs -copyFromLocal command to copy a file from local filesystem to HDFS. In this post we will write our own Java program to write the file from local file system to HDFS. Here is the program – FileWriteToHDFS.java

What is the difference between Hadoop and HDFS?

HDFS is a distributed file system for storing very large data files, running on clusters of commodity hardware. It is fault tolerant, scalable, and extremely simple to expand. Hadoop comes bundled with HDFS ( Hadoop Distributed File Systems ).

How do I overwrite an existing file in HDFS?

If the file already exists on HDFS, you will get an error message saying “File already exists”. In order to overwrite the file use -f option. Note that you can use it with either hadoop fs -put or hdfs dfs -put to upload files from the local file system to HDFS, both return the same results.


1 Answers

FileUtil provides a method for copying files.

Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://abc:9000");
FileSystem filesystem = FileSystem.get(configuration);
FileUtil.copy(filesystem, new Path("src/path"), filesystem, new Path("dst/path"), false, configuration);

If you need to copy it to another cluster, just make a new Configuration and setup and new FileSystem.

like image 179
Serhiy Avatar answered Nov 15 '22 06:11

Serhiy