#!/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 "