Vijaya Seetharaman
Vijaya Seetharaman

Reputation: 181

How to use Hive Query results(multiple) in a variable for other query

I have two tables one is schools and one is students.I want to find all the students of a particular school. The schema of schools is: id, name, location and of students is :id, name, schoolId. I wrote the following script:

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}%'

I dont get any result as schoolId stores all the id together.For example there are 3 schools with id: 123, 256,346 schoolId variable stores as 123 256 346 and the result is null.

Upvotes: 2

Views: 684

Answers (1)

leftjoin
leftjoin

Reputation: 38325

Use collect_set() with concat_ws to get comma delimited string, IDs should be cast to string:

schoolId=$(hive -e "set hive.cli.print.header=false;select concat_ws('\\',\\'',collect_set(cast(id as string))) from school;");

hive -hiveconf "schoolId"="$schoolId" 

Then use IN operator:

select id,name from student where schoolId in ('${hiveconf:schoolId}');

Upvotes: 1

Related Questions