Creating and Appending Data to a File
In this step, you will create a new file on HDFS, write some initial data to it, and then append more data to the file using the appendToFile
command.
-
Switch to the hadoop
user in the terminal:
su - hadoop
-
Create a new file named mining_data.txt
in the /home/hadoop
directory with initial content:
echo "Initial data for mining analysis" > mining_data.txt
-
Create a new file named mining_data.txt
on the HDFS /
directory:
hdfs dfs -touchz /mining_data.txt
-
Append more data to the mining_data.txt
file:
hdfs dfs -appendToFile /home/hadoop/mining_data.txt /mining_data.txt
Here's an explanation of the command and its components:
- hdfs: This is the command-line tool for interacting with HDFS.
- dfs: This is a subcommand of the hdfs tool, specifically used for working with HDFS.
- -appendToFile: This is an option of the hdfs dfs command, indicating that the data should be appended to the target file.
- /home/hadoop/mining_data.txt: This is the path to the source file that contains the data to be appended.
- /mining_data.txt: This is the path to the target file in HDFS where the data will be appended.
When the hdfs dfs -appendToFile
command is executed, it reads the data from the specified source file and appends it to the target file in HDFS.