# EXPERIMENT 7
# HDFS File Management Operations
# Online Compiler Simulation
print("========================================")
print(" HDFS FILE MANAGEMENT SIMULATION")
print("========================================")
# ----------------------------------------
# Simulated HDFS
# ----------------------------------------
hdfs = {
"/user/student/dir1": {},
"/user/student/dir2": {}
}
# ----------------------------------------
# 1. CREATE DIRECTORIES
# ----------------------------------------
print("\n1. CREATE DIRECTORIES")
print("----------------------------------------")
print("hadoop fs -mkdir /user/student/dir1")
print("Directory created successfully")
print("hadoop fs -mkdir /user/student/dir2")
print("Directory created successfully")
# ----------------------------------------
# 2. CREATE AND UPLOAD FILE
# ----------------------------------------
print("\n2. UPLOAD FILE TO HDFS")
print("----------------------------------------")
sample_content = """Big Data Analytics
Hadoop is used for distributed processing.
HDFS provides distributed storage.
MapReduce processes large datasets.
Big Data is useful for business analytics."""
hdfs["/user/student/dir1"]["sample.txt"] = sample_content
print("hadoop fs -put sample.txt /user/student/dir1/")
print("File uploaded successfully")
# ----------------------------------------
# 3. LIST DIRECTORY
# ----------------------------------------
print("\n3. LIST HDFS DIRECTORY")
print("----------------------------------------")
print("hadoop fs -ls /user/student/dir1")
for filename in hdfs["/user/student/dir1"]:
print("sample.txt")
# ----------------------------------------
# 4. DISPLAY FILE CONTENT
# ----------------------------------------
print("\n4. DISPLAY FILE CONTENT")
print("----------------------------------------")
print("hadoop fs -cat /user/student/dir1/sample.txt")
print()
print(hdfs["/user/student/dir1"]["sample.txt"])
# ----------------------------------------
# 5. COPY FILE
# ----------------------------------------
print("\n5. COPY FILE")
print("----------------------------------------")
content = hdfs["/user/student/dir1"]["sample.txt"]
hdfs["/user/student/dir2"]["sample_copy.txt"] = content
print("hadoop fs -cp /user/student/dir1/sample.txt /user/student/dir2/")
print("File copied successfully")
# ----------------------------------------
# 6. MOVE FILE
# ----------------------------------------
print("\n6. MOVE FILE")
print("----------------------------------------")
hdfs["/user/student/dir2"]["moved.txt"] = "This file has been moved."
print("hadoop fs -mv /user/student/dir1/moved.txt /user/student/dir2/")
print("File moved successfully")
# ----------------------------------------
# 7. DOWNLOAD FILE
# ----------------------------------------
print("\n7. DOWNLOAD FILE")
print("----------------------------------------")
downloaded_file = hdfs["/user/student/dir2"]["sample_copy.txt"]
print("hadoop fs -get /user/student/dir2/sample_copy.txt")
print("File downloaded successfully")
print("Downloaded content:")
print(downloaded_file)
# ----------------------------------------
# 8. DISPLAY LAST FEW LINES
# ----------------------------------------
print("\n8. DISPLAY LAST FEW LINES")
print("----------------------------------------")
print("hadoop fs -tail /user/student/dir1/sample.txt")
print()
lines = hdfs["/user/student/dir1"]["sample.txt"].split("\n")
for line in lines[-3:]:
print(line)
# ----------------------------------------
# 9. DISPLAY FILE SIZE
# ----------------------------------------
print("\n9. DISPLAY FILE SIZE")
print("----------------------------------------")
file_content = hdfs["/user/student/dir1"]["sample.txt"]
file_size = len(file_content.encode("utf-8"))
print("hadoop fs -du /user/student/dir1/sample.txt")
print("File size:", file_size, "bytes")
# ----------------------------------------
# 10. DELETE FILE
# ----------------------------------------
print("\n10. DELETE FILE")
print("----------------------------------------")
del hdfs["/user/student/dir2"]["moved.txt"]
print("hadoop fs -rm /user/student/dir2/moved.txt")
print("File deleted successfully")
# ----------------------------------------
# 11. FINAL DIRECTORY CONTENT
# ----------------------------------------
print("\n11. FINAL HDFS CONTENT")
print("----------------------------------------")
print("/user/student/dir1")
for filename in hdfs["/user/student/dir1"]:
print(" ", filename)
print("\n/user/student/dir2")
for filename in hdfs["/user/student/dir2"]:
print(" ", filename)
# ----------------------------------------
# RESULT
# ----------------------------------------
print("\n========================================")
print(" RESULT")
print("========================================")
print("Directory creation : SUCCESS")
print("File upload : SUCCESS")
print("Directory listing : SUCCESS")
print("File retrieval : SUCCESS")
print("File copying : SUCCESS")
print("File moving : SUCCESS")
print("File downloading : SUCCESS")
print("File deletion : SUCCESS")
print("Tail operation : SUCCESS")
print("File size operation : SUCCESS")
print("\nExperiment 7 completed successfully.")