Java API Operations for Hadoop_07_hdfs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.