zoukankan      html  css  js  c++  java
  • 使用sqoop将hive分区表的数据导入到mysql的解决方案:shell脚本循环

    方案1:使用shell脚本进行do while循环,挨个分区输出。从开始的分区一直循环到结束分区即可。本例子为月,若需日,改为%Y-%m-%d即可,-1month改为-1day即可

    partition_month=`date -d "$start_date" +"%Y-%m"`
    partition_month_end=`date -d "$end_date" +"%Y-%m"`
    while [[ ${partition_month} < $partition_month_end ]]
    do
    sqoop eval --connect "jdbc:mysql://。。。。。。。。。。。
    --password "。。。。。。
    --input-null-string '\N' --input-null-non-string '\N'
    --table 表的名字
    --export-dir hdfs://。。。/hive/warehouse/路径/表名字/分区字段=$partition_month
    --input-fields-terminated-by '01';
    partition_month=`date -d "$partition_month -1 month" +"%Y-%m"`
    echo "partition_month:"${partition_month}
    done;

    方案2:把要同步的数据拿出来,建立临时表,从临时表同步数据过去

    具体例子: 

    if [ ! -n "$1" ] ;then
    start_date=`date --date='30 days ago' +%Y-%m-%d`
    echo "start_date: "${start_date}
    elif [ `date -d "$1" +%s` -lt `date -d "2018-01-01" +%s` ];
    then
    start_date='2018-01-01'
    else
    start_date=`date -d "$1" +%Y-%m-%d`
    fi
    if [ ! -n "${start_date}" ];
    then
    start_date=`date --date='30 days ago' +%Y-%m-%d`
    fi

    echo "start_date: "${start_date}

     

    if [ ! -n "$2" ] ;then
    end_date=`date --date='1 days ago' +%Y-%m-%d`
    echo "end_date: "${end_date}
    elif [ `date -d "$2" +%s` -lt `date -d "2019-01-01" +%s` ];
    then
    end_date=`date --date='1 days ago' +%Y-%m-%d`
    else
    end_date=`date -d "$2" +%Y-%m-%d`
    fi

    if [ ! -n "${end_date}" ];
    then
    end_date=`date --date='1 days ago' +%Y-%m-%d`
    fi
    echo "end_date: "${end_date} 

    partition_month=`date -d "$start_date" +"%Y-%m"`
    partition_month_end=`date -d "$end_date" +"%Y-%m"`

    echo "partition_month:"${partition_month}
    echo "partition_month_end:"${partition_month_end}


    while [[ ${partition_month} < $partition_month_end ]]
    do
    sqoop eval --connect "jdbc:mysql://。。。。。。。。。。。
    --password "。。。。。。
    --input-null-string '\N' --input-null-non-string '\N'
    --table 表的名字
    --export-dir hdfs://。。。/hive/warehouse/app/。。。。/分区字段=$partition_month
    --input-fields-terminated-by '01';
    partition_month=`date -d "$partition_month -1 month" +"%Y-%m"`
    echo "partition_month:"${partition_month}
    done;

     

  • 相关阅读:
    Promise 对象
    [转] LVM分区在线扩容
    [转] 打开 CMD 时自动执行命令
    [转] FFmpeg常用基本命令
    systemd 之 journalctl
    systemd 之 systemctl
    关于用户权限的加强与理解(上)
    [转] 测试环境下将centos6.8升级到centos7的操作记录
    [搞机] 双网卡做数据均衡负载
    [转] 网络基础知识1:集线器,网桥,交换机
  • 原文地址:https://www.cnblogs.com/wingler/p/12456234.html
Copyright © 2011-2022 走看看