code: public static void getHadoopTokens(final State state, Optional<File> tokenFile, Credentials cred)
        throws IOException, InterruptedException {
  
      Preconditions.checkArgument(state.contains(KEYTAB_USER), "Missing required property " + KEYTAB_USER);
      Preconditions.checkArgument(state.contains(KEYTAB_LOCATION), "Missing required property " + KEYTAB_LOCATION);
  
      Configuration configuration = new Configuration();
      configuration.set(HADOOP_SECURITY_AUTHENTICATION, KERBEROS);
      UserGroupInformation.setConfiguration(configuration);
      UserGroupInformation.loginUserFromKeytab(obtainKerberosPrincipal(state), state.getProp(KEYTAB_LOCATION));
  
      final Optional<String> userToProxy = Strings.isNullOrEmpty(state.getProp(USER_TO_PROXY)) ? Optional.<String>absent()
          : Optional.fromNullable(state.getProp(USER_TO_PROXY));
      final Configuration conf = new Configuration();
  
      LOG.info("Getting tokens for " + userToProxy);
  
      getJhToken(conf, cred);
      getFsAndJtTokens(state, conf, userToProxy, cred);
  
      if (tokenFile.isPresent()) {
        persistTokens(cred, tokenFile.get());
      }
    }
docs: Get Hadoop tokens (tokens for job history server, job tracker and HDFS) using Kerberos keytab.
  
  @param state A {@link State} object that should contain property {@link #USER_TO_PROXY},
  {@link #KEYTAB_USER} and {@link #KEYTAB_LOCATION}. To obtain tokens for
  other namenodes, use property {@link #OTHER_NAMENODES} with comma separated HDFS URIs.
  @param tokenFile If present, the file will store materialized credentials.
  @param cred A im-memory representation of credentials.