Java Operations for HDFs

Source: Internet
Author: User

Lab Environment:

Windows 10

Eclipse mars.2 Release (4.5.2)

CentOS 7

Hadoop-2.7.3

Prerequisite:

1) The environment variables on windows are configured OK. such as: PATH, Hadoop_home;

2) Hadoop.dll with the Hadoop version and the Windows platform (x86 or x64), the Winutils.exe tool is configured OK.

1. Create a new Java project:

2. Import the external jar package, the HDFs jar package:

3.Java Code:

 PackageHdfsutil;ImportJava.net.URI;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.FileSystem;ImportOrg.apache.hadoop.fs.Path; Public classHdfsutil { Public Static voidMain (string[] args)throwsException {//constructs a wrapper object for a configuration parameterConfiguration conf =NewConfiguration (); //there will be a parameter in conf: The default value of Fs.defaultfs is file:///refers to the local file system URI//conf.set ("Fs.defaultfs", "HDFs://192.168.80.128:9000 "); //construct a client for HDFs//FileSystem fs = Filesystem.get (conf);FileSystem FS= Filesystem.get (NewURI ("hdfs://192.168.80.128:9000"), conf, "Hadoop"); //Use the HDFs file system client object fs to manipulate files, such as uploading a fileFs.copyfromlocalfile (NewPath ("C:/file.txt"),NewPath ("/user/hadoop/input/file.txt.4")); //when the upload is complete, close the clientFs.close (); }}

Java Operations for 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.