Hdfs 的读写操作

说明:本测试使用maven管理项目结构,测试前,请把 core-site.xml 拷贝到resource目录
package hadoop.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Hdfstest {

	public Hdfstest() throws IOException{
		// 创建于hdfs的连接(操作)对象
		hdfs = FileSystem.get(conf) ; 
	}
	
	//配置hadoop配置信息对象
	public final Configuration conf = new Configuration();
	// 声明hdfs文件系统操作对象
	public FileSystem hdfs ;
	
	public void createHdfsFile(String path) throws IOException{
		
		Path p = new Path(path);
		// 根据path创建文件
		FSDataOutputStream od = hdfs.create(p) ;
		//od.writeUTF("hello" );
		od.write("gbdghrherghr".getBytes());
		
	}
	// 内容追加
public void appendHdfsFile(String path,String content) throws IOException{
		Path p = new Path(path) ;
		FSDataOutputStream os = hdfs.append(p) ;
		os.write(content.getBytes());
	}
	    // 文件读取
	public void readHdfsFile(String path) throws IOException{
		Path p = new Path(path) ;
		FSDataInputStream fp = hdfs.open(p) ;
		InputStreamReader isr = new InputStreamReader(fp) ;
		BufferedReader br = new BufferedReader(isr) ;
		String line = br.readLine() ;
		while(line !=null){
			System.out.println(line);
			line = br.readLine() ;
		}
	
	}
	    //  遍历输出hdfs目录
	public void listHdfsFiles(String path) throws IOException{
		Path p = new Path(path);
		//判断给定的(路径)目录是否存在
		boolean isExists = hdfs.exists(p) ;
		if(isExists){
	
			boolean idDirectory = hdfs.isDirectory(p) ;
			boolean isFile = hdfs.isFile(p) ;
			if(idDirectory){
				System.out.println(p);
				FileStatus[] fastatus = hdfs.listStatus(p) ;
				for(FileStatus fs : fastatus){
					Path cp = fs.getPath() ;
					if(path.equals("/")){
						listHdfsFiles(path+cp.getName()) ;
					}else{
						listHdfsFiles(path+"/"+cp.getName()) ;
					}
				}
			}else if(isFile){
				System.out.println(p);
			}
		}
	}
	     //  文件删除
	public void deleteHdfsFile(String path) throws IOException{
		Path p = new Path(path);
		hdfs.delete(p, true) ;
		
	}
	
	  // 文件上传
	public void uploadFile(String res,String des) throws IOException{
		Path resPath = new Path(res) ;
		Path desPath = new Path(des) ;
		hdfs.copyFromLocalFile(resPath, desPath);
	}
	  // 文件下载
	public void copyTOLocalFile(String res,String des) throws IOException{
		Path resPath = new Path(res) ;
		Path desPath = new Path(des) ;
		hdfs.copyToLocalFile(resPath, desPath);
	}
	
	public static void main(String[] args) throws IOException {
		Hdfstest dfstest =new Hdfstest();
	//	dfstest.createHdfsFile("/hdfstest");
		// 内容追加
	//	dfstest.appendHdfsFile("/hdfstest", "\n 34638767896");
		
	//	dfstest.readHdfsFile("/hdfstest") ;
		
	//	dfstest.listHdfsFiles("/") ;
		
	//	dfstest.deleteHdfsFile("/hdfstest") ;
		
	//	dfstest.uploadFile("D:\\KuGou","/user/root/input") ;
		dfstest.copyTOLocalFile("/user/root/input/KuGou","D:\\KuGou") ;
		
		dfstest.hdfs.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值