hive数据备份与恢复

hive 数据备份

##数据准备
hive> create table demo(
    > id int comment 'ID',
    > name string comment '名字')
    > row format delimited fields terminated by ',';
OK
Time taken: 0.18 seconds
hive> load data local inpath '/data/lilin/demo.txt' into table demo;
Loading data to table lilin.demo
Table lilin.demo stats: [numFiles=1, totalSize=25]
OK
Time taken: 0.785 seconds
hive> select * from demo;
OK
1       aa
2       bb
3       cc
4       dd
5       gg
Time taken: 0.381 seconds, Fetched: 5 row(s)

##将数据备份至本地
hive> insert overwrite local directory '/data/lilin/demo' row format delimited fields terminated by ',' stored as textfile select * from demo;
[root@massive-dataset-new-006 demo]# cat 000000_0 
1,aa
2,bb
3,cc
4,dd
5,gg

##将数据备份至hdfs
hive> insert overwrite directory '/user/lilin/demo' row format delimited fields terminated by ',' stored as textfile select * from demo;

[root@massive-dataset-new-006 demo]# hadoop fs -cat /user/lilin/demo/000000_0
1,aa
2,bb
3,cc
4,dd
5,gg

##新建一张表
hive> create table demo_new(
    > id int comment 'ID',
    > name string comment '名字')
    > row format delimited fields terminated by ',';
OK
Time taken: 0.093 seconds
##本地的数据导入
hive> load data local inpath '/data/lilin/demo/000000_0' into table demo_new;
##hdfs的数据导入
hive> load data inpath '/user/lilin/demo/000000_0' overwrite into table demo_hdfs;