java实现判断集群中的主机是否在线

代码如下:

package uestc.cn;

import java.io.IOException;
import java.net.InetAddress;

import java.net.UnknownHostException;

public class test {
    String ip=null;
    static String host_ip=null;
    
    //获取连通主机的IP地址
    public String getIp() throws UnknownHostException,IOException{    
        String ipAddress[]={"192.168.1.111","192.168.1.103","192.168.1.112"};
       for(int i=0;i<ipAddress.length;i++){
            InetAddress inet = InetAddress.getByName(ipAddress[i]);
            System.out.println("sending Ping Request to "+inet);
            //System.out.println(inet.isReachable(100)?"Host is reachable":"Host is Not reachable");
            if(inet.isReachable(100)){
                String ip = ipAddress[i];
                return ip;
                
             }
        }
        return ip;
    }

    
    public static void main(String[] args) {
        
         test a = new test();
         try {
            host_ip =a.getIp();
            System.out.println(host_ip);
        } catch (Exception e) {
            System.out.println("fail");
        }
        }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
登录 CDP 集群需要使用 Kerberos 认证,可以使用 Java 的 Kerberos 相关类来实现。增删 Yarn 资源池队列可以使用 Cloudera Manager API 或者 Yarn REST API 来实现。 以下是一个简单的示例,实现登录 CDP 集群并添加 Yarn 资源池队列: ``` import java.io.File; import java.util.Map; import javax.security.auth.Subject; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import org.apache.hadoop.security.UserGroupInformation; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.FileEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class CdpYarnQueueExample { public static void main(String[] args) { String cdpPrincipal = "cdp-user@EXAMPLE.COM"; String cdpKeytab = "/path/to/cdp-user.keytab"; String cdpKrb5 = "/etc/krb5.conf"; String cmHost = "cm.host"; String cmUsername = "admin"; String cmPassword = "admin"; String yarnQueueName = "my_queue"; try { // Login to CDP using Kerberos authentication System.setProperty("java.security.krb5.conf", cdpKrb5); LoginContext lc = new LoginContext("CDPLogin", new CDPLoginCallbackHandler(cdpPrincipal, cdpKeytab)); lc.login(); Subject subject = lc.getSubject(); UserGroupInformation.setLoginUser(UserGroupInformation.getUGIFromSubject(subject)); // Create Yarn resource pool queue using Cloudera Manager API String cmApiUrl = "http://" + cmHost + ":7180/api/v19/clusters/my_cluster/services/yarn/yarnService"; String cmApiJson = "{\"yarnQueueConfigs\": [{\"name\": \"" + yarnQueueName + "\", \"capacity\": 10, \"maximumCapacity\": 20}]}"; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(cmApiUrl); httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON.getMimeType()); httpPost.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((cmUsername + ":" + cmPassword).getBytes())); httpPost.setEntity(new FileEntity(new File(cmApiJson), ContentType.APPLICATION_JSON)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); System.out.println(result); } EntityUtils.consume(entity); response.close(); httpClient.close(); } catch (LoginException | IOException e) { e.printStackTrace(); } } } ``` 在上面的示例,`CDPLoginCallbackHandler` 是一个自定义的回调处理器,用于加载 Kerberos keytab 并进行认证。`CDPLogin` 是一个自定义的 LoginModule,用于处理 Kerberos 认证流程。 注意,在使用 Cloudera Manager API 或者 Yarn REST API 时需要提供相应的认证信息。在示例,使用了 Basic 认证方式,用户名和密码通过 Base64 编码后放在请求头。具体实现可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值