Github user BruceXu1991 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20034#discussion_r158577749
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala 
---
    @@ -186,7 +186,7 @@ private[hive] class HiveClientImpl(
       /** Returns the configuration for the current session. */
       def conf: HiveConf = state.getConf
     
    -  private val userName = state.getAuthenticator.getUserName
    +  private val userName = conf.getUser
    --- End diff --
    
    well, if using spark 2.2.1's current implementation
    ```
    private val userName = state.getAuthenticator.getUserName
    ```
    when the implementation of state.getAuthenticator is 
**HadoopDefaultAuthenticator**, which is default in hive conf, the username is 
got. 
    
    however, in the case that the implementation of state.getAuthenticator is 
**SessionStateUserAuthenticator**, which is used in my case, then username will 
be null.
    
    the simplified code below explains the reason:
    1) HadoopDefaultAuthenticator
    ```
    public class HadoopDefaultAuthenticator implements 
HiveAuthenticationProvider {
    @Override
      public String getUserName() {
        return userName;
      }
    
      @Override
      public void setConf(Configuration conf) {
        this.conf = conf;
        UserGroupInformation ugi = null;
        try {
          ugi = Utils.getUGI();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        this.userName = ugi.getShortUserName();
        if (ugi.getGroupNames() != null) {
          this.groupNames = Arrays.asList(ugi.getGroupNames());
        }
      }
    }
    
    public class Utils {
      public static UserGroupInformation getUGI() throws LoginException, 
IOException {
        String doAs = System.getenv("HADOOP_USER_NAME");
        if(doAs != null && doAs.length() > 0) {
          return UserGroupInformation.createProxyUser(doAs, 
UserGroupInformation.getLoginUser());
        }
        return UserGroupInformation.getCurrentUser();
      }
    }
    ```
    it shows that HadoopDefaultAuthenticator will get username through 
Utils.getUGI(), so the username is HADOOP_USER_NAME of LoginUser.
    
    2)  SessionStateUserAuthenticator
    ```
    public class SessionStateUserAuthenticator implements 
HiveAuthenticationProvider {
      @Override
      public void setConf(Configuration arg0) {
      }
    
      @Override
      public String getUserName() {
        return sessionState.getUserName();
      }
    }
    ```
    it shows that SessionStateUserAuthenticator get the username through 
sessionState.getUserName(), which is null. Here is the [instantiation of 
SessionState in 
HiveClientImpl](https://github.com/apache/spark/blob/1cf3e3a26961d306eb17b7629d8742a4df45f339/sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala#L187)
 


---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to