querydate.sh

#!/bin/bash
display_help() {
    cat <<EOF
    Usage: ./querydate.sh <command> [options]...

    commands:
        help                    display this text
        --path                  数据存储位置
        --date                  查询某一给定日期的数据
        --from                  查询该日期以后的数据
        --to                    与--from配合使用,查询日期在(from, to]的数据
EOF
}

error() {
  local msg=$1
  local exit_code=$2

  echo "Error: $msg" >&2

  if [ -n "$exit_code" ] ; then
    exit $exit_code
  fi
}
    

###################
# Main
###################
q_path=""
q_date=""
q_from=""
q_to=""

mode=$1

if [ "$mode" = "help" ] || [ "$mode" = "" ]; then
    display_help
    exit 0
fi

while [ -n "$*" ] ; do
    arg=$1
    shift

    case "$arg" in
      --path)
        [ -n "$1" ] || error "选项 --path 需要指定一个参数" 1
        q_path=$1
        shift
        ;;
      --date)
        [ -n "$1" ] || error "选项 --date 需要指定一个参数" 1
        q_date=$1
        shift
        ;;
      --from)
        [ -n "$1" ] || error "选项 --from 需要指定一个参数" 1
        q_from=$1
        shift
        ;;
      --to)
        [ -n "$1" ] || error "选项 --to 需要指定一个参数" 1
        q_to=$1
        shift
        ;;
    esac
done

echo $q_path $q_date $q_from $q_to

if [ "$q_path" == "" ]; then
    error "必须指定 --path 属性"
    exit 1
fi
q_path="$q_path/`date '+%Y%m%d'`"
sql="insert overwrite local directory '$q_path' row format delimited fields terminated by '\t' select value from xm.log where 1=1 "
if [ "$q_date" != "" ]; then
    condition="and $q_date=date "
elif [ "$q_from" != "" ]; then
    condition="and unix_timestamp(date, 'yyyyMMdd') > unix_timestamp($q_from, 'yyyyMMdd') "
    if [ "$q_to" != "" ]; then
        condition="$condition unix_timestamp(date, 'yyyyMMdd') <= unix_timestamp($q_to, 'yyyyMMdd')"
    fi
fi

echo "hive -e \"$sql $condition\""

hive -e "insert overwrite local directory '/home/xiaoming/tmp/20150817' row format delimited fields terminated by '\t' select value from xm.log where 1=1  "

转载于:https://my.oschina.net/u/1474514/blog/493906

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值