从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下

阅读目录

本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文连接,谢谢合做。html

文章是哥(mephisto)写的,SourceLinknode

 

     上一篇,咱们介绍了Hive的数据多种方式导入,这样咱们的Hive就有了数据来源了,但有时候咱们可能须要纯粹的导出,或者集群Hive数据的迁移(不一样集群,不一样版本),咱们就能够经过这两章的知识来实现。数据库

   下面咱们开始介绍hive的数据导出,以及集群Hive数据的迁移进行描述。app

将查询的结果写入文件系统

一:说明

  将上篇中从其余表导入语法进行简单的修改,就能够将查询的结果写入到文件系统。oop

二:语法:

Standard syntax:
INSERT OVERWRITE [LOCAL] DIRECTORY directory1
  [ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive 0.11.0)
  SELECT ... FROM ...
 
Hive extension (multiple inserts):
FROM from_statement
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ...
 
 
row_format
  : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
        [NULL DEFINED AS char] (Note: Only available starting with Hive 0.13)

三:写入到本地

  若是使用LOCAL,则数据会写入到本地测试

四:写入到集群

  若是不使用LOCAL,则数据会写到指定的HDFS中,若是没写全路径,则使用Hadoop的配置项fs.default.name (NameNode的URI)。spa

五:实战

  修改tmp文件夹权限(这里只是测试,因此使用最大权限)3d

chmod 777 tmp

  进入Hivecode

sudo -u hdfs hive

 

  将上一篇中的score表数据导出到本地orm

insert overwrite local directory  '/data/tmp/score' select * from score;

  咱们能够看到/data/tmp/score/目录下有文件。

cd /data/tmp/score
ll

  这样咱们就把hive的数据导出到本地了。

  下面咱们使用不带local参数的命令,将hive表数据导到hdfs中

insert overwrite  directory  '/data/tmp/score' select * from score;

  咱们使用hdfs的ls命令查看

hadoop fs -ls /data/tmp/score

  这里文件只有一个,和上面的不同,但总的内容是同样的,上面一样的数据导出,有时候也只有一个文件。这里就不作考究了。

集群数据迁移一

一:介绍

  在官网里,咱们能够看到EXPORT和IMPORT,该功能从Hive0.8开始加入进来。

二:Export/Import

  导出命令根据元数据导出表或者分区,输出位置能够是另外一个Hadoop集群或者HIVE实例。支持带有分区的表。导出元数据存储目标目录,数据文件存储在子目录

  导入导出的源和目标的元数据存储DBMS能够是不一样的关系型数据库。

三:Export语法

EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
  TO 'export_target_path'

四:Import语法 

IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
  FROM 'source_path'
  [LOCATION 'import_target_path']

五:官方例子 

  简单导入导出

export table department to 'hdfs_exports_location/department';
import from 'hdfs_exports_location/department';

  更名导入导出

export table department to 'hdfs_exports_location/department';
import table imported_dept from 'hdfs_exports_location/department';

  分区导出

export table employee partition (emp_country="in", emp_state="ka") to 'hdfs_exports_location/employee';
import from 'hdfs_exports_location/employee';

  分区导入

export table employee to 'hdfs_exports_location/employee';
import table employee partition (emp_country="us", emp_state="tn") from 'hdfs_exports_location/employee';

  指定导入位置

export table department to 'hdfs_exports_location/department';
import table department from 'hdfs_exports_location/department' 
       location 'import_target_location/department';

  做为外部表导入

export table department to 'hdfs_exports_location/department';
import external table department from 'hdfs_exports_location/department';

  

集群数据迁移二

一:介绍

  虽然官方的Export/Import命令很强大,但在实际使用中,多是版本的不一样,会出现没法导入的状况,本身在这块也琢磨了下,总结出本身的一套带有分区的Hive表数据迁移方案,该方案在Cloudera和Hontorworks的集群中成功迁移过,Hive版本也不一致。

二:导出数据

  因为Cloudera的发行版本CDH-5.3.3的Hive版本低于0.8因此用这个做为数据源。

  建立带分区表score

create table score (
  id                int,
  studentid       int,
  score              double
)
partitioned by (openingtime string);

 

  根据上一篇中导入数据的方式导入7,8月数据

load data local inpath '/data/tmp/score_7.txt' overwrite into table score PARTITION (openingtime=201507);

  参考咱们上面的导出到本地仍是放在/data/tmp/score下

insert overwrite local directory  '/data/tmp/score' select * from score;

三:迁移数据

  在另一个集群新建/data/tmp目录

 mkdir  -p /data/tmp/score

  拷贝数据

 scp /data/tmp/score/* root@h188:/data/tmp/score/

   查看

cd /data/tmp/score
ll

四:建立分区表和没有分区的临时表

  被导入的集群是Hortonworks的HDP-2.7.1发行版本。

  分区表就是咱们最终的目标表,没有分区的临时表时过分用的。

  进入Hive

sudo -u hdfs hive

  建立带分区的表

create table score (
  id                int,
  studentid       int,
  score              double
)
partitioned by (openingtime string);

  建立不带分区的临时表

 create table score1(
     id int,
     studentid int,
     score double,
     openingtime string
);

五:将数据导入临时表

 

load data local inpath '/data/tmp/score' into table score1;

  咱们查下导进来的数据

select * from score1;

六:从临时表导入到分区表

set  hive.exec.dynamic.partition=true;   
set  hive.exec.dynamic.partition.mode=nonstrict;   
set  hive.exec.max.dynamic.partitions.pernode=10000; 
#导入
insert overwrite table score partition(openingtime) select * from score1;

查询

select * from score;

咱们在hdfs中查看下hive的文件

hadoop fs -ls -R /apps/hive/warehouse/score

能够明显的看到根据openingtime分区了。

七:删除临时表

drop table score1

 八:删除临时数据

rm -rf /data/tmp/score

  

 这样咱们的Hive集群数据迁移告一段落。

 

--------------------------------------------------------------------

  到此,本章节的内容讲述完毕。

系列索引

  【源】从零自学Hadoop系列索引

 

 

 

 

本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文连接,谢谢合做。

文章是哥(mephisto)写的,SourceLink

相关文章
相关标签/搜索