千家信息网

Hive -f如何封装支持传参数

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章给大家分享的是有关Hive -f如何封装支持传参数的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。需求}Hive -f}hiveF 封装hive -f aa.sql}
千家信息网最后更新 2025年02月02日Hive -f如何封装支持传参数

这篇文章给大家分享的是有关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 map = null;    public ParseArgs(String[] args){        map = new HashMap();        if(args.length == 0){            return ;        }        int i=0;        while(i 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 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 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/profilecd /opt/beifengbegin=`date -d -1days +%Y-%m-%d`begin="2013-08-08"#hive -f ./hive_test.hqlhiveF3 /usr/hiveF/bin/hive_test.hql -date "$begin"

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

source /etc/profile

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

感谢各位的阅读!关于"Hive -f如何封装支持传参数"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0