datax對應腳本刪除mysql部分數據

9 篇文章 0 订阅
# coding=utf-8
import json
import getopt
import os
import sys
import MySQLdb
import datetime

#MySQL相关配置,需根据实际情况作出修改
mysql_host = "8.212.95.XXX"
mysql_port = "3306"
mysql_user = "dw_readwrite"
mysql_passwd = "XXX"

#HDFS NameNode相关配置,需根据实际情况作出修改
hdfs_nn_host = "mycluster"
hdfs_nn_port = "8020"

#生成配置文件的目标路径,可根据实际情况作出修改
output_path = "/opt/module/datax/job/export/carry/ticket"


def get_time():
    today = datetime.date.today()
    last_friday = today - datetime.timedelta(days=today.weekday() + 3)
    return last_friday


def get_connection():
    return MySQLdb.connect(host=mysql_host, port=int(mysql_port), user=mysql_user, passwd=mysql_passwd)


def get_mysql_meta(database, table):
    connection = get_connection()
    cursor = connection.cursor()
    sql = "SELECT COLUMN_NAME,DATA_TYPE from information_schema.COLUMNS WHERE TABLE_SCHEMA=%s AND TABLE_NAME=%s ORDER BY ORDINAL_POSITION"
    cursor.execute(sql, [database, table])
    fetchall = cursor.fetchall()
    cursor.close()
    connection.close()
    return fetchall


def get_mysql_columns(database, table):
    return map(lambda x: x[0], get_mysql_meta(database, table))


def generate_json(target_database, target_table):
    datestr = get_time()

    job = {
        "job": {
            "setting": {
                "speed": {
                    "channel": 3
                },
                "errorLimit": {
                    "record": 0,
                    "percentage": 0.02
                }
            },
            "content": [{
                "reader": {
                    "name": "hdfsreader",
                    "parameter": {
                        "path": "${exportdir}",
                        "defaultFS": "hdfs://" + hdfs_nn_host + ":" + hdfs_nn_port,
                        "column": ["*"],
                        "fileType": "orc",
                        "encoding": "UTF-8",
                        "fieldDelimiter": "\t",
                        "nullFormat": "\\N"
                    }
                },
                "writer": {
                    "name": "mysqlwriter",
                    "parameter": {
                        "writeMode": "replace",
                        "username": mysql_user,
                        "password": mysql_passwd,
                        "column": get_mysql_columns(target_database, target_table),
                        "preSql": ["delete from " + target_table + " where end_date = '" + str(datestr) + "'"],
                        "connection": [
                            {
                                "jdbcUrl":"jdbc:mysql://" + mysql_host + ":" + mysql_port + "/" + target_database + "?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                                "table": [target_table]
                            }
                        ]
                    }
                }
            }]
        }
    }
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    with open(os.path.join(output_path, ".".join([target_database, target_table, "json"])), "w") as f:
        json.dump(job, f)


def main(args):
    target_database = ""
    target_table = ""

    options, arguments = getopt.getopt(args, '-d:-t:', ['targetdb=', 'targettbl='])
    for opt_name, opt_value in options:
        if opt_name in ('-d', '--targetdb'):
            target_database = opt_value
        if opt_name in ('-t', '--targettbl'):
            target_table = opt_value

    generate_json(target_database, target_table)

if __name__ == '__main__':
    main(sys.argv[1:])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一些关于使用DataX进行MySQL到Hive数据迁移的指导。 首先,您需要安装DataX并配置好环境。然后,您需要编写一个DataX任务配置文件,以指定源和目标数据源的详细信息。 以下是一个简单的MySQL到Hive的DataX任务配置文件示例: ``` { "job": { "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "your_mysql_username", "password": "your_mysql_password", "column": [ "column1", "column2", "column3" ], "connection": [ { "jdbcUrl": "jdbc:mysql://your_mysql_server:3306/your_mysql_database", "table": [ "your_mysql_table" ] } ] } }, "writer": { "name": "hdfswriter", "parameter": { "defaultFS": "hdfs://your_hdfs_server:9000", "fileType": "text", "path": "/your_hdfs_path", "fileName": "your_hdfs_file_name", "fieldDelimiter": "\t", "writeMode": "append", "column": [ "column1", "column2", "column3" ] } } } ], "setting": { "speed": { "channel": "3" } } } } ``` 在上面的配置文件中,您需要将以下信息进行替换: - `your_mysql_username`:MySQL数据库的用户名。 - `your_mysql_password`:MySQL数据库的密码。 - `your_mysql_server`:MySQL数据库的服务器地址。 - `your_mysql_database`:MySQL数据库的名称。 - `your_mysql_table`:要迁移的MySQL表的名称。 - `your_hdfs_server`:HDFS服务器的地址。 - `your_hdfs_path`:HDFS中要写入数据的路径。 - `your_hdfs_file_name`:在HDFS中要写入的文件名。 - `column1`,`column2`和`column3`:要迁移的列名称。 完成配置文件后,您可以使用以下命令来执行DataX任务: ``` python datax.py your_job_config.json ``` 这将启动DataX并开始将MySQL表中的数据传输到Hive表中。 希望这可以帮助您进行MySQL到Hive的数据迁移。如果您有任何其他问题,请随时问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_37759590

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值