将PostgreSQL库的数据同步到hive库中
#!/bin/bash
# 参数校验
if [ $# -lt 1 ] ; then
echo "期待1个参数"
exit 1
fi
#时间处理
dt="$(date -d "$1 1 day ago" +'%Y-%m-%d')"
# pg库的配置连接信息
rdb="postgresql"
host="**********************"
port="**********************"
user="**********************"
password="******************"
database="******************"
export LANG="en_US.UTF-8"
export PGPASSWORD=${password}
hive_table="****************"
hive_partition="dt='${dt}'"
pg_table="******************"
#路径
current_path="$(cd $(dirname $0);pwd)"
tmp_file="$current_path/data"
pg_sql="
select
name,
sex,
age
from ${pg_table}
"
psql -h${host} -p${port} -d${database} -U${user} -c "\\copy (${sql}) to $tmp_file" || exit 1
hive_sql="
LOAD DATA LOCAL INPATH '$tmp_file' OVERWRITE INTO TABLE ${hive_table} PARTITION(${hive_partition})
"
# load 的表的字段要与pg导到文件的相对应,hive的列还是要以"\t"分隔,并且是文本的存储格式
sudo -u root /home/apache-hive-2.1.1-bin/bin/hive -e " ${hive_sql} " || exit 1