Tuesday, 22 October 2024

Read write java program

 

FileReadFromHDFS.java

public class FileReadFromHDFS {

 

public static void main(String[] args) throws Exception {

 

//File to read in HDFS

String uri = args[0];

 

Configuration conf = new Configuration();

 

//Get the filesystem - HDFS

FileSystem fs = FileSystem.get(URI.create(uri), conf);

FSDataInputStream in = null;

 

try {

//Open the path mentioned in HDFS

in = fs.open(new Path(uri));

IOUtils.copyBytes(in, System.out, 4096, false);

 

System.out.println("End Of file: HDFS file read complete");

 

} finally {

IOUtils.closeStream(in);

}

}

}

 

FileWriteToHDFS.java

public class FileWriteToHDFS {

 

public static void main(String[] args) throws Exception {

 

//Source file in the local file system

String localSrc = args[0];

//Destination file in HDFS

String dst = args[1];

 

//Input stream for the file in local file system to be written to HDFS

InputStream in = new BufferedInputStream(new FileInputStream(localSrc));

 

//Get configuration of Hadoop system

Configuration conf = new Configuration();

System.out.println("Connecting to -- "+conf.get("fs.defaultFS"));

 

//Destination file in HDFS

FileSystem fs = FileSystem.get(URI.create(dst), conf);

OutputStream out = fs.create(new Path(dst));

 

//Copy file from local to HDFS

IOUtils.copyBytes(in, out, 4096, true);

 

System.out.println(dst + " copied to HDFS");

 

}

}

 

No comments:

Post a Comment