windows環境配置:
1.下載winutils的windows版本
GitHub上,有人提供了winutils的windows的版本,
項目地址是:https://github.com/srccodes/hadoop-common-2.2.0-bin,直接下載此項目的zip包,下載后是文件名是hadoop-common-2.2.0-bin-master.zip,
2016-12-14更新
在hadoop2.7.x版本里,請使用:https://github.com/SweetInk/hadoop-common-2.7.1-bin
隨便解壓到一個目錄,將加壓出來的bin文件夾覆蓋hadoop壓縮包解壓出來的bin目錄。
2.配置環境變量
增加用戶變量HADOOP_HOME,值是下載的zip包解壓的目錄,然后在系統變量path里增加%HADOOP_HOME%\bin 。
3、壓縮包里的hadoop.dll,并拷貝到c:\windows\system32目錄中。
如果下載的版本與hadoop不對應,或者windows環境沒配置好,會報錯:
1、java.io.IOException: (null) entry in command string: null chmod 0644
2、java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO
實例代碼:
package filedemo;import java.io.FileInputStream;
import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataOutputStream;
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;public class HdfsUtil {FileSystem fs=null;//@Before 是junit 測試框架@Beforepublic void init() throws Exception{/** * 注意注意注意* windows上eclipse上運行,hadoop的bin文件下windows環境編譯的文件和hadoop版本關系緊密* 如果報錯,或者api無效,很可能是bin下hadoop文件問題* *///讀取classpath下的core/hdfs-site.xml 配置文件,并解析其內容,封裝到conf對象中//如果沒有會讀取hadoop里的配置文件Configuration conf=new Configuration();//也可以在代碼中對conf中的配置信息進行手動設置,會覆蓋掉配置文件中的讀取的值//設置文件系統為hdfs。如果設置這個參數,那么下面的方法里的路徑就不用寫hdfs://hello110:9000///conf.set("fs.defaultFS", "hdfs://hello110:9000/");//根據配置信息,去獲取一個具體文件系統的客戶端操作實例對象fs=FileSystem.get(new URI("hdfs://hadoop1:9000/"), conf,"hadoop");Iterator<Entry<String, String>> iterator = conf.iterator();while(iterator.hasNext()){Entry<String, String> ent=iterator.next();System.out.println(ent.getKey()+" : "+ent.getValue());}System.out.println("-----------------/hdfs-site.xml 文件讀取結束----------------");}@Testpublic void listFiles() throws Exception{//RemoteIterator 遠程迭代器RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);while(listFiles.hasNext()){LocatedFileStatus file = listFiles.next();Path path = file.getPath();//String fileName=path.getName();System.out.println(path.toString());System.out.println("權限:"+file.getPermission());System.out.println("組:"+file.getGroup());System.out.println("文件大小:"+file.getBlockSize());System.out.println("所屬者:"+file.getOwner());System.out.println("副本數:"+file.getReplication());BlockLocation[] blockLocations = file.getBlockLocations();for(BlockLocation bl:blockLocations){System.out.println("塊起始位置:"+bl.getOffset());System.out.println("塊長度:"+bl.getLength());String[] hosts = bl.getHosts();for(String h:hosts){System.out.println("塊所在DataNode:"+h);}}System.out.println("*****************************************");}System.out.println("-----------------華麗的分割線----------------");FileStatus[] listStatus = fs.listStatus(new Path("/"));for(FileStatus status:listStatus){String name=status.getPath().getName();System.out.println(name+(status.isDirectory()?" is Dir":" is File"));}}//比較低層的寫法@Testpublic void upload() throws Exception{System.out.println("-----------------upload----------------");Path path = new Path("/testdata/testFs.txt");FSDataOutputStream os = fs.create(path);FileInputStream is = new FileInputStream("d:/testFS.txt");IOUtils.copy(is, os);}//封裝好的方法@Testpublic void upload2() throws Exception{System.out.println("-----------------upload2----------------");//hdfs dfs -copyFromLocal 從本地拷貝命令fs.copyFromLocalFile(new Path("d:/testFS-2.txt"), new Path("/testdata/testFs6.txt"));//如果windows-hadoop環境不行,可以用下面的api,最后的true是:用java的io去寫。//fs.copyToLocalFile(false, new Path("d:/testFS-2.txt"), new Path("/testdata/testFs6.txt"), true);fs.close();}@Testpublic void download() throws Exception{//hdfs dfs -copyToLocal 拷貝到本地命令fs.copyToLocalFile(new Path("/testdata/testFs6.txt") , new Path("d:/testFS3.txt"));//如果windows-hadoop環境不行,可以用下面的api,最后的true是:用java的io去寫。//fs.copyToLocalFile(false, new Path("/testdata/testFs6.txt"), new Path("d:/testFS3.txt"), true);}@Testpublic void mkdir() throws Exception{System.out.println("-----------------mkdir----------------");fs.mkdirs(new Path("/testdata/a123"));fs.mkdirs(new Path("/testdata/a124"));}@Testpublic void rm() throws Exception{//true:如果是目錄也會刪除。false,遇到目錄則報錯fs.delete(new Path("hdfs://hello110:9000/testdata/a124"), true);}}
package filedemo;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;/*** 用流的方式來操作hdfs上的文件* 可以實現讀取指定偏移量范圍的數據* @author**/
public class HdfsStreamAccess {FileSystem fs = null;Configuration conf = null;@Beforepublic void init() throws Exception{conf = new Configuration();//拿到一個文件系統操作的客戶端實例對象
// fs = FileSystem.get(conf);//可以直接傳入 uri和用戶身份fs = FileSystem.get(new URI("hdfs://mini1:9000"),conf,"hadoop");}/*** 通過流的方式上傳文件到hdfs* @throws Exception*/@Testpublic void testUpload() throws Exception {FSDataOutputStream outputStream = fs.create(new Path("/angelababy.love"), true);FileInputStream inputStream = new FileInputStream("c:/angelababy.love");IOUtils.copy(inputStream, outputStream);}/*** 通過流的方式獲取hdfs上數據* @throws Exception*/@Testpublic void testDownLoad() throws Exception {FSDataInputStream inputStream = fs.open(new Path("/angelababy.love")); FileOutputStream outputStream = new FileOutputStream("d:/angelababy.love");IOUtils.copy(inputStream, outputStream);}@Testpublic void testRandomAccess() throws Exception{FSDataInputStream inputStream = fs.open(new Path("/angelababy.love"));inputStream.seek(12);FileOutputStream outputStream = new FileOutputStream("d:/angelababy.love.part2");IOUtils.copy(inputStream, outputStream);}/*** 顯示hdfs上文件的內容* @throws IOException * @throws IllegalArgumentException */@Testpublic void testCat() throws IllegalArgumentException, IOException{FSDataInputStream in = fs.open(new Path("/angelababy.love"));IOUtils.copy(in, System.out);}}