---
title: "Shell脚本执行Hive语句"
author: "dajuezhao"
date: "2010-10-27T09:50:41Z"
tags: []
categories: ["企业架构"]
slug: "shell-hive"
draft: false
lastmod: "2010-10-27T09:50:41Z"
id: 795193
url: "https://dajuezhao.iteye.com/blog/795193"
---

一、环境

1、Hadoop 0.20.2

2、Hive 0.5

3、操作系统 Linux m131 2.6.9-78.8AXS2smp #1 SMP Tue Dec 16 02:42:55 EST 2008 x86\_64 x86\_64 x86\_64 GNU/Linux

二、步骤

1、直接贴上脚本吧  
#!/bin/bash  
#give the params: name value tablename  
if \[ $# -eq 3 ]  
then  
name=$1  
value=$2  
tablename=$3  
echo name=${name}  
echo value=${value}  
echo tablename=${tablename}  
else  
echo "Usage: $0 username username\_value tablename"  
exit 1  
fi

cd /run/hie/kakadata

#1.select the dialaccount, srcip  
cd /jz/hiveuser/hive.kaka/  
echo "insert overwrite directory '/bcpdata/kaka/ad\_ip\_${name}\_${value}' select distinct if(account&lt;&gt;'',account,0)  
as account, if(account&lt;&gt;'', 0, ip) as ip from ${tablename} where ${name}='${value}'; " | /jz/hive-0.5.0-bin/bin/hive

2、需要注意的是这里用到了hive.kaka目录下的cli，所以如果这个控制端是开启的，那么需要先退出再执行这个脚本。

三、总结

1、采用shell脚本来执行一些查询语句可以简化很多的开发工作

2、可以利用Linux自身的一些工具，实现定时的job任务