码迷,mamicode.com
首页 > 其他好文 > 详细

CODE

时间:2018-07-10 14:49:48      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:log   center   query   dfs   jid   table   ase   %s   append   

#!/bin/bash
db_database=inforcenter
jdbc_inforcenter=jdbc:sqlserver://10.0.92.149;username=sa;password=123;database=$db_database
db_table=operatelog
cur_dtime=$(date -d "-1 day" +"%Y-%m-%d %H:%M:%S")
top_table=top_table
hdfs_uri=hdfs://10.0.92.222

#时间转化为时间戳
cur_dtimestamp=$cur_dtime

#检查数据库是否存在
hadoop fs -test -e /apps/hive/warehouse/$db_database
if [ $? -eq 0 ] ;then
	echo ‘Database Exist‘
else
	echo ‘Database Not Exist‘
	echo ‘Create Database‘
	
	#创建inforcenter数据库
	hive -e "create database "${db_database}
fi

#检查表是否存在
hadoop fs -test -e /apps/hive/warehouse/$db_database/$db_table
if [ $? -eq 0 ] ;then
	echo ‘Table Exist‘
else
	echo ‘Table Not Exist‘
	echo ‘Create Table‘
	#创建operatelog表
	sqoop create-hive-table --connect $jdbc_inforcenter --table $db_table --hive-database $jdbc_inforcenter --fields-terminated-by ‘\001‘
fi

#将昨天的数据导入到hive中
sqoop import --connect $jdbc_inforcenter -table $db_table -hive-database $db_database -hive-table $db_table -hive-import -m -1 --incremental append --check-column CREATETIME --last-value $cur_dtimestamp --hive-drop-import-delims --split-by createtime

#创建分析表
hadoop fs -test -e /apps/hive/warehouse/$db_database/$top_table
if [ $? -eq 0 ] ;then
	echo ‘Table Exist‘
else
	echo ‘Table Not Exist‘
	echo ‘Create Table‘
	hive -e "create table "${top_table}"top_table (datainfo string,count int,optype string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\001‘"
fi

#清空历史数据
hive -e "truncate table "${top_table}

#统计ObjType数据
hive -e "insert into "${top_table}" select objtype,count(1) ct,‘objtype‘ from "${db_table}" where objtype!=‘null‘ group by objtype order by ct desc limit 10"
#统计objId数据
hive -e "insert into "${top_table}" select objid,count(1) ct,‘objid‘ from "${db_table}" where objid!=‘null‘ group by objid order by ct desc limit 10"
#operate数据
hive -e "insert into "${top_table}" select operate,count(1) ct,‘operate‘ from "${db_table}" where operate!=‘null‘ group by operate order by ct desc limit 10"
#operate id数据
hive -e "insert into "${top_table}" select concat(objid,‘&‘,operate),count(1) ct,‘objid&operate‘ from "${db_table}" where objid!=‘null‘ group by objid,operate order by ct desc limit 10"
#ip数据
hive -e "insert into "${top_table}" select ip,count(1) ct,‘ip‘ from "${db_table}" where ip!=‘null‘ group by ip order by ct desc limit 10"

#清空sqlserver表数据
sqoop eval --connect $jdbc_inforcenter --query "truncate table "${top_table}

#导出到sqlserver中
sqoop export --connect $jdbc_inforcenter --table $top_table --export-dir $hdfs_uri/apps/hive/warehouse/$db_database/$top_table

  

CODE

标签:log   center   query   dfs   jid   table   ase   %s   append   

原文地址:https://www.cnblogs.com/lerio/p/9288546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!