问题描述
数据表
Name Company Continent country state district
Tom HP Asia India Assam Kdk
George SAP Africa Sudan Chak ksk
Bill EBAY Europe Denmark Lekh Sip
Charles WM Asia India Haryana Jhat
Chip WM Asia India Punjab Chista
Chia WM Asia India Punjab Mast
规则表
Continent country state district Pass
Asia India ALL ALL Yes
Asia India Punjab ALL NO
Asia India Punjab Mast Yes
我在Hive中有两个表。根据规则,我必须过滤掉数据表中的数据。
规则表中有一列称为pass,它确定是否需要过滤数据表中的记录。
在此示例中,存在不同种类的规则。它们是广义的和狭义的。 狭窄级别的规则不应影响广义级别的规则。这意味着狭窄级别的规则是广义级别的规则的例外。 例如:在规则表中,有3条记录。第一条记录是更广泛级别的规则。其他则处于狭窄水平。
第一条规则说,要通过所有具有印度(国家),国家(任何/全部)和地区(任何/全部)的记录。 第二条规则说不要通过所有具有国家(如印度),州(如旁遮普邦和地区)的记录。 第三条规则说,要通过所有具有印度(国家),邦(旁遮普邦)和地区(桅杆)地区的记录。
第二条规则是第一条规则的例外。第三条规则是第二条规则的例外。
考虑数据表中的数据和规则表中的规则,对于印度(国家/地区)记录,传递列如下。
Name Company Continent country state district Pass
Tom HP Asia India Assam Kdk Yes
Charles WM Asia India Haryana Jhat Yes
Chia WM Asia India Punjab Mast Yes
Chip WM Asia India Punjab Chista No
这只是一个例子。在生产中,数据将有所不同。
非常感谢您的帮助。
解决方法
您需要最具体的规则。在Hive中,您可以使用多个left join
:
select d.*,coalesce(r1.pass,r2.pass,r3.pass)
from data d left join
rules r1
on r1.Continent = d.Continent and
r1.country = d.country and
r1.state = d.state and
r1.district = d.district left join
rules r2
on r2.Continent = d.Continent and
r2.country = d.country and
r2.state = d.state and
r2.district = 'ALL' left join
rules r3
on r3.Continent = d.Continent and
r3.country = d.country and
r3.state = 'ALL' and
r3.district = 'ALL' ;
如果LEFT JOIN
和'ALL'
允许使用continent
,则可能要继续使用country
。
@TomG:如果有帮助,请参见以下代码
select * from TEMP_TESTING where country ='India' and district<>'Chista'
union
(select * from TEMP_TESTING where country ='India' except
select * from TEMP_TESTING where country ='India' and state='Punjab')
union
select * from TEMP_TESTING where country ='India'and state='Punjab' and district='Mast'