Hive導入csv文件示例

正文

現有文件為csv格式,需要導入hive中,設csv內容如下

1001,zs,23
1002,lis,24

首先創建表

create table if not exists csv2(
    uid int,
    uname string,
    age int
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
stored as textfile ;

導入數據及查詢

load data local inpath '/data/csv2.csv' into table csv2;
select * from csv2;

其他註意事項

如果建表是parquet格式可否load導入csv文件?

drop table csv2;
create table if not exists csv2(
    uid int,
    uname string,
    age int
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
stored as parquet ;

load data local inpath '/data/csv2.csv' into table csv2;
select * from csv2;

使用時會報錯

Failed with exception java.io.IOException:java.lang.RuntimeException: hdfs://192.168.10.101:8020/user/hive/warehouse/csv2/csv2.csv is not a Parquet file. expected magic number at tail [80, 65, 82, 49] but found [44, 50, 52, 10]

**不可以,需要先導入成textfile,之後再從臨時表導入成parquet,**如下

drop table csv2;
create table if not exists csv2
(
    uid   int,
    uname string,
    age   int
)
    row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
    stored as textfile;
-- 先導入csv文件到表格csv2,保存格式是textfile
load data local inpath '/data/csv2.csv' into table csv2;


drop table csv3;
-- 創建csv3,保存格式parquet
create table if not exists csv3
(
    uid   int,
    uname string,
    age   int
)
    row format delimited
        fields terminated by ','
    stored as parquet;
-- 提取csv2的數據插入到csv3
insert overwrite table csv3 select * from csv2;

總結

  • 關鍵是要引入org.apache.hadoop.hive.serde2.OpenCSVSerde
  • csv要保存到hiveparquet,需要先保存成textfile

以上就是Hive導入csv文件示例的詳細內容,更多關於Hive導入csv文件的資料請關註WalkonNet其它相關文章!

推薦閱讀: