sql - Hive是否支持Select中的Select?

标签 sql hadoop hql hive

Hive 中的以下代码是否可能,并进行一些更改?:

insert into table webmap
select a.res reference, b.res resource, 
(select count(ip) from weblog where resource=a.res and referer=b.res) weight
from toprefres a join toprefres b;

我在 hive-0.10.0-cdh4.5.0 中运行它并得到错误:

FAILED: ParseException line 3:1 cannot recognize input near 'select' 'count' '(' in expression specification

如果支持select in select in Hive呢!

我终于找到答案了:

insert into table webmap
select refres.reso,refres.refe, count(ip) weight from 
weblog join (select a.res refe, b.res reso from toprefres a join toprefres b) refres on
trimrslash(weblog.resource)=refres.reso and trimrslash(weblog.referrer)=concat("http://dongxicheng.org",refres.refe)
group by refres.reso,refres.refe;

这非常适合我的需要!

最佳答案

据我所知,Hive 不支持此类相关子查询。我从以下帖子中证实了我的想法:

Hive column as a subquery select

无论我怎么想,您都可以修改查询以实现您想要获得的结果:

insert into table webmap
select a.res reference, b.res resource, 
count(weblog.ip) weight
from toprefres a 
join toprefres b
join weblog ON weblog.resource=a.res 
and weblog.referer=b.res;

另请注意,您的 ON 子句中没有这可能会导致交叉联接创建笛卡尔积并花费更多时间。如果可能,请尝试对其进行优化。

关于sql - Hive是否支持Select中的Select?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059559/

相关文章:

python - 将 JAR 文件添加到 Python 脚本

mysql - 根据与用户关联的项目获取数据列表

sql - 对字符串的一部分进行分组时避免子查询

sql - Hive 和选择不匹配的记录

hibernate - GORM Hibernate查询

Hibernate aliasToBean不会将字符串转换为Enum

java - Hibernate HQL 不断抛出 'QuerySyntaxException: unexpected token'

mysql - 嵌套 SQL 语句未按预期计算 SUM

mysql - 使用 if 条件过滤 SQL 查询

hadoop - 我可以像在 MySQL 中那样从文件创建 Hive 表吗