After building a high-availability HDFS cluster, Java can be used in Eclipse to operate HDFS and read and write files.
High Availability HDFS Cluster Building Steps: https://blog.csdn.net/Chris_MZJ/article/details/83033471
Connecting HDFS with Eclipse
1. Place hadoop-eclipse-plugin-2.6.0.rar in the installation directory of eclipse, as follows:
2. Restart eclipse and click Open Perspective in the upper right corner of eclipse
3. Select the map/reduce view and click open
4. Right-click new Hadoop Location in the margin of the Map/Reduce Locations view below
5. Fill in a NameNode node in Host. My name is Noe1. If you do not modify the hosts file of the system, you will make a mistake. Fill in port 8020.
6. Modify the hosts file: C: Windows System32 drivers etc
7. After opening the hosts file, edit and add the routing of the four nodes of HDFS
8. Elipse connects the HDFS view:
9. Create a new dynamic web project to test HDFS:
In fact, the configuration files core-site.xml and hdfs-site.xml are copied from the installation directory of hadoop in node1 node.
Test source code:
package com.hpe.hadoop.hdfs; import java.io.FileNotFoundException; import java.io.IOException; 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.FileUtil; import org.apache.hadoop.fs.HdfsBlockLocation; import org.apache.hadoop.fs.Path; import com.sun.xml.bind.v2.schemagen.xmlschema.List; /** * 1,see file * 2,Create a new folder * 3,Upload files * 4,Download File * 5,Delete files * 6,Internal mobility * 7,Internal replication * 8,rename * 9,Create new files * 10,Writing file * 11,Read the contents of the document * @author eversec * */ 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:/test.txt","/test/abc/"); //Download File downLoadFileFromHDFS(fs,"/test/abc/test.txt","d:/"); //Delete... //rename renameFile(fs,"/test/abc/test.txt","/test/abc/Angelababy.txt"); //Internal Mobile Internal Replication innerCopyAndMoveFile(fs,conf,"/test/abc/Angelababy.txt","/"); //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,"/test/abc/hanhong"); } 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; } }