impala SQL插入报错

impala插入数据的时候,出现报错:

[hs-170:21000] > insert into test_abc(fund_account, client_name, client_sex) values('1234567890123456789001', 'name1', '0');
Query: insert into test_abc(fund_account, client_name, client_sex) values('1234567890123456789001', 'name1', '0')
ERROR: AnalysisException: Possible loss of precision for target table 'wt_tmp.test_abc'.
Expression ''1234567890123456789001'' (type: STRING) would need to be cast to VARCHAR(32) for column 'fund_account'

出现这类错误的时候,你就看报错,它说把某个字段转成什么就把某个字段转成什么。上述报错中提示:

 (type: STRING) would need to be cast to VARCHAR(32) for column 'fund_account'

意思是:这个输入格式为STRING的字段fund_account'字段需转换成格式VARCHAR(32)

来看下表test_abc的结构:

[hs-170:21000] > show create table test_abc;
Query: show create table test_abc
+-------------------------------------------------------------------------------+
| result                                                                        |
+-------------------------------------------------------------------------------+
| CREATE TABLE wt_tmp.test_abc (                                                |
|   fund_account VARCHAR(32) COMMENT 'fund_account',                            |
|   client_name VARCHAR(64) COMMENT 'client_name',                              |
|   client_sex VARCHAR(32) COMMENT 'client_sex'                                 |
| )                                                                             |
| STORED AS TEXTFILE                                                            |
| LOCATION 'hdfs://hs-171:xxx/user/xxx/wt_tmp.db/test_abc' |
| TBLPROPERTIES ('transient_lastDdlTime'='1542874679')                          |
+-------------------------------------------------------------------------------+

接下来执行改造过的插入语句:

insert into test_abc(fund_account, client_name, client_sex) values(cast(001 as VARCHAR(32)), cast('name1' as varchar(64)), cast(0 as varchar(32)));
insert into test_abc(fund_account, client_name, client_sex) values(cast(002 as VARCHAR(32)), cast('name2' as varchar(64)), cast(0 as varchar(32)));
insert into test_abc(fund_account, client_name, client_sex) values(cast(003 as VARCHAR(32)), cast('name3' as varchar(64)), cast(1 as varchar(32)));
insert into test_abc(fund_account, client_name, client_sex) values(cast(004 as VARCHAR(32)), cast('name4' as varchar(64)), cast(1 as varchar(32)));
insert into test_abc(fund_account, client_name, client_sex) values(cast(005 as VARCHAR(32)), cast('name5' as varchar(64)), cast(0 as varchar(32)));
insert into test_abc(fund_account, client_name, client_sex) values(cast(006 as VARCHAR(32)), cast('name6' as varchar(64)), cast(2 as varchar(32)));

查看数据:

[hs-170:21000] > select * from test_abc;
Query: select * from test_abc
+--------------+-------------+------------+
| fund_account | client_name | client_sex |
+--------------+-------------+------------+
| 2            | name2       | 0          |
| 1            | name1       | 0          |
| 6            | name6       | 2          |
| 5            | name5       | 0          |
| 4            | name4       | 1          |
| 3            | name3       | 1          |
+--------------+-------------+------------+
Fetched 6 row(s) in 1.29s

插入成功。
上述SQL为单条插入,也可以一次插入多条(与mysql插入方式一致):

insert into test_abc(fund_account, client_name, client_sex) values(cast(007 as VARCHAR(32)), cast('name7' as varchar(64)), cast(2 as varchar(32))),(cast(008 as VARCHAR(32)), cast('name8' as varchar(64)), cast(0 as varchar(32)));

你可能感兴趣的:(数据库)