HDFS 使用java api实现上传/下载/删除文件

6 篇文章 0 订阅
4 篇文章 0 订阅
package hadoop;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


public class HDFSTest01 {
	
	/**
	 * 文件上传
	 * @param src 
	 * @param dst
	 * @param conf
	 * @return
	 */
	public static boolean put2HSFS(String src , String dst , Configuration conf){
		Path dstPath = new Path(dst) ;
		try{
			FileSystem hdfs = dstPath.getFileSystem(conf) ;
//			FileSystem hdfs = FileSystem.get( URI.create(dst), conf) ;
			hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
		}catch(IOException ie){
			ie.printStackTrace() ;
			return false ;
		}
		return true ;
	}
	
	/**
	 * 文件下载
	 * @param src
	 * @param dst
	 * @param conf
	 * @return
	 */
	public static boolean getFromHDFS(String src , String dst , Configuration conf){
		Path dstPath = new Path(dst) ;
		try{
			FileSystem dhfs = dstPath.getFileSystem(conf) ;
			dhfs.copyToLocalFile(false, new Path(src), dstPath) ;
		}catch(IOException ie){
			ie.printStackTrace() ;
			return false ;
		}
		return true ;
	}
	
	/**
	 * 文件检测并删除
	 * @param path
	 * @param conf
	 * @return
	 */
	public static boolean checkAndDel(final String path , Configuration conf){
		Path dstPath = new Path(path) ;
		try{
			FileSystem dhfs = dstPath.getFileSystem(conf) ;
			if(dhfs.exists(dstPath)){
				dhfs.delete(dstPath, true) ;
			}else{
				return false ;
			}
		}catch(IOException ie ){
			ie.printStackTrace() ;
			return false ;
		}
		return true ;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String src = "hdfs://xcloud:9000/user/xcloud/input/core-site.xml" ;
		String dst = "hdfs://xcloud:9000/user/xcloud/out" ;
		String src = "/home/xcloud/cdh3/hbase-0.90.4-cdh3u2/bin/loadtable.rb" ;
		boolean status = false ;
		
		
		Configuration conf = new Configuration() ;
		status = put2HSFS( src ,  dst ,  conf) ;
		System.out.println("status="+status) ;
		
		src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;
		dst = "/tmp/output" ;
		status = getFromHDFS( src ,  dst ,  conf) ;
		System.out.println("status="+status) ;
		
		src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;
		dst = "/tmp/output/loadtable.rb" ;
		status = checkAndDel( dst ,  conf) ;
		System.out.println("status="+status) ;
	}


}


参考:

http://www.360doc.com/content/11/0406/18/11586_107636584.shtml

hadoop-0.20_程式设计.pdf


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值