Thursday 14 April 2022

How to overwrite an existing file in HDFS?

Using  -f option, we can overwrite the existing file.

 

Example

hadoop fs -copyFromLocal -f emp.csv /copyDemo

 

Above snippet overwrite the content of file emp.csv in the folder /copyDemo.

 

Let’s experiment by copying the file emp.csv to some Hadoop location multiple times.

 

emp.csv  

1,Krishna
2,Ram
3,Raheem
4,Joel

 

Create a directory /copyDemo and copy the file emp.csv to it.

[cloudera@quickstart hive]$ hadoop fs -mkdir /copyDemo
[cloudera@quickstart hive]$ 
[cloudera@quickstart hive]$ hadoop fs -copyFromLocal emp.csv /copyDemo
[cloudera@quickstart hive]$ 
[cloudera@quickstart hive]$ hadoop fs -ls /copyDemo
Found 1 items
-rw-r--r--   1 cloudera supergroup         32 2022-04-14 21:41 /copyDemo/emp.csv
[cloudera@quickstart hive]$ 
[cloudera@quickstart hive]$ hadoop fs -cat /copyDemo/*
1,Krishna
2,Ram
3,Raheem
4,Joel

Let’s update emp.csv file like below.

 

emp.csv

1,Krishna
2,Ram
3,Raheem
4,Joel
5,Gopi
6,Sailu

Let’s try without -f option.

[cloudera@quickstart hive]$ hadoop fs -copyFromLocal emp.csv /copyDemo
copyFromLocal: `/copyDemo/emp.csv': File exists

Let’s try with -f option.

[cloudera@quickstart hive]$ hadoop fs -copyFromLocal -f emp.csv /copyDemo
[cloudera@quickstart hive]$ 
[cloudera@quickstart hive]$ hadoop fs -ls /copyDemo
Found 1 items
-rw-r--r--   1 cloudera supergroup         47 2022-04-14 21:46 /copyDemo/emp.csv
[cloudera@quickstart hive]$ 
[cloudera@quickstart hive]$ hadoop fs -cat /copyDemo/*
1,Krishna
2,Ram
3,Raheem
4,Joel
5,Gopi
6,Sailu

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment