1.导入
load data [local] inpath '' [overwrite] into database.table [partition(partcol=val)]
原始文件在linux本地 加上local 如果原始数据文件在hdfs 不用local
如果是覆盖数据加上overwrite 如果是追加 不要overwrite
如果是分区表加上partition,不是就不用了。
2.创建表通过insert 加载
create table db_hive.dept_copy like dept;
insert into table db_hive.dept_copy
select * from dept ;
3.创建表时通过location指定文件路径
2.导出
1.通过insert [overwrite] [local] directory
导出数据到本地文件 会自动创建目录啦 注意这里的目录是一个文件夹
insert overwrite local directory '/opt/datas/hive_exp_dept'
select * from db_hive.dept;
查看导出的数据
发现数据不太好看
格式化一下
insert overwrite local directory '/opt/datas/hive_exp_dept'
row format delimited fields terminated by '\t'
collection items terminated by '\n'
select * from db_hive.dept;
再次查看
导出数据到hdfs 当然就是不要local啦
insert overwrite directory '/user/hive/warehouse/db_hive.db/dept_exp_hdfs'
select * from db_hive.dept;
50070页面查看
查看
将文件拿到本地
bin/hdfs dfs -get /user/hive/warehouse/db_hive.db/dept_exp_hdfs/0* /root/hive_datas/
2.重定向
将查询结果重定向到一个文件 这里是一个文件 上面有数据而不是文件夹
bin/hive -e "select * from db_hive.emp;" > /opt/datas/emp_exp_dept ;
3.hive的import和export
官方文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ImportExport
import 导入数据到hive表中
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
FROM 'source_path'
[LOCATION 'import_target_path']
export (注意这个target_path是hdfs的路径)
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
TO 'export_target_path' [ FOR replication('eventid') ]