Javaapi of HDFs

Source: Internet
Author: User

Configure the Hadoop environment for the Windows platform

To do HDFS client app development on Windows, you need to set up a Hadoop environment and require a Windows platform-compiled Hadoop, or you will report the following error:

null\bin\winutils.exe in the Hadoop binaries.

Ref: 72896484

Create a MAVEN project that introduces Pom dependencies
<Dependencies>    <Dependency>        <groupId>Org.apache.hadoop</groupId>        <Artifactid>Hadoop-common</Artifactid>        <version>2.7.4</version>    </Dependency>    <Dependency>        <groupId>Org.apache.hadoop</groupId>        <Artifactid>Hadoop-hdfs</Artifactid>        <version>2.7.4</version>    </Dependency>    <Dependency>        <groupId>Org.apache.hadoop</groupId>        <Artifactid>Hadoop-client</Artifactid>        <version>2.7.4</version>    </Dependency></Dependencies>

Client Object

Working with HDFS in Java mainly involves the following Class:

Configuration: The object of this class is marshaled to the client or server;

FileSystem: The object of this class is a file system object, which can be manipulated by some methods of the object, and obtained by FileSystem's static method get.

FileSystem fs = filesystem.get (conf)

The Get method determines what type of file system is specific from the configuration value of a parameter Fs.defaultfs in Conf. If FS.DEFAULTFS is not specified in our code, and the project Classpath is not given a corresponding configuration, the default value in conf comes from Core-default.xml in the jar package of Hadoop, with the default value: file:///, is not an instance of Distributedfilesystem, but a client object for the local file system.

Sample code
1  Public classTesthdfs {2 3      Public Static voidMain (string[] args)throwsexception{4Configuration conf =NewConfiguration ();5         //specifies that the HDFs file system is used6 //conf.set ("Fs.defaultfs", "HDFs://node-1:9000 ");7 8         //filesystem is the core class of the Hadoop operational file system9         //get the file system client object by filesystem static methodTen //FileSystem fs = Filesystem.get (conf); One         //set uri,conf, user identity AFileSystem fs = Filesystem.get (NewURI ("hdfs://node-1:9000"), conf, "root"); -  -         //Create a folder the //Fs.mkdirs (New Path ("/createbyjava")); -         //Uploading Files - //Fs.copyfromlocalfile (New Path ("D:\\test.txt"), New Path ("/createbyjava/test.txt")); -         //Download File + //Fs.copytolocalfile (New Path ("/createbyjava/test.txt"), New Path ("D:\\test\\test.txt")); -  +         //stream form to read a local file AFileInputStream in =NewFileInputStream (NewFile ("D:\\test.txt")); at         //Create a file -Fsdataoutputstream out = Fs.create (NewPath ("/test.txt")); -         //stream in the form of uploading the local file to HDFs - ioutils.copy (in,out); -  - fs.close (); in     } -}

Javaapi of 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.