Search in sources :

Example 1 with HCatClient

use of org.apache.hive.hcatalog.api.HCatClient in project oozie by apache.

the class HCatCredentialHelper method getHCatClient.

/**
 * Getting the HCat client.
 * @param launcherConfig
 * @param principal
 * @param server
 * @return HCatClient
 * @throws HCatException
 */
public HCatClient getHCatClient(Configuration launcherConfig, String principal, String server) throws HCatException {
    HiveConf hiveConf = null;
    HCatClient hiveclient = null;
    hiveConf = new HiveConf();
    XLog.getLog(getClass()).debug("getHCatClient: Principal: {0} Server: {1}", principal, server);
    // specified a thrift url
    hiveConf.set(HIVE_METASTORE_SASL_ENABLED, "true");
    hiveConf.set(HIVE_METASTORE_KERBEROS_PRINCIPAL, principal);
    hiveConf.set(HIVE_METASTORE_LOCAL, "false");
    hiveConf.set(HiveConf.ConfVars.METASTOREURIS.varname, server);
    String protection = launcherConfig.get(HADOOP_RPC_PROTECTION, SaslRpcServer.QualityOfProtection.AUTHENTICATION.name().toLowerCase());
    XLog.getLog(getClass()).debug("getHCatClient, setting rpc protection to {0}", protection);
    hiveConf.set(HADOOP_RPC_PROTECTION, protection);
    hiveclient = HCatClient.create(hiveConf);
    return hiveclient;
}
Also used : HCatClient(org.apache.hive.hcatalog.api.HCatClient) HiveConf(org.apache.hadoop.hive.conf.HiveConf)

Example 2 with HCatClient

use of org.apache.hive.hcatalog.api.HCatClient in project oozie by apache.

the class HCatURIHandler method registerForNotification.

@Override
public void registerForNotification(URI uri, Configuration conf, String user, String actionID) throws URIHandlerException {
    HCatURI hcatURI;
    try {
        hcatURI = new HCatURI(uri);
    } catch (URISyntaxException e) {
        throw new URIHandlerException(ErrorCode.E0906, uri, e);
    }
    HCatAccessorService hcatService = Services.get().get(HCatAccessorService.class);
    if (!hcatService.isRegisteredForNotification(hcatURI)) {
        HCatClient client = getHCatClient(uri, conf);
        try {
            String topic = client.getMessageBusTopicName(hcatURI.getDb(), hcatURI.getTable());
            if (topic == null) {
                return;
            }
            hcatService.registerForNotification(hcatURI, topic, new HCatMessageHandler(uri.getAuthority()));
        } catch (HCatException e) {
            throw new HCatAccessorException(ErrorCode.E1501, e);
        } finally {
            closeQuietly(client, null, true);
        }
    }
    PartitionDependencyManagerService pdmService = Services.get().get(PartitionDependencyManagerService.class);
    pdmService.addMissingDependency(hcatURI, actionID);
}
Also used : HCatAccessorService(org.apache.oozie.service.HCatAccessorService) HCatClient(org.apache.hive.hcatalog.api.HCatClient) HCatMessageHandler(org.apache.oozie.dependency.hcat.HCatMessageHandler) HCatException(org.apache.hive.hcatalog.common.HCatException) URISyntaxException(java.net.URISyntaxException) HCatURI(org.apache.oozie.util.HCatURI) HCatAccessorException(org.apache.oozie.service.HCatAccessorException) PartitionDependencyManagerService(org.apache.oozie.service.PartitionDependencyManagerService)

Example 3 with HCatClient

use of org.apache.hive.hcatalog.api.HCatClient in project oozie by apache.

the class HCatURIHandler method getHCatClient.

private HCatClientWithToken getHCatClient(URI uri, Configuration conf, String user) throws HCatAccessorException {
    final HiveConf hiveConf = getHiveConf(uri, conf);
    String delegationToken = null;
    try {
        // Get UGI to doAs() as the specified user
        UserGroupInformation ugi = UserGroupInformation.createProxyUser(user, UserGroupInformation.getLoginUser());
        // Define the label for the Delegation Token for the HCat instance.
        hiveConf.set("hive.metastore.token.signature", "HCatTokenSignature");
        if (hiveConf.getBoolean(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL.varname, false)) {
            HCatClient tokenClient = null;
            try {
                // Retrieve Delegation token for HCatalog
                tokenClient = HCatClient.create(hiveConf);
                delegationToken = tokenClient.getDelegationToken(user, UserGroupInformation.getLoginUser().getUserName());
                // Store Delegation token in the UGI
                Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>();
                token.decodeFromUrlString(delegationToken);
                token.setService(new Text(hiveConf.get("hive.metastore.token.signature")));
                ugi.addToken(token);
            } finally {
                if (tokenClient != null) {
                    tokenClient.close();
                }
            }
        }
        XLog.getLog(HCatURIHandler.class).info("Creating HCatClient for user [{0}] login_user [{1}] and server [{2}] ", user, UserGroupInformation.getLoginUser(), hiveConf.get(HiveConf.ConfVars.METASTOREURIS.varname));
        HCatClient hcatClient = ugi.doAs(new PrivilegedExceptionAction<HCatClient>() {

            @Override
            public HCatClient run() throws Exception {
                HCatClient client = HCatClient.create(hiveConf);
                return client;
            }
        });
        HCatClientWithToken clientWithToken = new HCatClientWithToken(hcatClient, delegationToken);
        return clientWithToken;
    } catch (IOException e) {
        throw new HCatAccessorException(ErrorCode.E1501, e.getMessage());
    } catch (Exception e) {
        throw new HCatAccessorException(ErrorCode.E1501, e.getMessage());
    }
}
Also used : DelegationTokenIdentifier(org.apache.hadoop.hive.thrift.DelegationTokenIdentifier) Token(org.apache.hadoop.security.token.Token) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) HCatAccessorException(org.apache.oozie.service.HCatAccessorException) HCatException(org.apache.hive.hcatalog.common.HCatException) URISyntaxException(java.net.URISyntaxException) HCatAccessorException(org.apache.oozie.service.HCatAccessorException) ConnectionFailureException(org.apache.hive.hcatalog.api.ConnectionFailureException) IOException(java.io.IOException) HCatClient(org.apache.hive.hcatalog.api.HCatClient) HiveConf(org.apache.hadoop.hive.conf.HiveConf) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 4 with HCatClient

use of org.apache.hive.hcatalog.api.HCatClient in project oozie by apache.

the class HCatLauncherURIHandler method delete.

@Override
public boolean delete(URI uri, Configuration conf) throws LauncherException {
    HCatClient client = getHCatClient(uri, conf);
    try {
        HCatURI hcatURI = new HCatURI(uri.toString());
        if (!hcatURI.getPartitionMap().isEmpty()) {
            client.dropPartitions(hcatURI.getDb(), hcatURI.getTable(), hcatURI.getPartitionMap(), true);
        } else {
            client.dropTable(hcatURI.getDb(), hcatURI.getTable(), true);
        }
        System.out.println("Dropped partitions for " + uri);
        return true;
    } catch (ConnectionFailureException e) {
        throw new LauncherException("Error trying to drop " + uri, e);
    } catch (HCatException e) {
        throw new LauncherException("Error trying to drop " + uri, e);
    } catch (URISyntaxException e) {
        throw new LauncherException("Error trying to drop " + uri, e);
    } finally {
        closeQuietly(client);
    }
}
Also used : HCatClient(org.apache.hive.hcatalog.api.HCatClient) ConnectionFailureException(org.apache.hive.hcatalog.api.ConnectionFailureException) HCatException(org.apache.hive.hcatalog.common.HCatException) URISyntaxException(java.net.URISyntaxException) HCatURI(org.apache.oozie.util.HCatURI)

Example 5 with HCatClient

use of org.apache.hive.hcatalog.api.HCatClient in project oozie by apache.

the class HCatCredentialHelper method set.

/**
 * This Function will set the HCat token to the credentials
 * @param credentials - the credentials
 * @param launcherConfig - launcher configuration
 * @param principal - principal for HCat server
 * @param server - Serevr URI for HCat server
 * @throws Exception
 */
public void set(Credentials credentials, Configuration launcherConfig, String principal, String server) throws Exception {
    HCatClient client = null;
    try {
        client = getHCatClient(launcherConfig, principal, server);
        XLog.getLog(getClass()).debug("HCatCredentialHelper: set: User name for which token will be asked from HCat: {0}", launcherConfig.get(USER_NAME));
        String tokenStrForm = client.getDelegationToken(launcherConfig.get(USER_NAME), UserGroupInformation.getLoginUser().getShortUserName());
        Token<DelegationTokenIdentifier> hcatToken = new Token<DelegationTokenIdentifier>();
        hcatToken.decodeFromUrlString(tokenStrForm);
        credentials.addToken(CredentialsProviderFactory.getUniqueAlias(hcatToken), hcatToken);
        XLog.getLog(getClass()).debug("Added the HCat token to launcher's credentials");
    } catch (Exception ex) {
        XLog.getLog(getClass()).debug("set Exception {0}", ex.getMessage());
        throw ex;
    } finally {
        if (client != null) {
            client.close();
        }
    }
}
Also used : HCatClient(org.apache.hive.hcatalog.api.HCatClient) DelegationTokenIdentifier(org.apache.hadoop.mapreduce.security.token.delegation.DelegationTokenIdentifier) Token(org.apache.hadoop.security.token.Token) HCatException(org.apache.hive.hcatalog.common.HCatException)

Aggregations

HCatClient (org.apache.hive.hcatalog.api.HCatClient)9 HCatException (org.apache.hive.hcatalog.common.HCatException)6 URISyntaxException (java.net.URISyntaxException)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)3 Token (org.apache.hadoop.security.token.Token)3 HCatAccessorException (org.apache.oozie.service.HCatAccessorException)3 HCatURI (org.apache.oozie.util.HCatURI)3 IOException (java.io.IOException)2 DelegationTokenIdentifier (org.apache.hadoop.mapreduce.security.token.delegation.DelegationTokenIdentifier)2 ConnectionFailureException (org.apache.hive.hcatalog.api.ConnectionFailureException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DelegationTokenIdentifier (org.apache.hadoop.hive.thrift.DelegationTokenIdentifier)1 Text (org.apache.hadoop.io.Text)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1 HCatMessageHandler (org.apache.oozie.dependency.hcat.HCatMessageHandler)1 HCatAccessorService (org.apache.oozie.service.HCatAccessorService)1 PartitionDependencyManagerService (org.apache.oozie.service.PartitionDependencyManagerService)1