List of usage examples for org.apache.hadoop.security UserGroupInformation addToken
public boolean addToken(Text alias, Token<? extends TokenIdentifier> token)
From source file:hydrograph.engine.utilities.HiveMetastoreTokenProvider.java
License:Apache License
public static void obtainTokenForHiveMetastore(Configuration conf) throws TException, IOException { conf.addResource(new Path(HiveConfigurationMapping.getHiveConf("path_to_hive_site_xml"))); HiveConf hiveConf = new HiveConf(); hiveConf.addResource(conf);//from w w w. jav a2s . c o m try { UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); HiveMetaStoreClient hiveMetaStoreClient = new HiveMetaStoreClient(hiveConf); if (UserGroupInformation.isSecurityEnabled()) { String metastore_uri = conf.get("hive.metastore.uris"); LOG.trace("Metastore URI:" + metastore_uri); // Check for local metastore if (metastore_uri != null && metastore_uri.length() > 0) { String principal = conf.get("hive.metastore.kerberos.principal"); String username = ugi.getUserName(); if (principal != null && username != null) { LOG.debug("username: " + username); LOG.debug("principal: " + principal); String tokenStr; try { // Get a delegation token from the Metastore. tokenStr = hiveMetaStoreClient.getDelegationToken(username, principal); // LOG.debug("Token String: " + tokenStr); } catch (TException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } // Create the token from the token string. Token<DelegationTokenIdentifier> hmsToken = new Token<DelegationTokenIdentifier>(); hmsToken.decodeFromUrlString(tokenStr); // LOG.debug("Hive Token: " + hmsToken); // Add the token to the credentials. ugi.addToken(new Text("hive.metastore.delegation.token"), hmsToken); LOG.trace("Added hive.metastore.delegation.token to conf."); } else { LOG.debug("Username or principal == NULL"); LOG.debug("username= " + username); LOG.debug("principal= " + principal); throw new IllegalArgumentException("username and/or principal is equal to null!"); } } else { LOG.info("HiveMetaStore configured in local mode"); } } } catch (IOException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } catch (MetaException e) { LOG.error(e.getMessage(), e); throw new RuntimeException(e); } }