上一篇文章博主爲你們講述了hadoop的命令行操做,以及第一個mapreduce程序的運行案例。本篇將博主將繼續解釋在windows10系統上搭建hadoop以及使用JavaAPI操做HDFS文件系統。java
1、win10上搭建hadoop環境node
1.官網下載hadoop-2.9.1.tar.gz版本,解壓:E:\Program Files\hadoop-2.9.1apache
2.配置環境變量windows
HADOOP_HOME=E:\hadoop-2.9.1 PATH=%HADOOP_HOME%\bin
3.將windows上編譯的文件hadoop.dll、winutils.exe放至%HADOOP_HOME%\bin下centos
4.將hadoop.dll放到c:/windows/System32下api
5.設置E:\hadoop-2.9.1\etc\hadoop\hadoop-env.cmd中的JAVA_HOME爲真實java路徑(路徑中不能帶空格,否者會報錯).服務器
6.測試hadoop是否配置成功,命令行輸入:hadoop versioneclipse
2、在eclipse中新建maven工程hadoop-demojvm
pom.xml配置文件:maven
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.9.1</version> </dependency>
測試代碼:HdfsClientDemo.java
package com.empire.hadoop.hadoop_demo; import java.net.URI; import java.util.Iterator; import java.util.Map.Entry; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.RemoteIterator; import org.junit.Before; import org.junit.Test; /** * hdfs客戶端測試 */ public class HdfsClientDemo { FileSystem fs = null; Configuration conf = null; /** * 初始化hadoop hdfs文件系統遠程客戶端 * @throws Exception */ @Before public void init() throws Exception{ conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://master:9000"); //客戶端去操做hdfs時,是有一個用戶身份的 //默認狀況下,hdfs客戶端api會從jvm中獲取一個參數來做爲本身的用戶身份:-DHADOOP_USER_NAME=hadoop //拿到一個文件系統操做的客戶端實例對象 /*fs = FileSystem.get(conf);*/ //不配置會報錯 System.setProperty("hadoop.home.dir", "E:\\\\hadoop-2.9.1"); //能夠直接傳入 uri和用戶身份 //centos-aaron-h1爲namenode的主機名或者域名 fs = FileSystem.get(new URI("hdfs://centos-aaron-h1:9000"),conf,"hadoop"); //最後一個參數爲用戶名 } /** * 上傳文件 * @throws Exception */ @Test public void testUpload() throws Exception { Thread.sleep(2000); fs.copyFromLocalFile(new Path("F:/access.log"), new Path("/access.log.copy")); fs.close(); } /** * 下載文件 * @throws Exception */ @Test public void testDownload() throws Exception { fs.copyToLocalFile(new Path("/access.log.copy"), new Path("d:/")); fs.close(); } /** * 打印環境中的配置項 */ @Test public void testConf(){ Iterator<Entry<String, String>> iterator = conf.iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey() + "--" + entry.getValue());//conf加載的內容 } } /** * 建立目錄 */ @Test public void makdirTest() throws Exception { boolean mkdirs = fs.mkdirs(new Path("/aaa/bbb")); System.out.println(mkdirs); } /** * 刪除 */ @Test public void deleteTest() throws Exception{ boolean delete = fs.delete(new Path("/aaa"), true);//true, 遞歸刪除 System.out.println(delete); } /** * 遞歸顯示文件 * @throws Exception */ @Test public void listTest() throws Exception{ FileStatus[] listStatus = fs.listStatus(new Path("/")); for (FileStatus fileStatus : listStatus) { System.out.println(fileStatus.getPath()+"================="+fileStatus.toString()); System.out.println((fileStatus.isFile()?"file":"directory")); } //會遞歸找到全部的文件 RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true); while(listFiles.hasNext()){ LocatedFileStatus next = listFiles.next(); String name = next.getPath().getName(); Path path = next.getPath(); System.out.println(name + "---" + path.toString()); System.out.println((next.isFile()?"file":"directory")); System.out.println("blocksize: " +next.getBlockSize()); System.out.println("owner: " +next.getOwner()); System.out.println("Replication: " +next.getReplication()); System.out.println("Permission: " +next.getPermission()); System.out.println("Name: " +next.getPath().getName()); System.out.println("------------------"); BlockLocation[] blockLocations = next.getBlockLocations(); for(BlockLocation b:blockLocations){ System.out.println("塊起始偏移量: " +b.getOffset()); System.out.println("塊長度:" + b.getLength()); //塊所在的datanode節點 String[] datanodes = b.getHosts(); for(String dn:datanodes){ System.out.println("datanode:" + dn); } } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://master:9000"); //拿到一個文件系統操做的客戶端實例對象 FileSystem fs = FileSystem.get(conf); fs.copyFromLocalFile(new Path("E:/access.log"), new Path("/access.log.copy")); fs.close(); } }
運行測試結果(按照上面的配置所有成功), 若是配置路徑中帶有空格啥的,可能報找不到hadoop命令路徑、jdk路徑。
3、hadoop文件系統(FileSystem實現類型)
最後寄語,以上是博主本次文章的所有內容,若是你們以爲博主的文章還不錯,請點贊;若是您對博主其它服務器大數據技術或者博主本人感興趣,請關注博主博客,而且歡迎隨時跟博主溝通交流。