shell清晰日志文件到hive(先清洗出json文件,再将json转换为结构化数据)

#判断json中是否有值这个key,有就返回value。没有就返回NULL。
#传入两个参数,$1是key名,$2是整个一行json。
fun_col(){
    echo $2 > col1
    col=`cat col1|grep $1  |wc -l`
    if [ $col != 0 ];then
        value=`cat col1 |awk -F "${1}\":" '{print $2}' | awk -F ',' '{print $1}'`
           echo "$value"
    else
        value=\\N
        echo "$value"
    fi    
#上面的echo就已经return了    
#    return $value
}

#每次运行清空存储的文件。
>test.txt

date_yesterday=`date -d "1 day ago" +"%Y-%m-%d"`
#匹配原始日志,最后拿到json文件
cat e_charger_user_other_service.${date_yesterday}.log |grep "获取用户信息成功" > logs_${date_yesterday}
cat logs |grep "getInfo],result"|awk -F '"data":' '{print $2}'|awk -F ',"info":"' '{print $1}' >data_${date_yesterday}.json

while read line 
do
    idNo=`fun_col idNo $line`
    idPicBack=`fun_col idPicBack $line`
    idPicFront=`fun_col idPicFront $line`
    idType=`fun_col idType $line`
    isIdChecked=`fun_col isIdChecked $line`
    isInternalEmployee=`fun_col isInternalEmployee $line`
    isOrderCharge=`fun_col isOrderCharge $line`
    isPayPwdSet=`fun_col isPayPwdSet $line`
    phone=`fun_col phone $line`
    realName=`fun_col realName $line`
    regChannel=`fun_col regChannel $line`
    regTime=`fun_col regTime $line`
    sex=`fun_col sex $line`
    token=`fun_col token $line`
    updateTime=`fun_col updateTime $line`
    userId=`fun_col userId $line`
    userState=`fun_col userState $line`
    userType=`fun_col userType $line`

#\001分割符 分割符根据自己的换。
    echo "${idNo}${idPicBack}${idPicFront}${idType}${isIdChecked}${isInternalEmployee}${isOrderCharge}${isPayPwdSet}${phone}${realName}${regChannel}${regTime}${sex}${token}${updateTime}${userId}${userState}${userType}" >>test.txt
done < data_${date_yesterday}.json
#去掉最后的 } 和 string类型数据两边的""
sed -i 's/}//' test.txt
sed -i 's/\"//g' test.txt
mv test.txt data_${date_yesterday}.txt
#上传清理完的.txt文件到hdfs的相应目录。
hadoop fs -put -f data_${date_yesterday}.txt /user/hive/inceptor/user/hive/warehouse/default.db/gwddc_admin/test_json/

 

posted @ 2020-03-21 02:55  singsong~  阅读(340)  评论(0编辑  收藏  举报