impala SQL插入報錯
阿新 • • 發佈:2018-11-30
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)));