一条一条数据的插入显然是不现实的,现在尝试将txt文件直接导入到hive表student中
需求
将本地/opt/module/data/student.txt 这个目录下的数据导入到 hive 的 student(id int, name
string)表中。
1.数据准备
在/opt/module/data 这个目录下准备数据
(1)在/opt/module/目录下创建 data
root@iZptv27z435ygwZ:/opt/module# mkdir data
(2)在/opt/module/datas/目录下创建 student.txt 文件并添加数据
root@iZptv27z435ygwZ:/opt/module/data# touch student.txt
root@iZptv27z435ygwZ:/opt/module/data# vi student.txt
注意以 tab 键间隔。
2.Hive 实际操作
# 启动 hive
$ bin/hive
# 显示数据库
hive> show databases;
# 使用 default 数据库
hive> use default;
# 显示 default 数据库中的表
hive> show tables;
# 删除已创建的 student 表
hive> drop table student;
# 创建 student 表, 并声明文件分隔符’t’
hive> create table student(id int, name string) ROW FORMAT
DELIMITED FIELDS TERMINATED BY 't';
# 加载/opt/module/data/student.txt 文件到 student 数据库表中。
hive> load data local inpath '/opt/module/data/student.txt' into
table student;
# Hive 查询结果
hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
NULL NULL
Time