sql - Hive Insert 从一个表覆盖到另一个具有不同列数的表

标签 sql hadoop hive hiveql

我有 2 个配置单元表。

源表有以下列:

  correspondence_id       | decimal(22,0)  |          |
| template_id             | decimal(18,0)  |          |
| language_cd             | varchar(6)     |          |
| delivery_channel_cd     | varchar(20)    |          |
| job_id                  | decimal(18,0)  |          |
| correspondence_content  | string         |          |
| create_user_id          | varchar(40)    |          |
| create_ts               | timestamp      |          |
| last_updt_user_id       | varchar(40)    |          |
| last_updt_ts            | timestamp      |          |
| data_src_id             | decimal(18,0)  |          |
| src_app_resource_cd     | varchar(50)

目的地有以下列:

   correspondence_id        | decimal(22,0)         |                       |
| template_id              | decimal(18,0)         |                       |
| template_cd              | varchar(20)           |                       |
| template_type_cd         | varchar(40)           |                       |
| category_cd              | varchar(20)           |                       |
| language_cd              | varchar(6)            |                       |
| delivery_channel_cd      | varchar(20)           |                       |
| job_id                   | decimal(18,0)         |                       |
| correspondence_content   | string                |                       |
| create_user_id           | varchar(40)           |                       |
| create_ts                | timestamp             |                       |
| last_updt_user_id        | varchar(40)           |                       |
| last_updt_ts             | timestamp             |                       |
| data_src_id              | decimal(18,0)         |                       |
| src_app_resource_cd      | varchar(50)           |                       |
| part_create_year_num     | int                   |                       |
| part_create_month_num    | int                   |                       |
|                          | NULL                  | NULL                  |
| # Partition Information  | NULL                  | NULL                  |
| # col_name               | data_type             | comment               |
|                          | NULL                  | NULL                  |
| part_create_year_num     | int                   |                       |
| part_create_month_num    | int       

我使用以下查询来传输数据:

FROM source_table cc insert overwrite table 
destination_table partition 
(part_create_year_num=2016, part_create_month_num=9 )
select cc.correspondence_id, cc.template_id, cc.language_cd, cc.delivery_channel_cd, cc.job_id, 
cc.correspondence_content, cc.create_user_id, cc.create_ts, cc.last_updt_user_id, cc.last_updt_ts, 
cc.data_src_id, cc.src_app_resource_cd

但是当我运行这个查询时,我得到以下错误

rror: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:79 Cannot insert into target table because column number/types are different '9': Table insclause-0 has 15 columns, but query has 12 columns. (state=42000,code=10044)
org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:79 Cannot insert into target table because column number/types are different '9': Table insclause-0 has 15 columns, but query has 12 columns.

显然源表和目标表是不同的,但是我怎样才能使这个查询工作,我试过放入占位符值,但也没有用。

最佳答案

目标表似乎有五个额外的列

普通列: 1 个模板_cd |变种(20) | 2 模板类型CD |变种(40) | | 3 类别_cd |变种(20) | |

分区列 4. part_create_year_num |整数 | | 5. part_create_month_num |整数

查询应该是

Insert overwrite table Destination_table partition(part_create_year_num=2016, part_create_month_num=9 )  select
 correspondence_id,        
 template_id,      
 '' as template_cd,      
'' as  template_type_cd, 
 '' as category_cd,      
 language_cd,      
 delivery_channel_cd,
 job_id,
 correspondence_content,
 create_user_id,
 create_ts,
 last_updt_user_id,
 last_updt_ts,
 data_src_id,
 src_app_resource_cd
 from source_table 

关于sql - Hive Insert 从一个表覆盖到另一个具有不同列数的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703948/

相关文章:

hadoop - Mac机器上的Hadoop设置

hadoop - 如何在我的 Windows 64 位机器上安装 hadoop 和 hive?我是一个初学者,对此有点天真。请赐教

hadoop - hive 查询速度慢,为什么(左半连接)?

hadoop - Sqoop 的语法导入数据库中存在的 100 个表中的 5 个 - 不要使用排除关键字?

hadoop - Hive 中 LEFT OUTER JOIN 的全表扫描问题

php - 如何在 MySQL 查询中编写 PHP 代码

sql - 在一组实验室测试中选择第一个和最后一个测试

mysql - 将多个查询绑定(bind)到一个结果

python - 使用python从数据库中获取数据(在Django框架上)

hadoop - Sqoop如何导入表table1, table2, table3?