多个文件作为Hadoop Dfs和mapreduce的输入

时间:2017-10-29 06:18:10

标签: java hadoop mapreduce hdfs

通常我们在一个文本文件中输入java文件(比如简单的单词计数问题)。相反,现在我有100个csv文件,我想把它作为我的java代码的输入。(全部文件不能简单地合并为1个单个文件。。要预测给定100个股票的最大/最小股票波动率,因此每个csv文件都是唯一的。 那么,如何将整个csv文件夹作为输入流提供给java程序。

2 个答案:

答案 0 :(得分:2)

解决方案1:为了解决这个问题,我们可以使用FileInputFormat.addInputPaths()方法,它可以采用逗号分隔的多个输入列表,我们可以将其写为

FileInputFormat.addInputPaths(“file0,file1,....”)

假设需要分析2个文件以及使用Facebook和youtube服务的人员列表(需要单个输出文件)

我们有两个文件facebook.txt和youtube.txt

Path YoutubePath = new Path(args[0]);
Path FacebookPath = new Path(args[1]);
Path outputPath = new Path(args[2]);
MultipleInputs.addInputPath(job, FacebookPath, TextInputFormat.class, JoinFacebookMapper.class);
MultipleInputs.addInputPath(job, YoutubePath, TextInputFormat.class, YoutubeMapper.class);
FileOutputFormat.setOutputPath(job, outputPath);

在代码中添加以下行将产生在单个map reduce作业中传递的多个文件。

您可以将整个文件夹作为参数传递

答案 1 :(得分:0)

这是我的测试代码,将许多文件复制到hdfs并合并它们,它也可以过滤其他文件格式,我想它可能对你有帮助!

public class FilesMergeToHDFS {
private static FileSystem fs = null;
private static FileSystem local = null;

public static void main(String[] args) throws IOException, URISyntaxException {
    // TODO Auto-generated method stub
    list();
}

private static void list() throws IOException, URISyntaxException {
    // TODO Auto-generated method stub

            Configuration conf = new Configuration();   
            URI uri = new URI("hdfs://xxx:9000");//HDFS address
            fs = FileSystem.get(uri,conf);


            local = FileSystem.getLocal(conf);

            FileStatus[] dirsStatus = local.globStatus(new Path("E://data/73/*"), new RegexExcludePathFilter("^.*svn$"));
            Path[] dirs = FileUtil.stat2Paths(dirsStatus);
            FSDataInputStream in = null;
            FSDataOutputStream out = null;
            for(Path p:dirs){
                    //upload
                String filename = p.getName();
                FileStatus[] localStatus = local.globStatus(new Path(p+"/*"),new RegexAcceptPathFilter("^.*txt$"));
                Path[] listedPaths = FileUtil.stat2Paths(localStatus);
                //set outputpath
                Path block = new Path("hdfs://hadoop:9000/mergehdfs/filesmerge/"+filename+".txt");
                out =fs.create(block);
                for(Path path:listedPaths){
                    in = local.open(path);
                    IOUtils.copyBytes(in, out, 4096, false); // copydata
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
}

private static class RegexAcceptPathFilter implements PathFilter {

private final String regex;

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

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

}

private static class RegexExcludePathFilter  implements PathFilter {
private final String regex;
    public RegexExcludePathFilter (String regex) {
        this.regex = regex;
    }

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