实现MySQL数据全量迁移至Hive的简单脚本

1、主要思路:编写脚本执行建表语句、sqoop命令

1.1、编写建表语句脚本

思路:在虚拟机下执行hive -f /脚本路径即可执行hql脚本

1.2、编写shell脚本

脚本内容为分为两部分

  • 执行hql建表语句脚本
  • sqoop迁移命令

2、示范案例:

2.1、hive建表脚本:

-- 示范案例
drop database if exists ods_myshops cascade;
create database ods_myshops;
use ods_myshops;
create table ods_userinfos(
userid int,
username string,
name string,
email string,
password string,
cardno string,
gender int,
localid int,
mobile string,
qq string,
wechat string
);
............

2.2、执行hql脚本和sqoop迁移的shell脚本

#!/bin/bash
# hive数据仓库构建数据表
hive -f /opt/goods/INIT/init.hql

# 使用sqoop从mysql数据库的userinfos表导入数据到hive数据仓库的userinfos表
sqoop import \
--connect jdbc:mysql://192.168.221.221:3306/myshops \
--table userinfos \
--username root \
--password root \
--hive-import \
--hive-database ods_myshops \
--hive-table ods_userinfos \
-m 3

# 将数据库中orders表和orderItem合成一张表,ods_shopinfo导入hive数据仓库
sqoop import \
--connect jdbc:mysql://192.168.221.221:3306/myshops \
--query "select o.orderid,o.orderdate,o.orderstatus,o.orderno,i.goodid,i.buynum 
from orders o inner join orderitems i on o.orderid=i.orderid and \$CONDITIONS" \
--username root \
--password kb10 \
--target-dir /user/order2 \
--split-by orderdate \
--hive-import \
--hive-database ods_myshops \
--hive-table ods_orders \
-m 3

......

3、踩雷点

3.1、脚本在windows环境下编写的话,需要主要换行符是Unix还是windows格式,因为脚本是在linux环境下运行,只能是LF的文档格式
3.2、文本字符集需要改为UTF-8的格式,否则在linux格式下执行会报字符集错误

猜你喜欢

转载自blog.csdn.net/xiaoxaoyu/article/details/114684426
今日推荐