Step1: Increased dependency pom.xml ... <!--Https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -- <Dependency> <groupId>Org.apache.hadoop</groupId> <Artifactid> Hadoop-common</Artifactid> <version>2.2.0</version> <Exclusions> <exclusion> <Artifactid>Jdk.tools</Artifactid> <groupId>Jdk.tools</groupId> </exclusion> </Exclusions> </Dependency> <Dependency> <groupId>Org.apache.hadoop</groupId> <Artifactid> Hadoop-HDFs </Artifactid> <version>2.2.0</version> </Dependency>Step2: Copy config file ' hdfs-site.xml ' and ' Core-site.xml ' step3: Test code
PackageCjkjcn.demo.hadoop.hdfs;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.util.LinkedList;Importjava.util.List;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FSDataInputStream;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;/** * * @authorJunhong * * May 18, 2017*/ Public classHdfsdao {Private StaticConfiguration conf =NewConfiguration (); PrivateFileSystem HDFs; Final StaticString Root_path = "/user"; PublicHdfsdao () {Conf.addresource ("Hdfs-site.xml"); Conf.addresource ("Core-site.xml"); Try{HDFs= Filesystem.get (conf);//Initialize HDFs}Catch(IOException e) {e.printstacktrace (); } System.out.println ("Param size=" +conf.size ()); } /*** Scan test file exists*/ Public voidScanfiles () {Try{path Path=NewPath (Root_path); System.out.println (hdfs.exists (path)); } Catch(IOException e) {e.printstacktrace (); } } /*** Read text files by line *@paramfile *@return */ PublicList<string>lines (String file) {List<String> list =NewLinkedlist<>(); Path F=NewPath (file); Try{Fsdatainputstream input=Hdfs.open (f); InputStreamReader INR=NewInputStreamReader (input); BufferedReader Read=NewBufferedReader (INR); String Line; while(line = Read.readline ())! =NULL) {List.add (line); } } Catch(IOException e) {e.printstacktrace (); } returnlist; }}
Note:1) If the lack of dependency<Dependency> <groupId>Org.apache.hadoop</groupId> <Artifactid> Hadoop-HDFs </Artifactid> <version>2.2.0</version> </Dependency>will cause the following error!! Java.io.Exception:No FileSystem for Scheme:hdfs2) Permission issues may occur when you test a write file or create a directory method interfacepemission denied for test
HDFs Java Client to the HDFs file additions and deletions to check and change