Hadoop面试查询-Mapreduce-Pig-Hive

Hadoop interview query-Mapreduce-Pig-Hive

这是我在 hadoop 面试中问到的问题。 我有如下 table 数据。

我买了一辆新自行车 第一天我走了 20 公里的距离 第二天,仪表读数为 50(第 1 天 + 第 2 天) 第 3 天仪表读数为 60(第 1 天+第 2 天+第 3 天)

Day Distance
1    20
2    50
3    60

现在的问题是,我希望输出如下所示

Day  Distance
1    20
2    30
3    10

即我希望仅在第 1 天、第 2 天和第 3 天行驶的距离。

答案可以在Hive/Pig/MapReduce.

谢谢

这是一个 运行 类总计问题,您可以通过此 Hive 查询解决它

with b as (
select 0 as d, 0 as dst
union all 
select d, dst from mytable
)
SELECT a.d, a.km-b.km new_dst from mytable a, b 
where a.d-b.d==1

您可以使用 Hive 的内置窗口和分析功能来获得所需的结果。

这是一种方法。

SELECT day, NVL(CAST(distance-LAG(distance) OVER (ORDER BY day) AS INT),20) 
FROM table;

我在 map reduce 中尝试过。 包 hadoop;

public class distance {

public static class disMapper extends Mapper<LongWritable,Text,IntWritable,IntWritable>
{
    //1 20
    int pValue=0;
    IntWritable outkey=new IntWritable();
    IntWritable outvalue=new IntWritable();
    public void map(LongWritable key,Text values,Context context) throws IOException, InterruptedException
    {
        String cols[]=values.toString().split("\t");
        int dis=Integer.parseInt(cols[1])-pValue;
        outkey.set(Integer.parseInt(cols[0]));
        outvalue.set(dis);
        pValue=Integer.parseInt(cols[1]);
        context.write(outkey, outvalue);
    }
}

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    // TODO Auto-generated method stub
    Configuration conf=new Configuration();
    Job job =new Job(conf,"dfdeff");
    job.setJarByClass(distance.class);
    job.setMapperClass(disMapper.class);

    job.setMapOutputKeyClass(IntWritable.class);
    job.setMapOutputValueClass(IntWritable.class);

    job.setNumReduceTasks(0);

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    System.exit(job.waitForCompletion(true)?1:0);

}

}