SemanticException添加分区Hive表

问题描述:

尝试使用以下内容在Hive表上创建分区:

Attempting to create a partition on a Hive table with the following:

> alter table stock_ticker add if not exists
> partition(stock_symbol='ASP')
> location 'data/stock_ticker_sample/stock_symbol=ASP/'

哪个会产生以下输出

FAILED : SemanticException table is not partitioned but partition spec exists: {stock_symbol=ASP} 

在尝试添加之前,该表上没有分区

There are no partitions on this table prior to this addition attempt

> show partitions stock_ticker;

这将导致

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. 
Table stock_ticker_sample is not a partitioned table

毫无疑问,stock_symbol列存在且为字符串类型.

There is no question that the stock_symbol column exists and is of type string.

查询是要添加此分区需要采取哪些步骤?

The query is what steps need to be taken in order to add this partition?

解决方案是将分区信息添加到stock_ticker表的定义中:

Solution would be to add partitioning info into the definition of stock_ticker table:

CREATE EXTERNAL TABLE stock_ticker (
... 
)
PARTITIONED BY (stock_symbol STRING);

然后,您可以轻松地通过以下方式将外部数据添加到表中:

Then easily you can add external data to your table by:

> alter table stock_ticker add if not exists
> partition(stock_symbol='ASP')
> location 'data/stock_ticker_sample/stock_symbol=ASP/'

GL!