【问题标题】:Creating and updating a new column in hive在 hive 中创建和更新新列
【发布时间】:2017-08-01 12:54:24
【问题描述】:

我是 SQL 和 Hive 的新手。我在蜂巢中有一张桌子,我需要在其中添加 2 列。一个是“row_id”,另一个是“cto_id”。我已经使用 hive 函数和一个名为“cto_id”的新列添加了行 ID。

我想更新“cto_id”列中的值,例如它包含“CTO1101”+row_id 之类的值

我该怎么做?下面是我的代码。

-- assigning row number to each record in mu_temp_trials table
select *, row_number() over() as row_id from mu_temp_trials;

--adding new column for primary key in mu_temp_trials
alter table mu_temp_trials add columns(cto_id string);
//update mu_temp_trials set cto_id = "CTO_1101"+row_id; - I want to write this code in hive

【问题讨论】:

  • 你可以设置一个触发器来设置cto_id
  • @Adam 我需要更新列 cto_id 并且需要它以供进一步使用。可以使用什么配置单元代码来实现这一点?

标签: sql hadoop hive


【解决方案1】:

希望这会有所帮助!

#let's say table mu_temp_trials has two columns - col1 & col2    
ALTER TABLE mu_temp_trials ADD COLUMNS(cto_id STRING, row_id STRING);


INSERT OVERWRITE TABLE mu_temp_trials
SELECT  a.col1, a.col2, concat_ws('','CTO1101',cast(a.row_id as string)) AS cto_id, cast(a.row_id as string) AS row_id
FROM (SELECT  col1, col2, row_number() over() AS row_id FROM mu_temp_trials) a;


如果它解决了您的问题,请不要忘记告诉我们 :)

【讨论】:

  • @Vaibhav 如果您喜欢该解决方案,那么您应该接受它作为正确答案。如果他们将来遇到类似的问题,它肯定会帮助其他人。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多