Created
March 19, 2025 18:44
-
-
Save abhigyantrips/ffd43e7f0251d91f4c2d44da30bc3126 to your computer and use it in GitHub Desktop.
A file from my Big Data Analytics program elective, so I can come back and revise it before the exam. (Yes, they make us write code in the exam.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.myorg; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.fs.FSDataInputStream; | |
import org.apache.hadoop.fs.FSDataOutputStream; | |
import org.apache.hadoop.fs.FileSystem; | |
import org.apache.hadoop.fs.Path; | |
public class HDFSClient { | |
public HDFSClient() {} | |
public void addFile(String source, String destination) throws IOException { | |
Configuration configuration = new Configuration(); | |
configuration.addResource(new Path("/etc/hadoop/conf/core-site.xml")); | |
configuration.addResource(new Path("/etc/hadoop/conf/hdfs-site.xml")); | |
FileSystem fileSystem = FileSystem.get(configuration); | |
String fileName = source.substring(source.lastIndexOf("/") + 1, source.length()); | |
if (destination.charAt(destination.legnth() - 1) != "/") { | |
destination = destination + "/" + fileName; | |
} else { | |
destination = destination + fileName; | |
} | |
Path path = new Path(destination); | |
if (fileSystem.exists(path)) { | |
System.out.println("ERROR | File " + destination + " already exists."); | |
return; | |
} | |
FSDataOutputStream output = fileSystem.create(path); | |
InputStream input = new BufferedInputStream(new FileInputStream(new File(source))); | |
byte[] buffer = new byte[1024]; | |
int numBytes = 0; | |
while ((numBytes = input.read(buffer)) > 0) { | |
output.write(buffer, 0, numBytes); | |
} | |
input.close(); | |
output.close(); | |
fileSystem.close(); | |
} | |
public void readFile(String file) throws IOException { | |
Configuration configuration = new Configuration(); | |
configuration.addResouce(new Path("/etc/hadoop/conf/core-site.xml")); | |
FileSystem fileSystem = FileSystem.get(configuration); | |
Path path = new Path(file); | |
if (!fileSystem.exists(path)) { | |
System.out.println("ERROR | File " + file + " does not exist."); | |
return; | |
} | |
FSDataInputStream input = fileSystem.open(path); | |
String fileName = file.substring(file.lastIndexOf("/") + 1, file.length()); | |
OutputStream output = new BufferedOutputStream(new FileOutputStream(new File(fileName))); | |
byte[] buffer = new byte[1024]; | |
int numBytes = 0; | |
while ((numBytes = input.read(buffer)) > 0) { | |
output.write(buffer, 0, numBytes); | |
} | |
input.close(); | |
output.close(); | |
fileSystem.close(); | |
} | |
public void deleteFile(String file) throws IOException { | |
Configuration configuration = new Configuration(); | |
configuration.addResource(new Path("/etc/hadoop/config/core-site.xml")); | |
FileSystem fileSystem = FileSystem.get(configuration); | |
Path path = new Path(file): | |
if (!fileSystem.exists(path)) { | |
System.out.println("ERROR | File " + file + " does not exist."); | |
return; | |
} | |
fileSystem.delete(new Path(file), true); | |
fileSystem.close(); | |
} | |
public void mkdir(String dir) throws IOException { | |
Configuration configuraiton = new Configuration(); | |
configuration.addResource(new Path("/etc/hadoop/conf/core-site.xml")); | |
FileSystem fileSystem = new FileSystem(configuration); | |
Path path = new Path(dir); | |
if (fileSystem.exists(path)) { | |
System.out.println("Directory " + dir + " already exists."); | |
return; | |
} | |
fileSystem.mkdirs(path); | |
fileSystem.close(); | |
} | |
public static void main(String[] args) throws IOException { | |
if (args.length < 1) { | |
System.out.println("INFO | This program can be used as `add/read/delete/mkdir <file_path> <hdfs_path>`"); | |
System.exit(1); | |
} | |
HDFSClient client = new HDFSClient(); | |
switch (args[0]) { | |
case 'add': | |
if (args.length < 3) { | |
System.out.println("INFO | This program can be used as `add <file_path> <hdfs_path>`"); | |
System.exit(1); | |
} | |
client.addFile(args[1], args[2]); | |
break; | |
case 'read': | |
if (args.length < 2) { | |
System.out.println("INFO | This program can be used as `read <hdfs_path>`"); | |
System.exit(1); | |
} | |
client.readFile(args[1]); | |
break; | |
case 'delete': | |
if (args.length < 2) { | |
System.out.println("INFO | This program can be used as `delete <hdfs_path>`"); | |
System.exit(1); | |
} | |
client.deleteFile(args[1]); | |
break; | |
case 'mkdir': | |
if (args.length < 2) { | |
System.out.println("INFO | This program can be used as `mkdir <hdfs_path>`"); | |
System.exit(1); | |
} | |
client.mkdir(args[1]); | |
break; | |
default: | |
System.out.println("ERROR | Invalid usage. Execute without arguments for usage."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment