简体   繁体   English

在Hive中更改列数据类型

[英]Alter column data type in Hive

we need to alter the table column data type from string to date. 我们需要将表列数据类型从字符串更改为日期。 While am trying to do am getting the below error. 在尝试执行操作时遇到以下错误。 Could you please help. 能否请你帮忙。

hive> describe sales_staging; 蜂巢>描述sales_staging;

OK

cust_id string prod_num string cust_id字符串prod_num字符串
qty int sale_date string 数量int sale_date字符串

sale_id string Time taken: 0.151 seconds, Fetched: 5 row(s) sale_id字符串耗时:0.151秒,已访存:5行

hive> alter table sales_staging CHANGE sale_date sale_date DATE ; 蜂巢>更改表sales_staging更改sale_date sale_date DATE;

FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. 失败:执行错误,从org.apache.hadoop.hive.ql.exec.DDLTask返回代码1。 Unable to alter table. 无法更改表格。 The following columns have types incompatible with the existing columns in their respective positions :sale_date 以下各列的类型与各自位置上的现有列不兼容:sale_date

hive> 蜂巢>

You can't give same name to column you wish to change datatype of. 您不能为要更改其数据类型的列提供相同的名称。 use like this 这样使用

ALTER TABLE sales_staging CHANGE sale_date sale_date_new DATE;

refer this Apache Hive Wiki 请参阅此Apache Hive Wiki

you can't change the existing string type data to date type. 您不能将现有的字符串类型数据更改为日期类型。 but we can able to solve this issues in 2 ways. 但是我们可以通过两种方式解决此问题。

  1. create another table with the same columns count but the data type is date where the column you need string to date, then use insert command to export old table data to new table by casting the string to date. 创建另一个具有相同列数但数据类型为date的表,其中该列需要使用字符串更新日期,然后使用insert命令通过将字符串转换为date来将旧表数据导出到新表。
  2. add a new column to existing table with datatype as date. 将一个新列添加到数据类型为日期的现有表中。 overwrite the table itself by casting the string to date into the new column. 通过将日期字符串转换为新列来覆盖表本身。

ex: 例如:

I have orders table 我有订单表

describe orders; 描述订单;

order_id int order_id int
order_date string order_date字符串
order_customer_id int order_customer_id int
order_status string order_status字符串

created another table ordersnew 创建了另一个表ordersnew

describe ordersnew; 描述订单新内容;

id int id int
odate date 约会
cid int cid int
ostatus string stat弦

now exported the orders data to ordersnew table 现在将订单数据导出到ordersnew表

insert into ordersnew select order_id,cast(from_unixtime(unix_timestamp(substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss')) as timestamp) as strdate, order_customer_id,order_status from orders; 插入订单中新选择order_id,cast(from_unixtime(unix_timestamp(substring(order_date,1,19),'yyyy-MM-dd HH:mm:ss'))作为时间戳)作为strdate,order_customer_id,order_status从订单中;

substring(order_date,1,19), 'yyyy-MM-dd HH:mm:ss' this is the place you have to check and alter your query as per your data. substring(order_date,1,19),'yyyy-MM-dd HH:mm:ss'这是您必须根据数据检查和更改查询的地方。

please check here for date conversions 在这里查看日期转换

Do the following steps: step-1) check all the dates in field "sale_date" are valid dates or not. 请执行以下步骤:步骤1)检查“ sale_date”字段中的所有日期是否为有效日期。 If yes then go to step-2 如果是,则转到步骤2

step-2) Check the date format, for DATE datatype format should be 'yyyy-MM-dd'. 步骤2)检查日期格式,因为DATE数据类型格式应为'yyyy-MM-dd'。 If the date format is 'yyyy-MM-dd HH:mm:ss' then you should try the following syntax: 如果日期格式为“ yyyy-MM-dd HH:mm:ss”,则应尝试以下语法:

alter table sales_staging CHANGE sale_date sale_date TIMESTAMP ; 更改表sales_staging更改sale_date sale_date TIMESTAMP ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM