Access to HDFs through the Java API
Configuring environment variables on 1.Windows
Unzip Hadoop, then configure the root directory of Hadoop into the hadoop_home environment variable
And then put Hadoop_home/lib and Hadoop_home/bin in the path.
2. Replace the bin directory
The bin directory below the Hadoop directory downloaded by the flag network is replaced with the bin directory of Hadoop compiled under Windows:
I have uploaded the bin directory of my compiled hadoop-2.6.1, which can be downloaded from Https://pan.baidu.com/s/1P50N60Qb16zuZxSwPq6HaA
can also refer to others blog http://blog.csdn.net/mrbcy/article/details/55806809 self-compiled for their own computer system version can be
3.Eclipse requires a jar package dependency
The list of jar packages to add is as follows:
- Hadoop/share/hadoop/common/hadoop-common-2.6.4.jar
- Hadoop/share/hadoop/common/lib/*.jar
- Hadoop/share/hadoop/hdfs/hadoop-hdfs-2.6.4.jar
- Hadoop/share/hadoop/hdfs/lib/*.jar
4. Manipulating the HDFs file system using the client
4.1. Upload files (Hadoop_home can be uploaded)
PackageCn.bigdata.hdfs;Importjava.io.IOException;ImportJava.net.URI;Importjava.net.URISyntaxException;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path;ImportOrg.junit.Before;Importorg.junit.Test;/*** When the client goes to HDFs, there is a user identity * By default, the HDFs client API obtains a parameter from the JVM as its user identity:-dhadoop_user_name=root * can also be used when constructing client fs objects, Passed in by parameters*/ Public classHdfscientdemo {PrivateFileSystem fs =NULL; Configuration conf=NULL; @Before Public voidInin ()throwsIOException, Interruptedexception, urisyntaxexception{Configuration conf=NewConfiguration (); //get a client instance object with a file system operation, the last parameter is the user name, Namenode access path: hdfs://shizhan2:9000FS = Filesystem.get (NewURI ("hdfs://shizhan2:9000"), conf, "root"); } //Uploading Files@Test Public voidTestupload ()throwsexception{Fs.copyfromlocalfile (NewPath ("C:/regist.log"),NewPath ("/regist-copy.log")); Fs.close (); }}
After running, upload the file successfully:
4.2. Download the file (replace the bin to download)
@Test publicvoidthrows Exception { fs.copytolocalfile ( false New New false ); Fs.close (); }
4.3. Download the file (replace the bin to download)
Java API Operations for Hadoop_07_hdfs