DataX

DataX

DataX 是阿里巴巴开源的一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL、Oracle等)、HDFS、Hive、ODPS、HBase、FTP等各种异构数据源之间稳定高效的数据同步功能。源码地址:https://github.com/alibaba/DataX

1.安装部署

//上传,通过MobaXterm软件上传安装包放到已创建目录software
//解压,压缩包修改成自己下载的
tar -zxvf /opt/software/datax.tar.gz -C /opt/module/

使用DataX自带的job.json测试

#DataX运行命令
python /opt/module/datax/bin/datax.py /opt/module/datax/job/job.json

出现以下界面为安装成功 

2.DataX配置文件生成

#将生成器上传到服务器的/opt/module/gen_datax_config目录
mkdir /opt/module/gen_datax_config
#切换目录
cd /opt/module/gen_datax_config

 上传生成器

DataX配置文件生成器百度网盘链接: https://pan.baidu.com/s/1PrFpGQW61Smc2dhAiksd6w 提取码: ijtj 

 

生成器配置读取MySQL数据
#修改configuration.properties配置
vi configuration.properties
#修改内容(读取MySQL数据)
mysql.username=root
mysql.password=000000
mysql.host=hadoop01
mysql.port=3306
mysql.database.import=financial_lease
mysql.database.export=
mysql.tables.import= business_partner,department,employee,industry
mysql.tables.export=
is.seperated.tables=0
hdfs.uri=hdfs://hadoop01:8020
import_out_dir=/opt/module/datax/job/financial_lease/import
export_out_dir=

#运行jar包
java -jar datax-config-generator-1.0-SNAPSHOT-jar-with-dependencies.jar
#查看生成内容
cd /opt/module/datax/job/financial_lease/import

DataX导出数据到Hdfs
#创建hdfs目录,-p是递归创建
hdfs dfs -mkdir -p /origin_data/financial_lease/business_partner_full/2023-05-09
#DataX运行命令,后面是上传的hdfs路径
python /opt/module/datax/bin/datax.py /opt/module/datax/job/financial_lease/import/financial_lease.business_partner.json -p"-Dtargetdir=/origin_data/financial_lease/business_partner_full/2023-05-09" 

 另外三张表同样重复以上操作,记得改路径和文件名

3. 全量表数据同步脚本

#在/bin目录创建mysql_to_hdfs_full.sh,并粘贴下面脚本内容
vi /bin/financial_mysql_to_hdfs_full.sh
#!/bin/bash
DATAX_DATA=/opt/module/datax/job/financial_lease/import

#如果传参 使用传参的日期 如果不传参数 使用默认值昨天
if [[ -n "$2" ]]; then
  #statements
  do_date=$2
else
  do_date=${date -d "-1 day" +%F}
fi


handle_targetdir(){
  hadoop fs -rm -r $1 >/dev/null 2>&1
  hadoop fs -mkdir -p $1
}

#数据同步
import_data(){
  target_dir=$1
  datax_config=$2
  handle_targetdir $target_dir
  echo "导入表格数据$datax_config"
  python /opt/module/datax/bin/datax.py -p"-Dtargetdir=$target_dir" $datax_config >/tmp/datax_run.log 2>&1
  if [[ $? -ne 0 ]]; then
    #statements
    echo "导入数据失败 日志如下"
    cat /tmp/datax_run.log
  fi
}

case $1 in
  "business_partner" | "department" | "employee" | "industry"  )
  import_data "/origin_data/financial_lease/${1}_full/$do_date" $DATAX_DATA/financial_lease.${1}.json 

    ;;
  "all" )
  for table in "business_partner"   "department"  "employee"  "industry";
  do
  import_data "/origin_data/financial_lease/${table}_full/$do_date" $DATAX_DATA/financial_lease.${table}.json 
  done

    ;;
esac
#赋权
chmod 777 /bin/financial_mysql_to_hdfs_full.sh
#运行,四张表全量同步
financial_mysql_to_hdfs_full.sh all 2023-06-09
#查看hdfs的数据
hdfs dfs -text /origin_data/financial_lease/industry_full/2023-06-09/*
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值