插入 HIVE 表时转换值

Posted

技术标签:

【中文标题】插入 HIVE 表时转换值【英文标题】:Convert value while inserting into HIVE table 【发布时间】:2018-06-12 00:22:20 【问题描述】:

我创建了名为 emp_bucket 的分桶表,分为 4 个桶,这些桶聚集在薪水列上。表结构如下:

hive> describe Consultant_Table_Bucket;
OK
id                      int                                         
age                     int                                         
gender                  string                                      
role                    string                                      
salary                  double                                      
Time taken: 0.069 seconds, Fetched: 5 row(s)

我还有一个临时表,我可以从中将数据插入到上面的分桶表中。以下是暂存表中的示例数据:

id      age     Gender   role         salary
-----------------------------------------------------
938     38      F       consultant      55038.0
939     26      F       student 33319.0
941     20      M       student 97229.0
942     48      F       consultant       78209.0
943     22      M       consultant 77841.0

我的要求是将工资大于 10,000 的员工的数据加载到分桶表中,并且在加载时我必须将 "consultant" 角色 转换为 大数据顾问角色。

我知道如何使用 select 命令将数据插入到我的分桶表中,但需要一些指导,如何将上面角色列中的 consultant 值更改为 BigData advisor 插入时。

任何帮助表示赞赏

【问题讨论】:

用 CASE 语句替换插入期间的列选择。说,如果是顾问,则插入另一个值 如果你展示了你已经写的insert 声明,它会更容易帮助你。 @Guillaume 下面是我的插入语句INSERT TABLE bucketed_user PARTITION (salary) select id, age,gender,role,salary FROM stage_table where salary > 10000 【参考方案1】:

根据您的insert,您只需处理selectrole 部分:

INSERT into TABLE bucketed_user PARTITION (salary)
select
    id
  , age
  , gender
  , if(role='consultant', 'BigData consultant', role) as role
  , salary
FROM
  stage_table
where
  salary > 10000
;

【讨论】:

这是很棒的信息,我正在寻找

以上是关于插入 HIVE 表时转换值的主要内容,如果未能解决你的问题,请参考以下文章

将数据插入 HIVE 表时出错

列值采用 0 或 nul 代替 HIVE 中的 char 数据类型

使用 Spark Scala 将 Sql Server 数据类型转换为 Hive 数据类型

Hive表日期列值转换

Hive:将字符串转换为布尔值

在 Hive 中将 Long 转换为时间戳