在Hive中,仅当该列不存在时才如何添加该列?

编程入门 行业动态 更新时间:2024-10-11 03:23:23
本文介绍了在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

当我尝试使用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中,仅当该列不存在时才如何添加该列?

本文发布于:2023-07-21 03:38:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   时才   Hive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!