1. DML

2. 数据导入

  1. load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
  1. load data:表示加载数据
  2. local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
  3. inpath:表示加载数据的路径
  4. overwrite:表示覆盖表中已有数据,否则表示追加
  5. into table:表示加载到哪张表
  6. student:表示具体的表
  7. partition:表示上传到指定分区
load data local inpath "/opt/module/datas/student.txt" into table stu_par; -- 从宿机加载数据
load data local inpath "/opt/module/datas/student.txt" overwrite into table stu_par; -- 覆盖导入
load data inpath "/opt/module/datas/student.txt" overwrite into table stu_par; -- 从hdfs中加载指定路径文件  HDFS的导入是移动文件,而本地导入是复制上传
  • insert导入
insert into table  stu_par partition(month='201709') values(1,'wangwu') -- 插入单条 一个括号对应一条
insert into table  stu_par partition(month='201709') values(1,'wangwu'),(2,'zhaoliu'); -- 插入多条指定数据 多行数据用逗号隔开并用括号包裹 要类型一致
insert into table  stu_par select id,name from stu_par2 where class="01";  -- 插入查询后的数据
  • 建表时as select导入
create table if not exists student3
as select id, name from student;
  • 建表时通过location加载
create external table if not exists student4
(id int, name string)
row format delimited fields terminated by '\t'
location '/student';

3. 数据导出

3.1. Insert导出

  • 将查询的结果导出到本地
    默认不带格式
insert overwrite local directory '/opt/module/datas/export/student' select * from student;
  • 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;

3.2. Hive Shell 命令导出

用shell 命令查询表并写出本地

如果不指定库 默认为default库

hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

3.3. Export导出到HDFS上

将表导出到HDFS上 元数据和表数据文件

export table default.student to '/user/hive/warehouse/export/student';

3.4. Import数据导入到hive表中

要先用export导出到hdfs中 必须包含元数据和表数据

import table student from '/export/student';

4. 数据删除

清空表 只删除表数据 不删除表本身 Truncate只能删除管理表,不能删除外部表中数据

truncate table student;