복붙노트

[HADOOP] hive에서 hh : mm : ss의 평균을 찾는 방법

HADOOP

hive에서 hh : mm : ss의 평균을 찾는 방법

script_name, start_time, end_time, duration 열이있는 하이브 테이블이 있다고 가정하십시오. 시작 시간, 종료 시간 및 지속 시간은 hh : mm : ss 형식입니다. 내 요구 사항은 지난 7 일 동안이 열의 평균 시간을 찾아 파일에 저장하는 것입니다.

해결법

  1. ==============================

    1.unix_timestamp로 변환, 합계, 3으로 나누기, bigint로 변환 및 HH : mm : ss로 다시 변환 :

    unix_timestamp로 변환, 합계, 3으로 나누기, bigint로 변환 및 HH : mm : ss로 다시 변환 :

    with data as --Data example. Use your table instead
    (select '12:10:30' start_time,'01:10:00' end_time, '02:10:00' duration)
    
    select from_unixtime(cast((unix_timestamp(start_time,'HH:mm:ss')+ unix_timestamp(end_time,'HH:mm:ss')+unix_timestamp(duration,'HH:mm:ss'))/3 as bigint),'HH:mm:ss') from data;
    

    결과:

    05:10:10
    

    여기에서 테스트를 참조하십시오 : http://demo.gethue.com/hue/editor?editor=285484&type=hive

    단일 열의 경우 :

    유닉스 타임 스탬프로 변환하고 초 단위로 평균을 계산하고 bigint로 변환하십시오 (평균은 두 배이며 초 정밀도 손실의 일부가 있습니다). 그리고 마지막으로 문자열 시간 형식으로 다시 변환하십시오.

    with data as --Data example. Use your table instead
    (select stack(2,'12:10:30','01:10:00') as timeStr)
    
    select from_unixtime(cast(avg(unix_timestamp(timeStr,'HH:mm:ss'))as bigint),'HH:mm:ss') from data;
    

    결과:

    06:40:15
    

    여기서 테스트를 참조하십시오 : http://demo.gethue.com/hue/editor?editor=285464&type=hive

  2. from https://stackoverflow.com/questions/55020821/how-to-find-the-average-of-hhmmss-in-hive by cc-by-sa and MIT license