【问题标题】:SQL Modeling in Map reduceMap reduce 中的 SQL 建模
【发布时间】:2017-02-07 04:43:05
【问题描述】:

我正在尝试对 SQL 查询建模,例如 select distinct (col1) from table where col2= value2 in map reduce。我使用的逻辑是每个映射器将检查 where 子句,如果找到匹配项,它将发出 where 子句值作为键和 col1 作为值。基于默认的散列函数,所有输出都将与 where 子句中的 key used value 进入相同的 reducer。在 reducer 中,我可以排除重复值并发出不同的值。这是正确的做法吗?

这是实现此功能的正确方法吗?

注意:此查询的数据在 CSV 文件中。

【问题讨论】:

  • 你试过Hive吗?
  • 我需要使用 map reduce 框架来完成。我使用的逻辑是每个映射器都将检查 where 子句,如果匹配将发出 where 子句作为键和 col1 作为值。基于默认的散列函数,所有的输出都会去同一个reducer。在 reducer 中,我可以排除重复并发出不同的值。这是正确的方法吗?

标签: hadoop mapreduce hdfs bigdata


【解决方案1】:
//MAPPER pseudo code
public static class DistinctMapper extends  Mapper<Object, Text, Text, NullWritable> {
        private Text col1 = new Text();
        private Text col2 = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

            // Logic to extract columns
            String C1  = extractColumn(value);
            String C2  = extractColumn(value);


            if (C2 != 'WhereCluaseValue') {  // filter value
                return;
            }
            // Mapper output key to the distinct column value
            col1.set(C1);
            // Mapper value as NULL
            context.write(col1, NullWritable.get());
        }
    }

//REDUCER pseudo code
public static class DistinctReducer extends Reducer<Text, NullWritable, Text, NullWritable> {
        public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
            // distinct column with a null value
            //Here we are not concerned about the list of values
            context.write(key, NullWritable.get());
        }
}

【讨论】:

    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多