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