Java API for HDFS operations

Keywords: Big Data Eclipse Hadoop Java Mobile

1. Environmental Construction

  1. Configuration of environment variables HADOOP_HONE
  2. Add bin of HADOOP_HOME to PATH
  3. Permission issues: Add HADOOP_USER_NAME=root environment variable

Configuration of Eclipse

  1. Add the following hadoop-eclipse-plugin.jar package to the dropins plugins folder in the Eclipse installation directory
  2. Restart Eclipse 2. Restart Eclipse
  3. Import the necessary jar packages for the project
  4. Replace the bin folder under the Hadoop package corresponding to the project to make it easy to operate
  5. The configuration is as follows:

The configuration was successful as follows

Find Map/Reduce Locations


Final configuration

After successful configuration

3. Java API Code

public class TestHDFS {
	public static void main(String[] args) throws IOException {
		//Create configuration objects before operating HDFS
		Configuration conf = new Configuration(true);
		//Create objects that operate on HDFS
		FileSystem fs = FileSystem.get(conf);
		
		//View the contents of the file system
		List list = listFileSystem(fs,"/");
		//create folder
		createDir(fs,"/test/abc");
		
		//Upload files
		uploadFileToHDFS(fs,"d:/wc","/test/abc/");
		
		//Download File
		downLoadFileFromHDFS(fs,"/test/abc/wc","d:/");
		
		//Delete...
		
		//rename
		renameFile(fs,"/test/abc/wc","/test/abc/Angelababy");
		
		//Internal Mobile Internal Replication
		innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy","/");
		
		//Create a new file
		createNewFile(fs,"/test/abc/hanhong");
		
		//Writing file
		writeToHDFSFile(fs,"/test/abc/hanhong","hello world");
		//Additional writing
		appendToHDFSFile(fs,"/test/abc/hanhong","\nhello world");
		
		//Read the contents of the document
		readFromHDFSFile(fs,"/test/abc/hanhong");
		
		//Location of data acquisition
		getFileLocation(fs,"/install.log");
	}

	private static void getFileLocation(FileSystem fs, String string) throws IOException {
		FileStatus fileStatus = fs.getFileStatus(new Path(string));
		long len = fileStatus.getLen();
		BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, len);
		String[] hosts = fileBlockLocations[0].getHosts();
		for (String string2 : hosts) {
			System.out.println(string2);
		}
		
		HdfsBlockLocation blockLocation = (HdfsBlockLocation)fileBlockLocations[0];
		long blockId = blockLocation.getLocatedBlock().getBlock().getBlockId();
		System.out.println(blockId);
	}

	private static void readFromHDFSFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		FSDataInputStream inputStream = fs.open(new Path(string));
		
		FileStatus fileStatus = fs.getFileStatus(new Path(string));
		
		
		
		long len = fileStatus.getLen();
		
		byte[] b = new byte[(int)len];
		int read = inputStream.read(b);
		while(read != -1){
			System.out.println(new String(b));
			read = inputStream.read(b);
		}
		
		
	}

	private static void appendToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
		FSDataOutputStream append = fs.append(new Path(filePath));
		append.write(content.getBytes("UTF-8"));
		append.flush();
		append.close();
	}

	private static void writeToHDFSFile(FileSystem fs, String filePath, String content) throws IllegalArgumentException, IOException {
		 FSDataOutputStream outputStream = fs.create(new Path(filePath));
		 outputStream.write(content.getBytes("UTF-8"));
		 outputStream.flush();
		 outputStream.close();
	}

	private static void createNewFile(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		fs.createNewFile(new Path(string));
	}

	private static void innerCopyAndMoveFile(FileSystem fs, Configuration conf,String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		
		//Internal copy
//		FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,false, conf);
		//Internal mobility
		FileUtil.copy(srcPath.getFileSystem(conf), srcPath, destPath.getFileSystem(conf), destPath,true, conf);
	}

	private static void renameFile(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		
		fs.rename(srcPath, destPath);
	
	}

	private static void downLoadFileFromHDFS(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		//copyToLocal
//		fs.copyToLocalFile(srcPath, destPath);
		//moveToLocal
		fs.copyToLocalFile(true,srcPath, destPath);
	}

	private static void uploadFileToHDFS(FileSystem fs, String src, String dest) throws IOException {
		Path srcPath = new Path(src);
		Path destPath = new Path(dest);
		//copyFromLocal
//		fs.copyFromLocalFile(srcPath, destPath);
		//moveFromLocal
		fs.copyFromLocalFile(true,srcPath, destPath);
	}

	private static void createDir(FileSystem fs, String string) throws IllegalArgumentException, IOException {
		Path path = new Path(string);
		if(fs.exists(path)){
			fs.delete(path, true);
		}
		fs.mkdirs(path);
	}

	private static List listFileSystem(FileSystem fs, String path) throws FileNotFoundException, IOException {
		Path ppath = new Path(path);
		
		FileStatus[] listStatus = fs.listStatus(ppath);
		
		for (FileStatus fileStatus : listStatus) {
			System.out.println(fileStatus.getPath());
		}
		
		return null;
	}
}

Posted by naskoo on Sat, 02 Feb 2019 12:36:15 -0800