李双喆 2015-09-29
一.序言
简单分享个sqoop 从mysql 集群导入到hdfs hive-table 里面进行分析的例子。
sqoop1.4.6,hadoop2.7,hive1.2.1 ,因为 环境原因,sqoop 没有升级到2~。~
二.简单步骤
1.base_import.sh : 集中配置一些链接信息,和公共参数,并引入其他脚本执行
2.base_common.sh : 要导入表的基本信息,包含字段,分组 where 条件 等
3.run.sh : 循环执行模板,通过1,2脚本参数传入,进行执行
4.*.sql : hive 里面的 建表建库信息,最好提前建立
三.脚本
3.1 base_import.sh
#!/bin/sh
# 执行,run.sh 和当前文件同级
base_home='/home/sh_dir/queue'
# 所有数据库信息
export username='admin'
export password='123'
export database_url='jdbc:mysql://1.1.1.1:3306'
#1.创建表 /sql
#2.创建分区关联
#3.添加导入新表的结构
#4.导入
#时间控制:指定时间,转换微秒时间,字符串转换,后一天转换
export init_date='2015-07-08 00:00:00'
export min_time=`date -d "${init_date}" +%s000`
export mid_time=`date -d "${init_date} 1 days" "+%Y-%m-%d"`
export max_time=`date -d "${mid_time}" +%s000`
# 创建表结构和分区:只需要加载一次
# hive -f sql/queue.sql
# hive -f sql/queue_message.sql
# 加载导入的数据项
source ${base_home}/base_common.sh
# 执行导入
source ${base_home}/run.sh 3.2 base_common.sh
#!/bin/sh
# 变的参数定义
# 数据库名字和数据库数量
export base_database_name='queue'
# database 数量,一共4个库
export dnum=4
# 要导入的表
export tables=(queue queue_message)
# 导入进hive 库的名称
export target_database='queue'
# -------------- queue表 最为例子 ----------------------
export tb1='queue'
# 分割数据
export $tb1'_split_by'="create_time"
# where 条件
export $tb1'_where'="$(eval echo \$${tb1}_split_by)>=${min_time} and $(eval echo \$${tb1}_split_by)<${max_time} "
# 导出字段
export $tb1'_columns'="queue_id,create_time,op_time"
# -------------- 其他表例子 ----------------------3.3 run.sh
#!/bin/sh
# 循环导入,从1开始
for ((i=1;i<=$dnum;i++))
do
for table_name in ${tables[*]}
do
# 导入
sqoop import \
-connect ${database_url}/${base_database_name}${i} -username $username -password $password \
-table ${table_name} \
-hive-import \
-hive-database ${target_database} \
-hive-table ${table_name}${i} \
-split-by "$(eval echo \$${table_name}_split_by)" \
-where "$(eval echo \$${table_name}_where)" \
-columns "$(eval echo \$${table_name}_columns)" \
-fields-terminated-by '\t' \
-null-string '\\N' \
-null-non-string '\\N' \
-verbose
done
done
# 像按天增量,有些属性没用
#-delete-target-dir:存在目录则先删除【不用】
#--hive-overwrite:覆盖数据【不用】
#--split-by:根据某个字段进行分组,默认分成4份
#-hive-database:指定hive数据库
#--null-string '\\N' --null-non-string '\\N' :默认将null 和非法字符转为 NULL,不然会成为’null’ 字符串
#-column:’id,name,pwd’ 指定导出的字段
#--warehouse-dir : 指定hive 导出的目录
$-where : 指定条件3.4 queue.sql
-- queue_all : hive 的数据库
-- queue : 对应数据库表
set hivevar:database=queue_all;
set hivevar:tablename=queue;
create database IF NOT EXISTS queue_all;
use queue_all;
-- 创建排队表
CREATE TABLE IF NOT EXISTS queue (
`queue_id` STRING ,
`entity_id` STRING,
`create_time` BIGINT,
`op_time` BIGINT,
)PARTITIONED BY(pt INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
ESCAPED BY '\001'
LINES TERMINATED BY '\012' STORED AS TEXTFILE;
-- 分区关联:将几个库的全部链接起来,相当于从一个库查询
alter table ${tablename} add if not exists partition(pt=1) location "hdfs://master:9000/user/hive/warehouse/${tablename}.db/${tablename}1";
alter table ${tablename} add if not exists partition(pt=2) location "hdfs://master:9000/user/hive/warehouse/${tablename}.db/${tablename}2";
alter table ${tablename} add if not exists partition(pt=3) location "hdfs://master:9000/user/hive/warehouse/${tablename}.db/${tablename}3";
alter table ${tablename} add if not exists partition(pt=4) location "hdfs://master:9000/user/hive/warehouse/${tablename}.db/${tablename}4";四.小结:
1.上面省略了很多配置,包括日志等信息
2.只分享了个简单例子,基本上拿过去就能配置使用了,如果更复杂的 还是得看文档
3.脚本不方便(我脚本弱),sqoop2 支持java,最好升级来用
4.有错请提出,或者有更好的方式,请多建议,虽然mysql 可以直接像hadoop 导出,但是因为数据库很多种,就选了脚本 相对兼容的方式。