首页 > 代码库 > 利用Hive实现求两条相邻数据时间差

利用Hive实现求两条相邻数据时间差

1.Hive row_number() 函数的高级用法 row_num 按照某个字段分区显示第几条数据

select imei,ts,fuel_instant,gps_longitude,gps_latitude,row_number() over (PARTITION BY imei ORDER BY ts ASC) as row_num from sample_data_2

2.row_num 是相互连续的,join 自身,然后时间相减可求差
create table obd_20140101 as

  select a.imei,a.row_num,a.ts,COALESCE(unix_timestamp(a.ts, ‘yyyy-MM-dd HH:mm:ss.S‘), 0) - unix_timestamp(b.ts, ‘yyyy-MM-dd HH:mm:ss.S‘) as intervel ,a.fuel_instant,a.gps_speed as obd_speed,a.gps_status,a.gps_longitude,a.gps_latitude,a.direct_angle,a.obdspeed from obddata_20140101 a join obddata_20140101 b on a.imei = b.imei and a.row_num = b.row_num +1

利用Hive实现求两条相邻数据时间差