ETL-使用kettle批量复制sqlserver数据到mysql数据库

文章标题

yaml 复制代码
title: ETL-使用kettle批量复制sqlserver数据到mysql数据库
date: 2023-11-21 10:21:53
tags: ETL
cover: https://gulimall-ayu.oss-cn-chengdu.aliyuncs.com/blog/QQ%E5%9B%BE%E7%89%8720231121133353.png

1、安装sqlserver数据库

java 复制代码
#安装之前我们准备好挂载文件夹:/opt/module/mssql
#并且修改文件夹所有者:  chown -R 10001:0 ./opt/module/mssql

docker run \
 --name mssql \
 -e 'ACCEPT_EULA=Y' \
 -e 'MSSQL_SA_PASSWORD=XLYqwe123' \
 -p 1433:1433 \
 -v /opt/module/mssql:/var/opt/mssql \
 --restart=always \
 -d mcr.microsoft.com/mssql/server:2017-latest



#进入容器命令:
docker exec -it 容器id /bin/bash


#登录命令:
 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "XLYqwe123"


#然后我们就可以创建一些表用来模拟传输数据

2、下载kettle

java 复制代码
kettle在外网下载起来非常慢,这是我使用的版本
链接:https://pan.baidu.com/s/142eHrLx5AjmGxwCEbabfCw?pwd=uqmh 
提取码:uqmh

3、业务分析

java 复制代码
现在一共是四百多张表在sqlserver里面,直接用navicat的传输工具要报错,
在kettle里面是这样解决的,先根据sqlserver的表生成mysql的建表语句(ddl),然后
在将sqlserver的表格数据插入过去。

4、详细流程

java 复制代码
流程完全是copy的这个文章:
https://blog.csdn.net/xuyang2059/article/details/124431556?spm=1001.2014.3001.5502

总共涉及到两个工作流,4个转换算子
(1)转换1:获取sqlserver所有表格名字,将记录复制到结果
sql 复制代码
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_NAME;
(2)转换2:从结果设置变量
(3)转换3:生成建表的DDL
sql 复制代码
declare @table varchar(100) = '${TNAME}'
declare @sql table(s varchar(1000), id int identity)
-- 创建语句
insert into  @sql(s) values ('create table if not exists ${TNAME} (')

-- 获取注释
SELECT A.name  AS table_name,
       B.name  AS column_name,
       C.value AS column_description
into #columnsproperties
FROM sys.tables A
         INNER JOIN sys.columns B ON B.object_id = A.object_id
         LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id AND C.minor_id = B.column_id
WHERE A.name = @table

-- 获取列的列表,拼接语句
insert into @sql(s)
select '  `' + replace(lower(a.column_name),' ','') + '` ' +
       case data_type
           when 'datetime2' then 'datetime'
           when 'datetimeoffset' then 'datetime'
           when 'smalldatetime' then 'datetime'
           when 'money' then 'decimal(19,4)'
           when 'smallmoney' then 'decimal(19,4)'
           when 'nchar' then 'varchar'
           when 'ntext' then 'text'
           when 'nvarchar' then 'varchar'
           when 'char' then 'varchar'
           when 'real' then 'float'
           when 'numeric' then 'decimal'
           when 'uniqueidentifier' then 'varchar(40)'
           when 'xml' then 'text'
           when 'image' then 'longblob'
           else data_type
           end +
       coalesce(case data_type when 'image' then '' else '(' + cast(abs(character_maximum_length) as varchar) + ')' end, '') + ' ' +
       (case when IS_NULLABLE = 'NO' then 'NOT ' else '' end) + 'NULL ' +
       replace(replace(coalesce('DEFAULT ' + COLUMN_DEFAULT, ''), '(', ''), ')', '') +
       case
           when isnull(convert(varchar, b.column_description), '') <> ''
               then '/**' + isnull(convert(varchar, b.column_description), '') + '**/,'
           else ',' end
from INFORMATION_SCHEMA.COLUMNS a
         left join #columnsproperties b
                   on convert(varchar, a.column_name) = convert(varchar, b.column_name)
where a.table_name = @table
order by ordinal_position

-- etl日期字段
insert into @sql(s)
values ('  etl_date datetime NOT NULL ,')

-- 主键
declare @pkname varchar(100)
select @pkname = constraint_name
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where table_name = @table
  and constraint_type = 'PRIMARY KEY'
if (@pkname is not null)
    begin
        insert into @sql(s) values ('  PRIMARY KEY (')
        insert into @sql(s)
        select '   ' + COLUMN_NAME + ','
        from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
        where constraint_name = @pkname
        order by ordinal_position
        -- 去除尾部多余的字符
        update @sql set s=left(s, len(s) - 1) where id = @@identity
        insert into @sql(s) values ('  )')
    end
else
    begin
        -- 去除尾部多余的字符
        update @sql set s=left(s, len(s) - 1) where id = @@identity
    end
-- 继续拼接
insert into @sql(s)
values (')')

drop table #columnsproperties

-- 输出结果
select stuff((select CHAR(10) + s from @sql order by id for xml path('')), 1, 1, '') as ddl
(4)转换4:迁移数据到mysql
(5)工作流1:单表同步流程
(6)工作流2:主流程


相关推荐
weixin_307779132 天前
Jenkins Pipeline: Multibranch 插件详解:现代CI/CD的多分支管理利器
运维·开发语言·自动化·jenkins·etl
weixin_307779132 天前
Jenkins声明式管道扩展点API:深度解析与应用指南
开发语言·ci/cd·自动化·jenkins·etl
weixin_307779134 天前
Jenkins Pipeline: Basic Steps 插件详解
开发语言·ci/cd·自动化·jenkins·etl
weixin_307779134 天前
Jenkins Pipeline:Groovy插件全解析:从原理到实战应用
开发语言·ci/cd·自动化·jenkins·etl
weixin_307779134 天前
Jenkins Pipeline: API 插件详解
运维·ci/cd·自动化·jenkins·etl
weixin_307779134 天前
Jenkins Declarative Pipeline:现代CI/CD的声明式实践指南
开发语言·ci/cd·自动化·jenkins·etl
weixin_307779135 天前
Jenkins Structs 插件:为插件提供命名(DSL)支持的核心库
开发语言·ci/cd·架构·jenkins·etl
RestCloud6 天前
如何用ETL做实时风控?从交易日志到告警系统的实现
数据库·数据仓库·kafka·数据安全·etl·数据处理·数据集成
RestCloud8 天前
人大金仓数据库集成实战:ETL 如何解决国产化替代挑战
数据库·数据仓库·etl·数据集成·数据同步·人大金仓
亚林瓜子8 天前
AWS中国云中的ETL之从aurora搬数据到s3(Glue版——修复版)
云计算·etl·aws·s3·glue