hive 如果表不存在则创建,在Hive中,如何仅在该列不存在时添加该列?

I would like to add a new column to a table, but only if that column does not already exist.

This works if the column does not exist:

ALTER TABLE MyTable ADD COLUMNS (mycolumn string);

But when I execute it a second time, I get an error.

Column 'mycolumn' exists

When I try to use the "IF NOT EXISTS" syntax that is supported for CREATE TABLE and ADD PARTITION, I get a syntax error:

ALTER TABLE MyTable ADD IF NOT EXISTS COLUMNS (mycolumn string);

FAILED: ParseException line 3:42 required (...)+ loop did not match anything at input 'COLUMNS' in add partition statement

What I need is something that can execute itempotently so I can run my query whether this column exists or not.

解决方案

You can partially work it around, by setting the hive.cli.errors.ignore flag. In this case hive CLI will force the execution of further queries even when queries on the way fail.

In this example:

SET hive.cli.errors.ignore=true;

ALTER TABLE MyTable ADD COLUMNS (mycolumn string);

ALTER TABLE MyTable ADD COLUMNS (mycolumn string);

ALTER TABLE MyTable ADD COLUMNS (mycolumn2 string);

hive will execute all queries, even though there'll be an error in the second query.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值