Hive -f 封装支持传参数

需求

}Hive    -f 

}hiveF  封装hive –f    aa.sql

}支持传任意多个参数,实现shell脚本和sql文件的分离

}Java  类名  *.sql  -date  “2013-01-01”  ….

分析

对一下sql中的分区字段${date} 进行hiveF的封装,让date字段传值和sql文件分离

create table tmp_test1 as 
select session_id,
       url,
       getcmsid(url)
 from page_views where dt='{$date}' and url like '%topicId%';

java代码如下

main.java

package hive.hiveF;
import java.io.File;
public class Main {
    /**
     * 
     * @param ../*.sql -date "2013-01-01"
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
//        args = new String[5];
//        args[0] ="c:/test.sql";
//        args[1] = "-date";
//        args[2] = "2013-01-01";
//        args[3] = "-date1";vi hi
//        args[4] = "2013-11-11";
        
        
        ParseArgs parse = new ParseArgs(args);
        String sql = Utils.getSql(new File(args[0]));
        System.out.println(Utils.parse(sql, parse.getMap()));
//        System.out.println(parse.getMap().get("date"));
//       System.out.println("select * from t1 limit 2");    
    }
}

ParesArgs.java(将shell脚本中的-date "$date" 中的date值分离出来,以便传到sql文件中执行,见hive_test.sh,hive_test.hql)

   

package hive.hiveF;

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

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 par = args[i].trim();
            if(par.startsWith("-")){
                String key = par.substring(1).trim();//key是date value是下一个参数。i++
                i++;
                String value = null;
                if(args.length > i){
                    value = args[i].trim();
                    if(value.startsWith("\"") || value.startsWith("\'")){
                        value = value.substring(1,value.length()-1).trim();
                    }
                }
                
                map.put(key, value);
                i++;
            }else{
                i++;    
            }
            
        }
        
    }
    
    public Map<String, String> getMap() {
        return map;
    }
    
}


Utils.java

   

package hive.hiveF;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
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()); //data}
            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("Incalid Expression...");
            }
            begin = sql.indexOf(BEGIN);
            
        }
        return sql ;
    }

}

   

准备

1、在hive中创建表page_views

create table page_views (
             sessionid string,
             url string  ) partitioed by (dt string) 
             row format delimited fields terminated by '\t' ;

2、在root目录下创建文件vi pageviews

1       http://www.baidu.com/topicId=123123

2       http://www.baidu.com/topicId=14325245

3       http://www.baidu.com/topicId=432w32452

4       http://www.baidu.com/topicId=542351

4       http://www.baidu.com/topicId=5423e

5       http://www.baidu.com/topicId=54325342

6       http://www.baidu.com/topicId=2452

3       http://www.baidu.com/topicId=543222


3、加载数据到page_views中

load data local inpath '/root/pageviews' into table page_views partition (dt='2013-08-08');

4、向/usr/hiveF/lib中导入一个做好的udf(getCmsId)参考UDF和UDAF开发

操作

1、在Linux文件中创建目录hiveF/lib、HiveF/bin 分别存放jar包,和shell文件

2、将以上三个java程序打成hiveF3.jar包上传到HiveF/lib文件中

3、进入HiveF/bin目录下创建两个文件hive_test.sh,hive_test.hql,hiveF

vi hiveF

. /etc/profile     
sql=`java -jar /usr/hiveF/lib/hiveF3.jar $*`      
echo "$sql"      
hive -e "$sql"

vi hive_test.hql 

add jar /usr/hiveF/getId.jar; 
create temporary function getcmsid as 'hive.GetCmsID';

drop table tmp_test1;

create table tmp_test1 as 
select sessionid,
       url,
       getcmsid(url)
 from page_views where dt='{$date}' and url like '%topicId%';

vi hive_test.sh

#!/bin/bash

#set -x
. /etc/profile

cd /opt/beifeng
begin=`date -d -1days +%Y-%m-%d`

begin="2013-08-08"


#hive -f ./hive_test.hql

hiveF3 /usr/hiveF/bin/hive_test.hql -date "$begin"

4、在/etc/profile 中将/usr/hiveF/bin添加到Path中保存退出

   source /etc/profile

5、执行shell脚本 ./hive_test.sh 完成后

捕获

转载于:https://my.oschina.net/yangzhiyuan/blog/270227

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值