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;

     

  • 相关阅读:
    【数据结构(C语言版)系列三】 队列
    【数据结构(C语言版)系列二】 栈
    【数据结构(C语言版)系列一】 线性表
    [转]Boosting
    吴恩达机器学习笔记
    C语言之图像旋转
    DP【洛谷P4290】 [HAOI2008]玩具取名
    背包 DP【洛谷P4158】 [SCOI2009]粉刷匠
    最短路+状压DP【洛谷P3489】 [POI2009]WIE-Hexer
    模板 Trie树
  • 原文地址:https://www.cnblogs.com/wingler/p/12456234.html
Copyright © 2011-2022 走看看