kettle 脚本批量替换数据库连接配置

本质是对kettle脚本文件中的连接信息进行替换

脚本

import xmltodict
import json
import os

node_transformation = 'transformation'
node_job = 'job'

ext_ktr = '.ktr'
ext_kjb = '.kjb'


def get_file_content(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    # print(lines)
    return "".join(lines)


def save_file_content(file_path, content):
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)
    return True


def fix_xml(node, xml_dict):
    if isinstance(xml_dict[node]['connection'], dict):
        xml_dict[node]['connection'] = [xml_dict[node]['connection']]

    if node == node_transformation and isinstance(xml_dict[node]['step'], dict):
        xml_dict[node]['step'] = [xml_dict[node]['step']]


# 替换连接信息
def change_detail(node, xml_dict, config):
    for i, con_detail in enumerate(xml_dict[node]['connection']):
        for pair in config['details']:
            is_match = True
            for key, value in pair['old'].items():
                # print(key)
                # print(con_detail)
                if (value != con_detail[key]):
                    is_match = False
                    break
            if is_match:
                # 用新配置替换老配置
                for key, value in pair['new'].items():
                    xml_dict[node]['connection'][i][key] = value
                break


# 替换连接名称
def change_name(node, xml_dict, config):
    key = 'name'
    for i, con_detail in enumerate(xml_dict[node]['connection']):
        for pair in config['names']:
            if pair['old'] == con_detail[key]:
                xml_dict[node]['connection'][i][key] = pair['new']
                break

    key = 'connection'
    if node == node_transformation:
        for i, step in enumerate(xml_dict[node]['step']):
            for pair in config['names']:
                if pair['old'] == step[key]:
                    xml_dict[node]['step'][i][key] = pair['new']
                    break

    if node == node_job:
        for i, entry in enumerate(xml_dict[node]['entries']['entry']):
            for pair in config['names']:
                if key in entry and pair['old'] == entry[key]:
                    xml_dict[node]['entries']['entry'][i][key] = pair['new']
                    break


# 更新脚本中的数据库连接
def do_change_db(ext, old_script_path, new_script_path, config_path):
    config = json.loads(get_file_content(config_path))
    content = get_file_content(old_script_path)
    xml_dict = xmltodict.parse(content)

    if ext_ktr == ext:
        fix_xml(node_transformation, xml_dict)
        change_detail(node_transformation, xml_dict, config)
        change_name(node_transformation, xml_dict, config)
    if ext_kjb == ext:
        fix_xml(node_job, xml_dict)
        change_detail(node_job, xml_dict, config)
        change_name(node_job, xml_dict, config)

    save_file_content(new_script_path, xmltodict.unparse(xml_dict, pretty=True))


# 批量更新
def change_batch(dir_path, config_path):
    file_names = os.listdir(dir_path)
    for file_name in file_names:
        f, ext = os.path.splitext(file_name)
        print(f, ext)
        if ext_ktr == ext or ext_kjb == ext:
            file_path = os.path.join(dir_path, file_name)
            print('parsing:', file_path)
            do_change_db(ext, file_path, file_path, config_path)


if __name__ == '__main__':
    dir_path = 'D:/xxx/xxx'
    config_path = 'config.json'

    change_batch(dir_path, config_path)

配置文件

{
    "names":
        [
            {
                "old":"xxx",
                "new":"xxx"
            },
            {
                "old":"xxx",
                "new":"xxx"
            }
        ],
    "details":
        [
            {
                "old":
                    {
                        "server": "xxx",
                        "type": "POSTGRESQL",
                        "access": "Native",
                        "database": "xxx",
                        "port": "5432",
                        "username": "postgres",
                        "password": ""
                    },
                "new":
                    {
                        "server": "xxx",
                        "type": "POSTGRESQL",
                        "access": "Native",
                        "database": "xxx",
                        "port": "5432",
                        "username": "xxx",
                        "password": ""
                    }
            },
            {
                "old":
                    {
                        "server": "xxx",
                        "type": "POSTGRESQL",
                        "access": "Native",
                        "database": "xxx",
                        "port": "5432",
                        "username": "xxx",
                        "password": ""
                    },
                "new":
                    {
                        "server": "xxx",
                        "type": "POSTGRESQL",
                        "access": "Native",
                        "database": "xxx",
                        "port": "5432",
                        "username": "xxx",
                        "password": ""
                    }
            }
        ]
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值