Hive import csv file example

Table of contents
  • text
    • First create the table
    • Import data and query
    • other considerations
    • Summarize

text

The existing file is in csv format and needs to be imported into hive. Set the content of csv as follows

?

1

2

1001,zs,23

1002,lis,24

First create the table

?

1

2

3

4

5

6

7

create table if not exists csv2(

    uid int,

    uname string,

    age int

)

row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'

stored as textfile ;

Import data and query

?

1

2

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

select * from csv2;

other considerations

If the created table is in parquet format, can it load and import csv file?

?

1

2

3

4

5

6

7

8

9

10

11

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;

An error will be reported when using

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]

**No, you need to import it into textfile first, and then import it into parquet from the temporary table,** as follows

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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;

Summarize

  • The key is to introduce org.apache.hadoop.hive.serde2.OpenCSVSerde
  • csvTo save to hive, parquetyou need to save astextfile

Guess you like

Origin blog.csdn.net/qq_15509251/article/details/131589755