Write Java code to add delete and modify HDFS and check operational code examples

  • 2021-07-18 07:52:41
  • OfStack

In this paper, we share the specific codes of Java code for adding, deleting, modifying and checking HDFS for your reference. The specific contents are as follows


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FileOpreation {

	public static void main(String[] args) throws IOException {
		//CreateFile();
		//DeleteFile();
		//CopyFileToHDFS();
		//MkDirs();
		//DelDirs();
		ListDirectory();
		DownLoad();

	}
	public static void CreateFile() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration =new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		byte[] file_content_buff="hello hadoop world, test write file !\n".getBytes();
		Path dfs = new Path("/home/test.txt");
		FSDataOutputStream outputStream = fSystem.create(dfs);
		outputStream.write(file_content_buff.length);
	}
	public FileOpreation() {
		// TODO Auto-generated constructor stub
	}public static void DeleteFile() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration =new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		Path deletf = new Path("/home/test.txt");
		boolean delResult = fSystem.delete(deletf,true);
		System.out.println(delResult==true?" Delete succeeded ":" Delete failed ");
	}
  
	public static void CopyFileToHDFS() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration =new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		Path src = new Path("E:\\SerializationTest\\APITest.txt");
		Path dest_src = new Path("/home");
		fSystem.copyFromLocalFile(src, dest_src);
	}
	
	public static void MkDirs() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration =new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		Path src = new Path("/Test");
		fSystem.mkdirs(src);
		
	}
	
	public static void DelDirs() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration = new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		Path src = new Path("/Test");
		fSystem.delete(src);

	}
	
	public static void ListDirectory() throws IOException {
		String uri = "hdfs://Alvis:9000";
		Configuration configuration = new Configuration();
		FileSystem fSystem = FileSystem.get(URI.create(uri), configuration);
		FileStatus[] fStatus = fSystem.listStatus(new Path("/output"));
		for(FileStatus status : fStatus)
			if (status.isFile()) {
				System.out.println(" File path: "+status.getPath().toString());
				System.out.println(" File path  getReplication : "+status.getReplication());
				System.out.println(" File path  getBlockSize:"+status.getBlockSize());
				BlockLocation[] blockLocations = fSystem.getFileBlockLocations(status, 0, status.getBlockSize());
				for(BlockLocation location : blockLocations){
					System.out.println(" Hostname: "+location.getHosts()[0]);
					System.out.println(" Hostname: "+location.getNames()[0]);
			  }
		  }
			else {
				System.out.println("directory:"+status.getPath().toString());
			}
	}
	
	public static void DownLoad() throws IOException {
		Configuration configuration = new Configuration();
		configuration.set("fs.defaultFS", "hdfs://Alvis:9000");
		FileSystem fSystem =FileSystem.get(configuration);
		FSDataInputStream inputStream =fSystem.open( new Path("/input/wc.jar"));
		FileOutputStream outputStream = new FileOutputStream(new File("E:\\LearnLife\\DownLoad\\wc.jar"));
		IOUtils.copy(inputStream, outputStream);
		System.out.println(" Download successful! ");
	}
}

Thoughts:

1. Define the virtual machine interface

2. Get the HDFS remote call interface object Configuration first

3. Define the distributed file system FileSystem Object Acquisition Object

4. Given the path

5. Invoke an operation with an FileSystem object


Related articles: