hive执行流程------CommandProcessor

本文介绍了Hive的CommandProcessor在CliDriver中的作用,它根据不同的命令选择相应的实现类,如set、reset等由特定类处理,而select、insert等由Driver类处理。CommandProcessorFactory负责创建具体实现类,如HiveCommand枚举定义命令,AddResourceProcessor处理资源添加,DeleteResourceProcessor处理资源删除,DfsProcessor处理dfs命令,SetProcessor处理设置命令等。
摘要由CSDN通过智能技术生成

CliDriver 类中介绍了CliDriver 类会引用到CommandProcessor相关类,主要是根据命令来判断具体实现类,比如通过本地的hive cli启动时,运行hive的命令(非list/source/shell命令等)时在processCmd方法中有如下实现: 

1
2
3
4
5
6
7
8
try  {
         CommandProcessor proc = CommandProcessorFactory.get(tokens, (HiveConf) conf); // 根据命令判断具体的CommandProcessor 实现类
         ret = processLocalCmd(cmd, proc, ss);
       catch  (SQLException e) {
         console.printError(  "Failed processing command "  + tokens[ 0 ] +  " "  + e.getLocalizedMessage(),
           org.apache.hadoop.util.StringUtils.stringifyException(e));
         ret =  1 ;
       }

具体的决定什么样的命令对应什么样的具体实现类由 CommandProcessorFactory 规定:如果是set,reset,dfs,add delete,compile等命令,返回对应的CommandProcessor实现类。其余有效命令比如select,insert 都是返回Driver类。

CommandProcessor相关类在org.apache.hadoop.hive.ql.processors包中,类的具体的uml图如下:

wKioL1RHw_LzbuQHAATW62gKz60796.jpg

简单看下几个类的实现:

1.HiveCommand类,是一个迭代类,定义了非sql的一些语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public  enum  HiveCommand {
   SET(),
   RESET(),
   DFS(),
   ADD(),
   DELETE(),
   COMPILE();
   private  static  final  Set<String> COMMANDS =  new  HashSet<String>();
   static  {
     for  (HiveCommand command : HiveCommand. values()) {
       COMMANDS.add(command.name());
     }
   }
   public  static  HiveCommand find(String[] command) {
     if  ( null  == command){
       return  null ;
     }
     String cmd = command[ 0 ];
     if  (cmd !=  null ) {
       cmd = cmd.trim().toUpperCase();
       if  (command. length >  1  &&  "role" .equalsIgnoreCase(command[ 1 ])) {
         // special handling for set role r1 statement
         return  null  ;
       else  if  (COMMANDS .contains(cmd)) {
         return  HiveCommand. valueOf(cmd);
       }
     }
     return  null ;
   }
}

2.CommandProcessorFactory 类,主要用于获取具体的命令实现类

主要定义了get和getForHiveCommand方法

方法调用get----->getForHiveCommand,其中getForHiveCommand会调HiveCommand 类,HiveCommand 类是一个枚举类型,定义了一些命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
getForHiveCommand方法中:
   public  static  CommandProcessor getForHiveCommand(String[] cmd, HiveConf conf)
       throws  SQLException {
     HiveCommand hiveCommand = HiveCommand.find(cmd);   // sql语句返回值为null
     if  (hiveCommand ==  null  || isBlank(cmd[ 0 ])) {
       return  null ;
     }
     if  (conf ==  null ) {
       conf =  new  HiveConf();
     }
     Set<String> availableCommands =  new  HashSet<String>();
     for  (String availableCommand : conf.getVar(HiveConf.ConfVars.HIVE_SECURITY_COMMAND_WHITELIST).split(  "," )) {
       availableCommands.add(availableCommand.toLowerCase().trim());
     }
     if  (!availableCommands.contains(cmd[ 0 ].trim().toLowerCase())) {
       throw  new  SQLException( "Insufficient privileges to execute "  + cmd[ 0 ],  "42000" );
     }
     switch  (hiveCommand) {   // 每种语句对应的具体的processor类
       case  SET:
         return  new  SetProcessor();
       case  RESET:
         return  new  ResetProcessor();
       case  DFS:
         SessionState ss = SessionState.get();
         return  new  DfsProcessor(ss.getConf());
       case  ADD:
         return  new  AddResourceProcessor();
       case  DELETE:
         return  new  DeleteResourceProcessor();
       case  COMPILE:
         return  new  CompileProcessor();
       default :
         throw  new  AssertionError( "Unknown HiveCommand "  + hiveCommand);
     }
   }
get方法:
   public  static  CommandProcessor get(String[] cmd, HiveConf conf)
       throws  SQLException {
     CommandProcessor result = getForHiveCommand(cmd, conf);
     if  (result !=  null ) {
       return  result;   // 如果result不为空,即命令在HiveCommand 的迭代器中定义的话,直接返回对应的结果
     }
     if  (isBlank(cmd[ 0 ])) {
       return  null ;
     else  {   // 为空的话返回Driver类的实例
       if  (conf ==  null ) {
         return  new  Driver();    
       }
       Driver drv = mapDrivers.get(conf);
       if  (drv ==  null ) {
         drv =  new  Driver();
         mapDrivers.put(conf, drv);
       }
       drv.init();
       return  drv;
     }
   }

3.CommandProcessorResponse类封装了processor的返回信息,比如返回码,错误信息等。

4.CommandProcessor 类是一个接口,具体的实现类由下面几个:

1
AddResourceProcessor/CompileProcessor/DeleteResourceProcessor/DfsProcessor/ResetProcessor/SetProcessor/Driver

主要实现方法在各个实现类的run方法中,run方法返回一个CommandProcessorResponse的对象。

下面简单的说下常用的几个实现类:

a.AddResourceProcessor类是处理add xxx命令的。

主要有两个步骤:

1)判断命令的合法性(长度,类型是否在FILE,JAR,ARCHIVE 3种之内)

2)调用SessionState的add_resource方法(

1
2
SessionState.add_resource方法---->调用SessionState.downloadResource--->
调用FileSystem的copyToLocalFile方法,把文件下载到本地

)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  public  CommandProcessorResponse run(String command) {
     SessionState ss = SessionState.get();
     command =  new  VariableSubstitution().substitute(ss.getConf(),command);
     String[] tokens = command.split(  "\\s+" );
     SessionState.ResourceType t;
     if  (tokens. length <  2
         || (t = SessionState.find_resource_type(tokens[ 0 ])) ==  null ) {
       console.printError(  "Usage: add ["
           + StringUtils.join(SessionState .ResourceType.values(),  "|"  )
           "] <value> [<value>]*"  );
       return  new  CommandProcessorResponse( 1 );
     }
     for  int  i =  1 ; i < tokens.length ; i++) {
       String resourceFile = ss.add_resource(t, tokens[i]);
       if (resourceFile ==  null ){
         String errMsg = tokens[i]+  " does not exist." ;
         return  new  CommandProcessorResponse( 1 ,errMsg, null );
       }
     }
     return  new  CommandProcessorResponse( 0 );
   }

b.相反的DeleteResourceProcessor是用来处理delete  xxx命令的。

最终调用了SessionState的delete_resource方法,把resource从HashMap中去掉。

1
2
3
4
5
6
7
8
9
10
11
12
SessionState的delete_resource方法
   public  boolean  delete_resource(ResourceType t, String value) {
     if  (resource_map.get(t) ==  null ) {
       return  false ;
     }
     if  (t. hook !=  null ) {
       if  (!t. hook.postHook( resource_map.get(t), value)) {
         return  false ;
       }
     }
     return  ( resource_map.get(t).remove(value));
   }

c.DfsProcessor类用来处理dfs 命令,即已“!dfs”开头的命令,最终调用了FsShell的run方法

d.SetProcessor类用来处理set xxx等命令,可以用来设置参数,变量等。

设置参数时

1)以system: 开头的调用了 System.getProperties().setProperty方法。

比如

1
2
3
hive> set system:user.name=xxxx;
hive> set system:user.name;    
system:user.name=xxxx

2)以hiveconf:开头:

调用了HiveConf的verifyAndSet方法

3)以hivevar:开头:

ss.getHiveVariables().put方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive-JDBC Uber Jar 是一个包含了所有依赖项的单一 JAR 文件,使得使用 Hive JDBC 连接数据库更加方便。为了下载 hive-jdbc-uber-jar,您可以按照以下步骤进行操作: 1. 打开您的网络浏览器并访问 Apache Hive 的官方网站(https://hive.apache.org/)。 2. 在页面的顶部菜单栏中,您会找到一个"Downloads"(下载)的选项。单击这个选项。 3. 在下载页面上,您可以看到不同的 Hive 版本和相关的下载链接。根据您的需求选择适合的版本。一般建议选择最新版本。 4. 找到并单击下载链接,以启动 hive-jdbc-uber-jar 文件的下载。可以选择一个合适的下载镜像,点击相关链接即可开始下载。 5. 下载完成后,您可以在您指定的下载文件夹中找到 hive-jdbc-uber-jar 文件。可以通过文件管理器打开文件夹并查看文件。 在您下载了 hive-jdbc-uber-jar 文件后,您可以将其添加到您的项目中,并使用 Hive 的 JDBC API 连接到 Hive 数据库。您可以在项目的构建路径中添加该 JAR 文件,并在代码中引入相关的类和方法。确保在代码中正确配置 JDBC 连接参数,如 Hive 服务器的 URL、用户名和密码等。 总结起来,您可以通过在 Apache Hive 官方网站下载页面上选择适当的 Hive 版本并点击相关的下载链接,从中直接下载 hive-jdbc-uber-jar 文件。这个 JAR 文件是使用 Hive JDBC 连接到 Hive 数据库时所需的所有依赖项的集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值