hadoop - 从mapreduce中读取Hive表

标签 hadoop mapreduce hive

我目前正在编写一个mapreduce 程序来查找两个配置单元表之间的差异。 我的配置单元表按一列或多列进行分区。因此文件夹名称包含分区列的值。

有没有办法读取hive分区表。

可以在mapper中读取吗?

最佳答案

由于底层 HDFS 数据默认在分区 Hive 表中组织为

 table/root/folder/x=1/y=1
 table/root/folder/x=1/y=2
 table/root/folder/x=2/y=1
 table/root/folder/x=2/y=2....,

您可以在驱动程序中构建每个输入路径,并通过多次调用 FileInputFormat.addInputPath(job, path) 添加它们。您构建的每个文件夹路径一次调用。

下面粘贴了示例代码。请注意如何将路径添加到 MyMapper.class。在此示例中,我使用 MultipleInputs API。表按“part”和“xdate”分区。

public class MyDriver extends Configured implements Tool {
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        conf.set("mapred.compress.map.output", "true");
        conf.set("mapred.output.compression.type", "BLOCK"); 

        Job job = new Job(conf);
        //set up various job parameters
        job.setJarByClass(MyDriver.class);
        job.setJobName(conf.get("job.name"));
        MultipleInputs.addInputPath(job, new Path(conf.get("root.folder")+"/xdate="+conf.get("start.date")), TextInputFormat.class, OneMapper.class);
        for (Path path : getPathList(job,conf)) {
            System.out.println("path: "+path.toString());
            MultipleInputs.addInputPath(job, path, Class.forName(conf.get("input.format")).asSubclass(FileInputFormat.class).asSubclass(InputFormat.class), MyMapper.class);
        }
        ...
        ...
        return job.waitForCompletion(true) ? 0 : -2;

    }

    private static ArrayList<Path> getPathList(Job job, Configuration conf) {
        String rootdir = conf.get("input.path.rootfolder");
        String partlist = conf.get("part.list");
        String startdate_s = conf.get("start.date");
        String enxdate_s = conf.get("end.date");
        ArrayList<Path> pathlist = new ArrayList<Path>();
        String[] partlist_split = partlist.split(",");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startdate_d = null;
        Date enxdate_d = null;
        Path path = null;
        try {
            startdate_d = sdf.parse(startdate_s);
            enxdate_d = sdf.parse(enxdate_s);
            GregorianCalendar gcal = new GregorianCalendar();
            gcal.setTime(startdate_d);
            Date d = null;
            for (String part : partlist_split) {
                gcal.setTime(startdate_d);
                do {
                    d = gcal.getTime();
                    FileSystem fs = FileSystem.get(conf);
                    path = new Path(rootdir + "/part=" + part + "/xdate="
                            + sdf.format(d));
                    if (fs.exists(path)) {
                        pathlist.add(path);
                    }
                    gcal.add(Calendar.DAY_OF_YEAR, 1);
                } while (d.before(enxdate_d));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pathlist;
    }

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new MyDriver(), args);
        System.exit(res);
    }
}

关于hadoop - 从mapreduce中读取Hive表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16191477/

相关文章:

performance - 如何优化对大型数据集的查询?

sql - 如何在 Hive 中一次查询多个表?

hadoop - MapReduce 中是否需要 key ?

hadoop - 为什么 Mahout 还没有线性回归

hadoop - Hive 查询语言仅返回与另一个表中的值不同的值

hadoop - Amazon Elastic Cloud 无法在子网上启动

hadoop - Hadoop:错误执行编译WordCount

hadoop - 彼此之间在一分钟之内发生的 pig 重复数据删除事件

java - Tez在Hadoop-2.5.2集群上崩溃

hadoop - 如何确定配置单元中的存储桶数