3 Star 3 Fork 2

JIE / DataX_MySQL_To_HDFS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

项目说明


基于 DataX 开发的快速同步 MySQL 数据至 HDFS 上的工具。自动获取 MySQL 中对象库的所有表,一键自动化批量配置 DataX 可执行 JSON 配置文件,自动化创建本地路径、HDFS 同结构存储路径。省去了繁琐的字段映射、配置文件编写步骤,自动化程度高,效率提升明显。

本项目基于 DataX3.0 开源项目的基础上完成 —— DataX 官网

Quick Start


1.安装 Python3 环境

本项目同时支持 Python3 和 Python2,请根据你的环境进行选择。

下载 Python3 连接 MySQL 的依赖库。

你可以使用 pip3 安装它:

pip3 install PyMySQL

或者:

pip install PyMySQL

如果你使用的是 MySQL8.x 版本,则需要进行额外的扩展下载,因为在 MySQL8.x 中需要进行连接认证,你可以通过如下命令来查询对应用户的认证规则:

SELECT host,user,plugin FROM mysql.user;

当认证方式为 sha256_passwordcaching_sha2_password,你需要安装额外的依赖项:

pip3 install PyMySQL[rsa]

或者:

pip install PyMySQL[rsa]

2.统一文件路径

必须将下列文件放置在同一路径下:

  • 配置文件 config.py

  • JSON 模板文件 template.json

  • 文件创建程序 datax_mysql_hdfs_json.py

  • 程序调度脚本 create_json_files.sh

3.编辑配置文件

按照你的需求,编辑 config.py 配置文件,请检查你配置的相关路径是否具有读写权限,否则程序调度时会出现异常。

不要更改配置文件原有结构、名称,配置内容必须使用英文双引号进行引用。

4.运行任务并创建 JSON 文件

运行脚本文件 create_json_files.sh,它会将本次执行日志信息存储在 /tmp/run_log.txt 文件中,如果遇到异常,请查看该文件内容。

bash create_json_files.sh

正常运行完成后,它将根据你在 config.py 配置文件中设定的信息,生成 DataX 所需的 JSON 配置文件,生成的 JSON 文件将存储在你指定的目录下。

5.可选项——自动批量执行 DataX 任务

自动批量执行 DataX 任务,将 MySQL 数据同步到 HDFS 上的 DataX 脚本 run_datax.sh,需要指定 DataX 安装路径以及存放 JSON 配置文件的路径(目录)。

开始执行前,请先启动 Hadoop 和 MySQL,确保服务能够正常执行,并且脚本拥有执行权限。

bash run_datax.sh $DATAX_HOME $JSON_FILES_HOME

如果你在环境变量中配置了 $DATAX_HOME 变量,那么会默认将该路径作为 DataX 的安装路径,只需要指定 $JSON_FILES_HOME 文件的存放根路径即可。

bash run_datax.sh $JSON_FILES_HOME

运行过程中,会打印对应提示信息,它会将本次运行日志信息存储在 /tmp/run_datax_log.txt 文件中,如果数据同步执行失败,会将失败的文件统计到 /tmp/run_datax_errors.txt 文件中,以供查看。

功能与限制同 DataX 开源项目保持一致,详细内容请查看:hdfswriter

文件列表说明


展开对文件列表的说明。

1.config.py

  • 定义 DataX 的相关配置文件。

2.template.json

  • DataX 的 JSON 模板文件。

3.datax_mysql_hdfs_json.py

  • 根据 config.py 配置文件和 template.json 模板文件,生成 DataX 可执行的 JSON 数据同步文件。

4.create_json_files.sh

  • 文件调度执行脚本,执行成功后,会将创建好的 JSON 数据同步文件存储到你指定的路径下,该路径由你在 config.py 配置文件中指定的 save_json_path 参数决定。

5.run_datax.sh

  • 执行 DataX 数据同步任务,需要传入两个参数:

    • 1.DataX 安装根路径,该参数默认为环境变量中的 $DATAX_HOME,如果你配置了该环境变量,则可以省略该参数。
    • 2.待执行的批量 JSON 文件根路径

    检测到 HDFS 路径未创建时会自动进行创建,前提是需要相关权限。

配置文件参数介绍


展开对配置文件 config.py 的详细说明。

  • username 指定 MySQL 连接账号,无默认值;

  • password 指定 MySQL 连接密码,无默认值;

  • database 指定 MySQL 连接库,无默认值;

  • host 指定 MySQL 连接主机,默认值:localhost

  • port 指定 MySQL 连接端口,默认值:3306

  • defaultFS 指定 HDFS 连接地址,默认值:hdfs://localhost:8020

  • fileName 指定文件在 HDFS 上文件的存储前缀,默认值:当前操作的表名称;

    DataX 在进行数据同步时,为了避免重复路径的存在,采用了 fileName + 随机路径的方式实现,所以定义该值的意义并不大,在这里,直接对其做了默认化处理,也就是将正在操作的表名称作为前缀。当然,你也可以自定义名称。

  • fileType 指定 HDFS 数据存储类型,默认值:text 文本类型【可选数据类型 orc】;

  • path 指定 HDFS 数据存储路径,默认值:/ 根路径;

  • template_path 新增参数,指定 HDFS 中数据存储路径模板,无默认值;

    使用说明:假设要将 MySQL 库 test 所有的表都导入到 HDFS 上的 /ods 路径下的子目录 /2023/09/19 中,那么你将这样实现:

    • 定义 "path": "/ods" 存放主目录;
    • 定义"template_path":"/2023/09/19" 存放模板目录;

    系统在运行过程中会自动补全当前操作的表名称,补全后数据最终的存储路径为:/ods/table_name/2023/09/19/

    其中 table_name 会随着正在操作的表进行变化,该操作为批量执行,执行前请确认是否需要将所有表存储在相同结构的路径下。

  • writeMode 指定文件写入形式,默认值:append 追加模式;

    • append:写入前不做任何处理,直接使用 filename 写入,并保证文件名不冲突。
    • nonConflict:如果目录下有 fileName 前缀的文件,直接报错。
    • truncate:如果目录下有 fileName 前缀的文件,先删除后写入。
  • fieldDelimiter 指定数据行间隔符,默认值:\t 制表符;

  • compress 指定数据压缩模式,默认值:gzip 压缩格式;

    • text 类型文件支持压缩类型:gzipbzip2
    • orc 类型文件支持压缩类型:NONESNAPPY(需要用户安装SnappyCodec);
  • template_json_file_path 新增参数,指定生成 DataX 可执行 JSON 文件的模板文件;

  • save_json_path 新增参数,指定生成的 JSON 文件存储路径,默认值:/opt

    该路径在执行过程中会自动进行创建(需要有创建权限), 最终生成的文件目录为:save_json_path/database_name/

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于 DataX 开发的快速同步 MySQL 数据至 HDFS 上的工具。 展开 收起
Python 等 2 种语言
Apache-2.0
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/h2830201534/data-x_-my-sql_-to_-hdfs.git
git@gitee.com:h2830201534/data-x_-my-sql_-to_-hdfs.git
h2830201534
data-x_-my-sql_-to_-hdfs
DataX_MySQL_To_HDFS
master

搜索帮助