gpt4 book ai didi

hive - 计算 Hive 中不包括星期日的天数

转载 作者:行者123 更新时间:2023-12-04 18:32:21 27 4
gpt4 key购买 nike

我有两个时间戳作为输入。我想计算这些时间戳之间不包括星期日的时差(以小时为单位)。

我可以使用 获得天数日期差异 hive 中的功能。

我可以使用 获取特定日期的日期from_unixtime(unix_timestamp(startdate), 'EEEE')。

但我不知道如何关联这些功能来实现我的要求,或者有没有其他简单的方法来实现这一点。

提前致谢。

最佳答案

您可以编写一个自定义 UDF,它将包含日期的两列作为输入并计算不包括星期日的日期之间的差异。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;

public class IsoYearWeek extends UDF {

public LongWritable evaluate(Text dateString,Text dateString1) throws ParseException { //takes the two columns as inputs
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
/* String date1 = "20/07/2016";
String date2 = "28/07/2016";
*/ int count=0;

List<Date> dates = new ArrayList<Date>();

Date startDate = (Date)date.parse(dateString.toString());
Date endDate = (Date)date.parse(dateString1.toString());
long interval = 24*1000 * 60 * 60; // 1 hour in millis
long endTime =endDate.getTime() ; // create your endtime here, possibly using Calendar or Date
long curTime = startDate.getTime();
while (curTime <= endTime) {
dates.add(new Date(curTime));
curTime += interval;
}
for(int i=0;i<dates.size();i++){
Date lDate =(Date)dates.get(i);
if(lDate.getDay()==0){
count+=1; //counts the number of sundays in between
}
}

long days_diff = (endDate.getTime()-startDate.getTime())/(24 * 60 * 60 * 1000)-count; //displays the days difference excluding sundays
return new LongWritable(days_diff);

}

}

关于hive - 计算 Hive 中不包括星期日的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com