码迷,mamicode.com
首页 > Windows程序 > 详细

Hadoop HDFS编程 API入门系列之合并小文件到HDFS(三)

时间:2016-12-14 01:32:02      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:http   技术   name   nts   名称   pac   reg   系统   block   

 

  不多说,直接上代码。

技术分享

技术分享

技术分享

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 代码

package zhouls.bigdata.myWholeHadoop.HDFS.hdfs7;

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.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.io.IOUtils;
/**
* function 合并小文件至 HDFS
*
*
*/
public class MergeSmallFilesToHDFS
{
private static FileSystem fs = null; //定义文件系统对象,是HDFS上的
private static FileSystem local = null; //定义文件系统对象,是本地上的
/**
* @function main
* @param args
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws IOException,URISyntaxException
{
list();
}

/**
*
* @throws IOException
* @throws URISyntaxException
*/
public static void list() throws IOException, URISyntaxException
{
// 读取hadoop配置文件
Configuration conf = new Configuration();
// 文件系统访问接口和创建FileSystem对象,在本地上运行模式
URI uri = new URI("hdfs://HadoopMaster:9000");
fs = FileSystem.get(uri, conf);
// 获得本地文件系统
local = FileSystem.getLocal(conf);
// 过滤目录下的 svn 文件
FileStatus[] dirstatus = local.globStatus(new Path("D://Data/tvdata/*"),new RegexExcludePathFilter("^.*svn$"));
//获取D:\Data\tvdata目录下的所有文件路径
Path[] dirs = FileUtil.stat2Paths(dirstatus);
FSDataOutputStream out = null;
FSDataInputStream in = null;
for (Path dir : dirs)
{//比如拿2012-09-17为例
//将文件夹名称2012-09-17的-去掉,直接,得到20120901文件夹名称
String fileName = dir.getName().replace("-", "");//文件名称
//只接受20120917日期目录下的.txt文件
FileStatus[] localStatus = local.globStatus(new Path(dir+"/*"),new RegexAcceptPathFilter("^.*txt$"));
// 获得20120917日期目录下的所有文件
Path[] listedPaths = FileUtil.stat2Paths(localStatus);
// 输出路径
Path block = new Path("hdfs://HadoopMaster:9000/middle/tv/"+ fileName + ".txt");
System.out.println("合并后的文件名称:"+fileName+".txt");
// 打开输出流
out = fs.create(block);
//循环20120917日期目录下的所有文件
for (Path p : listedPaths)
{
in = local.open(p);// 打开输入流
IOUtils.copyBytes(in, out, 4096, false); // 复制数据
// 关闭输入流
in.close();
}
if (out != null)
{
// 关闭输出流
out.close();
}
//当循环完20120917日期目录下的所有文件之后,接着依次20120918,20120919,,,
}
}

/**
*
* @function 过滤 regex 格式的文件
*
*/
public static class RegexExcludePathFilter implements PathFilter
{
private final String regex;

public RegexExcludePathFilter(String regex)
{
this.regex = regex;
}


public boolean accept(Path path)
{
// TODO Auto-generated method stub
boolean flag = path.toString().matches(regex);
return !flag;
}

}

/**
*
* @function 接受 regex 格式的文件
*
*/
public static class RegexAcceptPathFilter implements PathFilter
{
private final String regex;

public RegexAcceptPathFilter(String regex)
{
this.regex = regex;
}


public boolean accept(Path path)
{
// TODO Auto-generated method stub
boolean flag = path.toString().matches(regex);
return flag;
}

}
}

Hadoop HDFS编程 API入门系列之合并小文件到HDFS(三)

标签:http   技术   name   nts   名称   pac   reg   系统   block   

原文地址:http://www.cnblogs.com/zlslch/p/6174553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!