1. 程式人生 > >Hdfs的JAVA客戶端基本操作

Hdfs的JAVA客戶端基本操作

一:需要的jar包:

hadoop-2.4.1\share\hadoop\hdfs\hadoop-hdfs-2.4.1.jar
hadoop-2.4.1\share\hadoop\hdfs\lib\所有jar包

hadoop-2.4.1\share\hadoop\common\hadoop-common-2.4.1.jar
hadoop-2.4.1\share\hadoop\common\lib\所有jar包

二:連線HDFS和客戶端

HDFS環境配置可以參考這篇部落格:HDFS環境配置

public class HdfsUtil {
	public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
//		構造一個配置引數封裝物件
		Configuration conf = new Configuration();
//		構造一個hdfs的客戶端
		FileSystem fs=FileSystem.get(new URI("hdfs://192.168.77.70:9000"), conf, "root");
//		用hdfs檔案系統的客戶端物件fs來操作檔案,比如上傳一個檔案
		fs.copyFromLocalFile(new Path("C:/jdk-7u65-linux-i586.tar.gz"), new Path("/"));
		fs.close();
	}
}

三:Java客戶端基本操作:

public class HdfsUtil {
	FileSystem fs=null;
	
	@Before
	public void init() throws IOException, InterruptedException, URISyntaxException{
//     構造一個配置引數封裝物件
		Configuration conf = new Configuration();
//		構造一個hdfs的客戶端
		fs=FileSystem.get(new URI("hdfs://192.168.77.70:9000"), conf, "root");
	}
	
	/*
	 * 從本地上傳檔案到hdfs中
	 */
	@Test
	public void testUpload() throws IllegalArgumentException, IOException{
		fs.copyFromLocalFile(new Path("C:/jdk-7u65-linux-i586.tar.gz"), new Path("/"));
		fs.close();
	}
	
	/*
	 * 從hdfs中下載檔案到本地
	 */
	@Test
	public void testDownload() throws IllegalArgumentException, IOException{
		fs.copyToLocalFile(false, new Path("/jdk-7u65-linux-i586.tar.gz"), new Path("C:/"), true);
		fs.close();
	}
	
	/*
	 * 資料夾操作
	 */
	@Test
	public void testDir() throws IllegalArgumentException, IOException{
		fs.mkdirs(new Path("/aaa"));
		System.out.println("建立了一個資料夾:/aaa");
		
		boolean exists = fs.exists(new Path("/aaa"));
		System.out.println("/aaa資料夾存在否?"+exists);
		
		fs.copyFromLocalFile(new Path("C:/input.txt"), new Path("/aaa"));
		System.out.println("成功上傳了一個檔案到/aaa目錄下");
		
		fs.delete(new Path("/aaa"), true);
		System.out.println("已經將/aaa目錄刪除");
		
		boolean exists2 = fs.exists(new Path("/aaa"));
		System.out.println("/aaa資料夾存在否?"+exists2);
		fs.close();
	}
	
	/*
	 * 檔案資訊檢視
	 */
	@Test
	public void testFileStatus() throws FileNotFoundException, IllegalArgumentException, IOException{
		//只能列出檔案資訊
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
		while(listFiles.hasNext()){
			LocatedFileStatus fileStatus = listFiles.next();
			System.out.println(fileStatus.getPath().getName());
		}
		
		System.out.println("-----------------------");
		//能列出檔案和資料夾資訊
		FileStatus[] listStatus = fs.listStatus(new Path("/"));
		for(FileStatus f:listStatus){
			String type="-";
			if(f.isDirectory()) type="d";
			System.out.println(type+"\t"+f.getPath().getName());
		}
		fs.close();
	}
	
	@Test
	public void testOthers() throws IllegalArgumentException, IOException{
		//檔案偏移量資訊
		BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(new Path("/jdk-7u65-linux-i586.tar.gz"), 0, 143588167);
		for(BlockLocation location : fileBlockLocations){
			System.out.println(location.getOffset());
			System.out.println(location.getNames()[0]);
		}
		
		//修改檔名
		fs.rename(new Path("/jdk-7u65-linux-i586.tar.gz"), new Path("/jdk-7u65-linux-i586.tgz"));
		
		//修改一個檔案的副本數量
		fs.setReplication(new Path("/jdk-7u65-linux-i586.tgz"), (short)2);
		fs.close();
	}

}

四:Java客戶端IO流操作:

package hdfsUtil;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

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.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;


public class HdfsIO {
	FileSystem fs=null;
	
	@Before
	public void init() throws IOException, InterruptedException, URISyntaxException{
//	     構造一個配置引數封裝物件
		Configuration conf = new Configuration();
//			構造一個hdfs的客戶端
		fs=FileSystem.get(new URI("hdfs://192.168.77.70:9000"), conf, "root");
	}
	
	/*
	 * 下載檔案
	 */
	@Test
	public void testDownload() throws IllegalArgumentException, IOException{
		FSDataInputStream in = fs.open(new Path("/jdk-7u65-linux-i586.tgz"));
		FileOutputStream out=new FileOutputStream("C:/jdk.tgz");
		IOUtils.copyBytes(in,out,new Configuration());
		IOUtils.closeStream(in);
		IOUtils.closeStream(out);
		fs.close();
	}
	
	/*
	 * 上傳檔案
	 */
	@Test
	public void testUpload() throws IllegalArgumentException, IOException{
		FileInputStream in=new FileInputStream("c:/jdk.tgz");
		FSDataOutputStream out = fs.create(new Path("/jdk.tar.gz"));
		IOUtils.copyBytes(in, out, new Configuration());
		IOUtils.closeStream(in);
		IOUtils.closeStream(out);
		fs.close();
	}
	
	/*
	 * 從指定偏移量讀取hdfs中的檔案資料
	 * 在分散式資料處理時,可以將資料分片來分配給不同的節點處理
	 */
	@Test
	public void testSeek() throws IllegalArgumentException, IOException{
		FSDataInputStream in = fs.open(new Path("/test.txt"));
		in.seek(6);//定位,設定起始偏移量
		FileOutputStream out=new FileOutputStream("c:/test.seg.txt");
		IOUtils.copyBytes(in, out, new Configuration());
		IOUtils.closeStream(in);
		IOUtils.closeStream(out);
		fs.close();
	}
}