数据导入
-
向表中装载数据(load):
语法:
load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student partition(month=2019);
①load data:表示加载数据;
②local:表示从本地加载数据,否则从hdfs上加载数据到hive表;
③inpath:表示被加载数据的路径;
④into:向表中追加数据,加上overwrite表示先删除表中数据再加载新数据;
⑤table:表示加载到哪张表;
⑥student:表示具体的表;
⑦partition:表示上传到指定分区; -
通过查询语句向表中插入数据(insert):
示例:insert into table student partition(month='201909') values(1,'Curry');
-
查询语句中创建表并加载数据(as select):
根据查询结果创建表(查询的结果会添加到新创建的表中):
create table if not exists student as select id,name from student2;
-
创建表时通过Location指定加载数据路径:
- 创建表并指出表在hdfs的位置:
create table if not exists stu(name string,age int) row format delimited fields terminated by '\t' location '/user/hive/warehouse/student5'
- 上传数据到hdfs上:
dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5
- 创建表并指出表在hdfs的位置:
-
Import数据到指定Hive表中:
注意:先用export导出后,再将数据导入
import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';
数据导出
- Insert导出
- 将查询的结果导出到本地
insert overwrite local directory '/opt/module/hive/data/stu.txt' select * from stu;
- 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/hive/data/stu1.txt' row format delimited fields terminated by '\t' select * from stu;
- 将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/user/zy/stu,txt' row format delimited fields terminated by '\t' select * from stu;
- 将查询的结果导出到本地
- Hadoop命令导出到本地
dfs -get '/user/hive/warehouse/student/month=201709/000000_0' /opt/module/datas/export/student3.txt;
- Hive Shell 命令导出
基本语法:hive -f/-e 执行语句或脚本 > file_name
示例:bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student.txt;
- Export导出到HDFS上
export table default.stu to '/user/hive/warehouse/export/student';
清除表中数据
truncate只能删除管理表,不能删除外部表中的数据
truncate table student;