问题描述
我想向表中添加新列,但前提是该列尚不存在.
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
当我尝试使用CREATE TABLE和ADD PARTITION支持的"IF NOT 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.
推荐答案
您可以通过设置hive.cli.errors.ignore
标志来部分解决此问题.在这种情况下,即使在途中的查询失败时,配置单元CLI也会强制执行进一步的查询.
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.
在此示例中:
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.
这篇关于在Hive中,仅当该列不存在时才如何添加该列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!