我有两张桌子,一张是学校,另一张是学生。我想找到某所学校的所有学生。学校的模式是:id,name,location和学生的是:id,name,schoolId。我写了以下脚本:
schoolId=$(hive -e "set hive.cli.print.header=false;select id from school;")
hive -hiveconf "schoolId"="$schoolId"
hive>select id,name from student where schoolId like '${hiveconf:schoolId}%'
我没有得到任何结果,因为schoolId将所有id存储在一起。例如,有3所学校的id为:123256346schoolId变量存储为123 256 346,结果为null。
使用collect_set()
和concat_ws
来获得逗号分隔的字符串,ID应转换为字符串:
schoolId=$(hive -e "set hive.cli.print.header=false;select concat_ws('\',\'',collect_set(cast(id as string))) from school;");
hive -hiveconf "schoolId"="$schoolId"
然后使用IN运算符:
select id,name from student where schoolId in ('${hiveconf:schoolId}');