1、语法
load data (local) inpath '文件路径' into|overwrite table students;
local:表示从本地加载数据,不加local
则是从HDFS中加载数据
overwrite:覆盖表中的原有数据
2、向stutest表加载数据
源数据
1 zhangsan yw 83
2 lisi sx 60
3 wangwu yw 84
4 zhaoliu sx 95
1 zhangsan sx 96
2 lisi yw 88
3 wangwu sx 80
4 zhaoliu yw 87
建一个内部表
hive> create table stutest(
> stid string,
> stname string,
> stpro string,
> scores int
> )
> row format delimited fields terminated by ' ';
(1)加载本地文件到表中
导入后会在stutest的目录下多个文件
load data local inpath '/opt/soft/data/i.txt' into table stutest;
## 参数介绍
(1)load data:表示加载数据
(2)local:表示从本地加载数据到 hive 表(复制);否则从 HDFS 加载数据到 hive 表(移动)
(3)inpath:表示加载数据的路径
(4)overwrite into:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表
(7)partition:表示上传到指定分区
hdfs dfs -put /opt/soft/data/i.txt /student
加载数据
load data inpath '/student/i.txt' into table student;
(3)覆盖原有数据
load data inpath '/student/i.txt' overwrite into table student;
1、语法 与mysql相似
insert into [table_name] values(field1,field2,...);
示例
创建一个外部表
hive> create external table student_ex(
> stid string,
> stname string,
> stpro string,
> score int
> )
> row format delimited fields terminated by ' ';
插入数据
hive> insert into student_ex values('1','test','yw',88);
通过查询一张表向另一张表插入数据
hive> insert overwrite table student_ex
> select stid,stname,stpro,scores from stutest;
导入数据
import table student from '文件路径';
创建表的时候指定数据路径,加载数据
create table if not exists stutest(
id int,name string,scores int
)
row format delimited fieds terminated by ','
location '文件路径'
导出数据export
export table mydemo.stutest to '文件路径'
查询结果导出到本地insert
insert overwrite local directory '/opt/data'
select * from student;
清空表数据
truncate 只能清空元数据表,不会删除外部表中的数据
truncate table student
清空元数据表,
drop table [table_name]
1.建立一个外表
hive> create external table origninfos(
> id string,
> name string,
> sex string,
> age int
> )
> row format delimited fields terminated by ' '
> location '/orign';
查询除表数据
hive> select * from origninfos;
OK
1 zhangsan male 30
2 lisi female 20
3 wangwu male 26
4 zhaoliu female 44
5 qiqi unknown 42
2.建立分区表
hive> create external table partinfos(
> id string,
> name string,
> age int
> )partitioned by (sex string)
> row format delimited fields terminated by ' ';
3.实现分区操作
hive> set hive.exec.dynamic.partition=true;
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> insert into partinfos partition(sex) select id,name,age,sex from origninfos;
源数据
1 h01 zhangsan male 30
2 h03 lisi female 20
3 h01 wangwu male 26
4 h03 zhaoliu female 25
5 h03 qiqi unknown 25
6 h01 huahua female 24
7 h01 didi unknown 27
8 h03 haha male 26
创建元数据表(外表) 用来对比
create external table allinfos(
id int,
cla string,
name string,
sex string,
age int
)row format delimited fields terminated by ' ';
# 加载数据
load data local inpath '/opt/soft/data/td.txt' into table allinfos;
1、创建一个分区表
create external table partinfos(
id int,
cla string,
name string,
age int
)
partitioned by(sex string)
row format delimited fields terminated by ' '
向分区表插入数据
load data local inpath '/opt/soft/data/part.txt' into table partinfos partition(sex='male');
创建一个内部表
设置动态分区
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict
插入数据
insert into table part_ partition(sex) select id,name,age,sex from origninfos;