Java API for Hadoop file system additions and deletions
The Hadoop file system can be manipulated through shell commands hadoop fs -xx
, as well as a Java programming interface
MAVEN Configuration
<project xmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hadoopStudy</groupId> <artifactId>demo</artifactId> < Version>1.0-snapshot</version> <packaging>jar</packaging> <name>demo</name> <url>http://maven.apache.org</url><properties> <project.Build.sourceencoding>utf-8</project.Build.sourceencoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <a Rtifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupid>org .Apache.Hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupid>org.Apache.Hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupid>org.Apache.Hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>3.0.0</version> </dependency> </dependencies></project>
Code implementation
Package Hadoopstudy;import org.apache.hadoop.conf.Configuration;import Org.apache.hadoop.fs.FileSystem;import Org.apache.hadoop.fs.LocatedFileStatus;import Org.apache.hadoop.fs.Path;import Org.apache.hadoop.fs.RemoteIterator;import java.io.IOException;import Java.net.URI;import java.net.URISyntaxException; Public classHdfsutil {PrivateFileSystem FS;Hdfsutil(String user) {Configuration cfg =NewConfiguration (); Uri uri =NULL;Try{URI =NewURI ("hdfs://localhost:9000"); }Catch(URISyntaxException e) {e.Printstacktrace(); }Try{//According to the configuration file, instantiate into DistributedfilesystemFS = FileSystem.Get(URI, CFG, user);//Get FS handle}Catch(IOException e) {e.Printstacktrace(); }Catch(Interruptedexception e) {e.Printstacktrace(); } }/*** Upload Files */ Public void Upload(string src, string dst) {Try{//UploadFs.Copyfromlocalfile(New Path(SRC),New Path(DST)); }Catch(IOException e) {e.Printstacktrace(); } }/*** Download File */ Public void Download(string src, string dst) {Try{//DownloadFs.Copytolocalfile(New Path(SRC),New Path(DST)); }Catch(IOException e) {e.Printstacktrace(); } }/*** Create Folder */ Public void mkdir(String dir) {Try{fs.mkdirs(New Path(dir)); }Catch(IOException e) {//Create a directoryE.Printstacktrace(); } }/*** Delete Files */ Public void Delete(String name,BooleanRecursive) {Try{fs.Delete(New Path(name), recursive); }Catch(IOException e) {e.Printstacktrace(); } }/*** Rename */ Public void Rename(string source, string DST) {Try{fs.Rename(New Path(source),New Path(DST)); }Catch(IOException e) {e.Printstacktrace(); } }/*** List File information */ Public void List(String dir,BooleanRecursive) {Try{remoteiterator<locatedfilestatus> iter = fs.Listfiles(New Path(dir), recursive); while(ITER.Hasnext()) {Locatedfilestatus file = iter.Next(); System. out.println(file.GetPath().GetName()); } }Catch(IOException e) {e.Printstacktrace(); } }}
Summary
The Java API for HDFs provides an elegant filesystem abstract class that is not distinguished from other file systems when used by clients
Java API for Hadoop file system additions and deletions