我有一个CSv文件:
Name,Age,City,Country
SACHIN,44,PUNE,INDIA
TENDULKAR,45,MUMBAI,INDIA
SOURAV,45,NEW YORK,USA
GANGULY,45,CHICAGO,USA
我创建了一个HIVE表并将数据加载到其中。
我发现上面的文件错误,修正后的文件如下:
Name,Age,City,Country
SACHIN,44,PUNE,INDIA
TENDULKAR,45,MUMBAI,INDIA
SOURAV,45,NEW JERSEY,USA
GANGULY,45,CHICAGO,USA
我需要用正确的文件更新我的主表。
我尝试过以下方法。
1-在City上创建主表作为分区表,并动态加载第一个文件。
步骤1 - 创建临时表并按原样加载old.csv文件而不进行分区。我这样做的步骤是通过不为每个分区创建单独的输入文件来动态地在主表dyn中插入数据。
create table temp(
name string,
age int,
city string,
country string)
row format delimited
fields terminated by ','
stored as textfile;
Step2-将旧文件加载到临时表中。
load data local inpath '/home/test_data/old.csv' into table temp;
第3步 - 创建主分区表。
create table dyn(
name string,
age int)
partitioned by(city string,country string)
row format delimited
fields terminated by ','
stored as textfile;
步骤4-将old.csv文件动态插入临时表的分区表中。
insert into table dyn
partition(city,country)
select name,age,city,country from temp;
旧记录动态插入主表。在接下来的步骤中,我尝试使用old.csv将主表dyn
更正为new.csv
步骤5-使用新的正确输入文件创建另一个临时表。
create table temp1(
name string,
age int,
city string,
country string)
row format delimited
fields terminated by ','
stored as textfile;
步骤6-将新的正确输入文件加载到第二个临时表中,然后用于覆盖主表,但只覆盖old.csv中数据错误的行。这适用于SOURAV,45,NEW YORK,USA
到SOURAV,45,NEW JERSEY,USA
。
load data local inpath '/home/test_data/new.csv' into table temp1;
覆盖主表,但只覆盖old.csv中数据错误的行。这适用于SOURAV,45,NEW YORK,USA
到SOURAV,45,NEW JERSEY,USA
。
最后覆盖Step7尝试1-
insert overwrite table dyn partition(country='USA' , city='NEW YORK') select city,country from temp1 t where t.city='NEW JERSEY' and t.country='USA';
结果: - 在名称列中插入了NUll。
NEW JERSEY NULL NEW YORK USA
最后覆盖Step7尝试2-
insert overwrite table dyn partition(country='USA' , city='NEW YORK') select name,age from temp1 t where t.city='NEW JERSEY' and t.country='USA';
结果: - No change in dyn table. Same as before. NEW YORk did not update to NEW JERSEY
最后覆盖Step7的尝试3 -
insert overwrite table dyn partition(country='USA' , city='NEW YORK') select * from temp1 t where t.city='NEW JERSEY' and t.country='USA';
错误: - FAILED: SemanticException [Error 10044]: Line 1:23 Cannot Insert into target table because column number/types are different. Table insclause-0 has 2 columns,but query has 4 columns
解决此问题的正确方法是什么。