由于使用 sort by (order by, distribute by, cluster by 等)对同一个 ID 根据时间进行排序时,并不是按照真正的时间进行排序(sort by , 其实是对每个reduce中对数据进行排序,也就是执行一个局部排序过程)。故使用 sort_array 对数据排序。
示例如下:
select
sessionid,
regexp_replace(
concat_ws('###===###', sort_array(
collect_list(concat_ws('=', cast(time as string), content)
)
)
), '[0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}} [0-9]{{2}}:[0-9]{{2}}:[0-9.]{{2,4}}=', '') as session_seq_text
from
(
select sessionid, content, time from your_self_hive_table
where dt='2020-06-01' and length(content)>0 and score>=0.5
) x
group by sessionid
;