您的位置:首页 > 其它

HIVE入门_3_数据导入导出

2015-10-23 09:34 295 查看
数据导出方式
导出到本地文件系统

导出到HDFS上

导出到HIVE的另一个表中

数据导入方式
从本地文件导入

从HDFS上导入

创建表后从别的表查询出的相应数据导入

创建表的时候通过别的表查询记录插入

参考资料

数据导出方式

导出到本地文件系统

hive> insert overwrite local directory '/home/wyp/wyp'
> row format delimited
> fields terminated by '\t'
> select * from wyp;

hive -e "select * from wyp" >> local/wyp.txt

cat wyp.sql
#select * from wyp
hive -f wyp.sql >> local/wyp2.txt


导出到HDFS上

hive> insert overwrite directory '/home/wyp/hdfs'
> select * from wyp;


导出到HIVE的另一个表中

hive> insert into table test
> partition (age='25')
> select id, name, tel
> from wyp;


数据导入方式

从本地文件导入

hive> create table wyp
> (id int, name string,
> age int, tel string)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;

cat wyp.txt
#1       wyp     25      13188888888888
#2       test    30      13888888888888

load data local inpath 'wyp.txt' into table wyp;

dfs -ls /user/hive/warehouse/wyp ;


从HDFS上导入

从本地文件系统将数据导入到HIVE表的过程中,其实是现将数据临时复制到HDFS下面的一个目录,然后再将数据从临时目录下移动到对应HIVE表的数据目录中。

因此,HIVE也支持将数据直接从HDFS上的一个目录移动到相应HIVE表的目录中去。

和本地文件系统导入的区别只是是否有inpath

load data inpath '/home/wyp/add.txt' into table wyp;


创建表后从别的表查询出的相应数据导入

hive> create table test(
> id int, name string
> ,tel string)
> partitioned by
> (age int)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;

hive> insert into table test > partition (age='25') > select id, name, tel > from wyp;

##动态指明分区
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> insert into table test
> partition (age)
> select id, name,
> tel, age
> from wyp;

#overwrite方式重写原来的数据
hive> insert overwrite table test
> PARTITION (age)
> select id, name, tel, age
> from wyp;

#多表插入,只需要扫描一遍数据生成需要的各种表
hive> from wyp
> insert into table test
> partition(age)
> select id, name, tel, age
> insert into table test3
> select id, name
> where age>25;


创建表的时候通过别的表查询记录插入

hive> create table test4
> as
> select id, name, tel
> from wyp;


参考资料

过往记忆的blog
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hive