简体   繁体   中英

creating a hiveQL query that uses UDF function that can return column names

I want to create a Hive UDF function that returns specific column names based on some value say retreivecol(age).If the age is 20 then return the list of column names to be used in select query like 'name,email,fbuserid,friend list ' etc and if the age is less than 20 return 'name' alone.So I want my HIVE QL query to look like

select retreivecol(age) from User_Data;

The above query just prints the name of the columns like 'name,email,fbuserid,friendslist' etc as opposed to treating them as column names and filtering based on the same.Any pointers are appreciated.

I'm not sure a UDF is the right place to do this, as UDF's simply see the value passed to them, they don't really have access to the whole table structure.

Instead, could you do this in a nested table?

select name,email,id FROM
(
  select
    name,
    if(age < 20, email, NULL) as email,
    if(age < 20, id, NULL) as id
  FROM mytable

) a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM