sqoop 将mysql表导入到hive分区表 您所在的位置:网站首页 hive创建静态分区表 sqoop 将mysql表导入到hive分区表

sqoop 将mysql表导入到hive分区表

2023-07-21 04:32| 来源: 网络整理| 查看: 265

使用sqoop 将mysql表导入到hive分区表,两种方法:

第一种,先通过sqoop将mysql表中分区记录导入到hdfs上,然后从hdfs插入数据到hive分区表。

第二种,直接通过sqoop命令导入到hive分区表中。

mysql来源表`sqlzoo`.`nobel` :

Create Table: CREATE TABLE `nobel` ( `yr` int(11) DEFAULT NULL, `subject` varchar(15) DEFAULT NULL, `winner` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1

 创建hive目的表`sqooptest`.`hi_nobel`并按照subject分区:

CREATE TABLE `hi_nobel`( `yr` int, `winner` string) PARTITIONED BY ( `subject` string) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='\t', 'serialization.format'='\t') LOCATION 'hdfs://localhost:9000/xxx/hive/warehouse/sqooptest.db/hi_nobel'

新增一个分区subject='chemistry' :

alter table hi_nobel add partition(subject='chemistry');

1. 第一种方法

    通过sqoop参数--query 查询指定分区记录将sql结果集保存到参数--target-dir 指定hdfs目录下。

sqoop import --connect jdbc:mysql://xxx:3306/sqlzoo --username hive -P --query "select yr,winner from nobel where subject='chemistry' and \$CONDITIONS" --fields-terminated-by '\t' --target-dir /user/xxx/sqooptest/nobel/subject=chemistry --delete-target-dir --m 1

从hdfs文件上插入数据到hive上指定分区表:

load data inpath'hdfs://localhost:9000/user/xxx/sqooptest/nobel/subject=chemistry' into table hi_nobel partition(subject='chemistry');

查看插入数据数目是否一致: select count(*) from nobel where subject='chemistry';

2. 第二种方法

    直接通过sqoop查询参数--query和hive相关参数将数据导入到hive分区表。

新增一个分区subject='physics' :

alter table hi_nobel add partition(subject='physics');

sqoop参数与第一种方法的区别:

--target-dir :将hdfs目录更改为hive warehouse下指定分区表目录。

增加hive侧参数:

--hive-import、--hive-datebase、--hive-table、--hive-partition-key 、--hive-partition-value

sqoop import --connect jdbc:mysql://xxx:3306/sqlzoo --username hive -P --query "select yr,winner from nobel where subject='physics' and \$CONDITIONS" --fields-terminated-by '\t' --target-dir /xxx/hive/warehouse/sqooptest.db/hi_nobel/subject=physics --delete-target-dir --m 1 --hive-import --hive-database sqooptest --hive-table hi_nobel --hive-partition-key subject --hive-partition-value physics

查看插入数据数目是否一致: select count(*) from nobel where subject='physics';

第二种方法明显需要指定更多参数,但省去了在hive上插入数据的操作,生成的_SUCCESS文件也会留在hive目录下。

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有