Hive - LIKE 运算符
Hive - LIKE Operator
我不知道我是如何处理这个问题的:
这是我的数据:
Table1: Table2:
BRAND PRODUCT SOLD
Sony Sony ABCD 1233
Apple Sony adv 1233
Google Sony aaaa 1233
IBM Apple 123 1233
etc. Apple 345 1233
IBM 13123 1233
是否可以过滤我有 table 品牌和总销量的查询?
我的想法是:
Select table1.brand, sum(table2.sold) from table1
join table2
on (table1.brand LIKE '%table2.product%')
group by table.1.brand
那是我的主意,但我总是得到一个错误
最大的问题是Like-Operator还是有其他解决方案?
我看到两个问题:首先,Hive 中的 JOIN 仅适用于相等条件,like 在那里不起作用。
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins
Only equality joins, outer joins, and left semi joins are supported in Hive. Hive does not support join conditions that are not equality conditions as it is very difficult to express such conditions as a map/reduce job.
相反,它想进入 where 子句。
其次,我还发现 like 语句本身存在问题:“%table2.product%”被解释为字符串“%table2.product%”。此外,即使这是按预期进行的,它也会尝试在品牌内部寻找 table2.product ,而当您似乎想要以另一种方式进行时。为了得到你想要的评价,你需要在table1.brand的内容中加上通配符;为此,您需要将通配符连接到表达式中。
table2.product LIKE concat('%',table1.brand,'%'))
通过这样做,您的点赞将评估字符串“%Sony%”、“%Apple%”...等而不是“%table2.product%”。
您想要的是 Brandon Bell 的查询,我已将其合并到此答案中:
SELECT table1.brand, SUM(table2.sold)
FROM table1, table2
WHERE table2.product LIKE concat('%', table1.brand, '%')
GROUP BY table1.brand;
您应该能够在没有 JOIN 的情况下完成此操作。请参阅以下查询:
SELECT table1.brand, sum(table2.sold)
FROM table1, table2
WHERE table2.product LIKE concat('%', table1.brand, '%')
GROUP BY table1.brand;
这个returns
Apple 2466
IBM 1233
Sony 3699
我的输入文件如下:
Sony
Apple
Google
IBM
和
Sony ABCD 1233
Sony adv 1233
Sony aaaa 1233
Apple 123 1233
Apple 345 1233
IBM 13123 1233
我不知道我是如何处理这个问题的:
这是我的数据:
Table1: Table2:
BRAND PRODUCT SOLD
Sony Sony ABCD 1233
Apple Sony adv 1233
Google Sony aaaa 1233
IBM Apple 123 1233
etc. Apple 345 1233
IBM 13123 1233
是否可以过滤我有 table 品牌和总销量的查询? 我的想法是:
Select table1.brand, sum(table2.sold) from table1
join table2
on (table1.brand LIKE '%table2.product%')
group by table.1.brand
那是我的主意,但我总是得到一个错误
最大的问题是Like-Operator还是有其他解决方案?
我看到两个问题:首先,Hive 中的 JOIN 仅适用于相等条件,like 在那里不起作用。
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins
Only equality joins, outer joins, and left semi joins are supported in Hive. Hive does not support join conditions that are not equality conditions as it is very difficult to express such conditions as a map/reduce job.
相反,它想进入 where 子句。
其次,我还发现 like 语句本身存在问题:“%table2.product%”被解释为字符串“%table2.product%”。此外,即使这是按预期进行的,它也会尝试在品牌内部寻找 table2.product ,而当您似乎想要以另一种方式进行时。为了得到你想要的评价,你需要在table1.brand的内容中加上通配符;为此,您需要将通配符连接到表达式中。
table2.product LIKE concat('%',table1.brand,'%'))
通过这样做,您的点赞将评估字符串“%Sony%”、“%Apple%”...等而不是“%table2.product%”。
您想要的是 Brandon Bell 的查询,我已将其合并到此答案中:
SELECT table1.brand, SUM(table2.sold)
FROM table1, table2
WHERE table2.product LIKE concat('%', table1.brand, '%')
GROUP BY table1.brand;
您应该能够在没有 JOIN 的情况下完成此操作。请参阅以下查询:
SELECT table1.brand, sum(table2.sold)
FROM table1, table2
WHERE table2.product LIKE concat('%', table1.brand, '%')
GROUP BY table1.brand;
这个returns
Apple 2466
IBM 1233
Sony 3699
我的输入文件如下:
Sony
Apple
Google
IBM
和
Sony ABCD 1233
Sony adv 1233
Sony aaaa 1233
Apple 123 1233
Apple 345 1233
IBM 13123 1233