简体   繁体   English

如何从当前日期减去一天,然后在Hive中转换为字符串

[英]How to subtract one day from current date then convert to string in Hive

Here is the case. 情况就是这样。 I'm trying to make select syntax to get data from last day (today we have 21.10 so as a result I should have data with 20.10 date query will be a part of ETL proces in Talend so I can't simply do where date = '2016-10-20' ) The problem is that all columns in data source are in VARCHAR or STRING type - date also. 我正在尝试使用select语法从最后一天获取数据(今天我们有21.10,因此我应该有20.10日期查询的数据将成为Talend中ETL过程的一部分所以我不能简单地做到where date = '2016-10-20' )问题是数据源中的所有列都是VARCHAR或STRING类型 - 日期也是。 Source is on Hive Hadoop. 来源是Hive Hadoop。

My code: 我的代码:

select 
cast(to_date(from_unixtime(unix_timestamp(dzien ,'yyyyMMdd'), 'yyyy-MM-dd')) as date),
count(ns_utc) as ILOSC_ODSLON
from portal.portal_data 
where
portal_data.opl_ev_ty is null 
and portal_data.opl_ev_as is null
and cast(to_date(from_unixtime(unix_timestamp(dzien ,'yyyyMMdd'), 'yyyy-MM-dd')) as date) = CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day

GROUP BY 
cast(to_date(from_unixtime(unix_timestamp(dzien ,'yyyyMMdd'), 'yyyy-MM-dd')) as date)

With that code query returns nothing exept columns name. 使用该代码查询返回没有exept列名称。 The problem is probably with this part = CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day . 问题可能是这个部分= CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day

I made some tests. 我做了一些测试。 When I'm running this query 当我运行此查询时

select CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day

result is 2016-10-20 00:00:00.0 and part 00:00:00.0 probably ruins my query, becasue when in main query instead of = CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day I'm putting condition = '2016-10-20' result is as expected. 结果是2016-10-20 00:00:00.0和00:00:00.0可能会破坏我的查询,因为在主查询中而不是= CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()))as date) - interval '1' day我把条件= '2016-10-20'结果如预期的那样。

Can you please guide me how to solve this problem? 你能指导我如何解决这个问题吗?

Instead of Hue I'm using SQL Workbench 而不是Hue我正在使用SQL Workbench

once you parsed the date then use date_sub function that is available in hive 一旦解析了日期,就可以使用hive中提供的date_sub函数

date_sub(string startdate, int days) 

date_sub('2008-12-31', 1) = '2008-12-30'

You can even follow the link below. 您甚至可以点击以下链接。

https://www.qubole.com/resources/cheatsheet/hive-function-cheat-sheet/ https://www.qubole.com/resources/cheatsheet/hive-function-cheat-sheet/

DATE_SUB is Available in HIVE 2.1.0 DATE_SUB在HIVE 2.1.0中可用

date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

Subtracts a number of days to startdate: date_sub('2008-12-31', 1) = '2008-12-30'. 减去开始日期的天数:date_sub('2008-12-31',1)='2008-12-30'。

Prior to Hive 2.1.0 (HIVE-13248) the return type was a String because no Date type existed when the method was created. 在Hive 2.1.0(HIVE-13248)之前,返回类型是一个String,因为创建方法时不存在Date类型。

问题是你试图从date减去一天的方式。我建议从where子句中的unix时间戳减去一天中的秒数(86400) -

CAST(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP()-86400))as date) 

For versions >= Hive 2.0 Try this: 对于版本> = Hive 2.0试试这个:

select current_date;

Then try this: 然后尝试这个:

select date_sub(current_date, 1);

It should give you current date minus 1 day. 它应该给你当前日期减去1天。

cast(date_sub(CURRENT_DATE,1)as string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM