您的位置:首页 > 其它

hive组件二次开发之HiveF

2016-01-15 14:24 351 查看
其实这个HiveF组件很简单。用Java代码写一个拼接字符串的东西,让他可以传值。之后以hive -e "$sql" 执行该sql

下面我们来介绍main的主要代码

package qh.zcy.hiveF;

import java.io.File;

/**
*
* author:zl
* action:完成对hive -f的封装
* time:下午5:06:53
*/
public class Main {

/**
*  ../*.sql  -date "2013-01-01"
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ParseArgs parse = new ParseArgs(args);
String sql = Utils.getSql(new File(args[0])) ;
System.out.println(Utils.parse(sql,parse.getMap()));
}
}


ParseArgs代码如下

package qh.zcy.hiveF;

import java.util.HashMap;
import java.util.Map;

/**
*
* author:zl
* action:处理main函数参数
* time:下午5:23:55
* ../*.sql  -date "2013-01-01"
*/
public class ParseArgs {

private  Map<String, String> map=null;

public ParseArgs(String [] args) {
map=new HashMap<String, String>();
if(args.length==0){
return ;

}
int i = 0 ;
while ( i < args.length){
String temp = args[i].trim();

if(temp.startsWith("-")){
String key =temp.substring(1).trim();
i++;
String value=null;

if(args.length>i){
value=args[i].trim();

if(value.startsWith("\"") || value.endsWith("\'")){
value=value.substring(1,value.length()-1);
}
}
map.put(key, value);
i++;
}else{
i++;
}

}
}

public Map<String, String> getMap() {
return map;
}

}
utils类主要代码

package qh.zcy.hiveF;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Map;

public class Utils {

public static final String BEGIN="{$" ;
public static final String END="}" ;

public static String getSql(File file) throws Exception{
BufferedReader bf = new BufferedReader(new FileReader(file)) ;
StringBuffer sqlBuffer = new StringBuffer();
String temp = null;
while((temp=bf.readLine())!=null){
String tmp = temp.trim();
if (tmp.length()==0 || tmp.startsWith("#") || tmp.startsWith("--")) {
continue ;
}
sqlBuffer.append(tmp+ " ") ;
}
bf.close();
return sqlBuffer.toString();

}
/**
* 把sql里的参数引用,替换为map里的value
* @param sql
* @param map
*/
public static String parse(String sql, Map<String, String> map)
{
int begin = sql.indexOf(BEGIN) ;
while(begin != -1)
{
String suffix = sql.substring(begin + BEGIN.length());
int end = begin + BEGIN.length() + suffix.indexOf(END) ;
String key = sql.substring(begin+BEGIN.length(), end).trim() ;
if (map != null && map.get(key) != null) {
sql = sql.substring(0, begin) + map.get(key) + sql.substring(end + 1, sql.length()) ;
}
else
{
throw new RuntimeException("Invalid Expression.....");
}
begin = sql.indexOf(BEGIN) ;
}
return sql ;
}
}


将代码用fat jar打包,上传至服务器

在服务器上创建如下目录,其中lib存放jar包,bin上存放命令语句文件



bin下创建文件HiveF,内容如下:

. /etc/profile
sql=`java -jar /data/myhiveF/lib/MyHiveF.jar $*`
echo "$sql"
hive -e "$sql"


在/etc/profile下注册HiveF命令

随便找个目录,下创建hive的hql,hiveFTest.hql测试内容如下

drop table test_zhang;
create table test_zhang as
select * from concat_test where dt="{$dt}";


测试shell文件如下

#!/bin/sh
. /etc/profile
dt="2015-01-12"
hiveF ./hiveFTest.hql -dt "$dt"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: