HadoopFileUtil

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

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.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.log4j.Logger;



public class HadoopFileUtil {
static Logger logger = Logger.getLogger(HadoopFileUtil.class);
/**
* @param args
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
String[] otherArgs = null;
try {
otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
String src=args[0];
String dst=args[1];
String tag=args[2];
HadoopFileUtil util=new HadoopFileUtil();
if(tag!=null&&tag.equals("1")){
System.out.println(util.createFile(src, dst));
}
else{
util.deleteFile(dst);
}

}

/**
* 拷贝一个本地文件到hadoop里面
* @param localFile 本地文件和路径名
* @param hadoopFile hadoop文件和路径名
* @return
*/
public boolean createFile(String localFile,String hadoopFile){
try {
Configuration conf=new Configuration();
FileSystem src=FileSystem.getLocal(conf);
FileSystem dst= FileSystem.get(conf);
Path srcpath = new Path(localFile);
Path dstpath = new Path(hadoopFile);
FileUtil.copy(src, srcpath, dst, dstpath,false,conf);
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}


/**将一个流作为输入,生成一个hadoop里面的文件
* @param inStream 输入流
* @param hadoopFile hadoop路径及文件名字
* @return
*/
public boolean createFileByInputStream(InputStream inStream,String hadoopFile){
try {
Configuration conf=new Configuration();
FileSystem dst= FileSystem.get(conf);
Path dstpath = new Path(hadoopFile);
FSDataOutputStream oStream=dst.create(dstpath);
byte[] buffer = new byte[400];
int length = 0;
while((length = inStream.read(buffer))>0){
oStream.write(buffer,0,length);
}
oStream.flush();
oStream.close();
inStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除hadoop里面的一个文件
* @param hadoopFile
* @return
*/
public boolean deleteFile(String hadoopFile){
try {
Configuration conf=new Configuration();
FileSystem dst= FileSystem.get(conf);
FileUtil.fullyDelete(dst,new Path(hadoopFile));
} catch (Exception e) {
e.printStackTrace();
return false;
}

return true;
}
/**
* 从hadoop中读取一个文件流
* @param hadoopFile
* @return
*/
public FSDataInputStream getInputStream(String hadoopFile){
FSDataInputStream iStream=null;
try {
Configuration conf=new Configuration();
FileSystem dst= FileSystem.get(conf);
Path p=new Path(hadoopFile);
iStream=dst.open(p);
} catch (Exception e) {
e.printStackTrace();
logger.error("getInputStream error:", e);
}
return iStream;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值