简体   繁体   English

使用Java SDK从AWS EMR集群获取主公共DNS值

[英]Obtain Master public DNS value from AWS EMR Cluster using the Java SDK

I need to obtain the master public DNS value via the Java SDK. 我需要通过Java SDK获取主公共DNS值。 The only information that I'll have at the start of the application is the ClusterName which is static. 在应用程序开始时,我将只有的信息是ClusterName,它是静态的。

Thus far I've been able to pull out all the other information that I need excluding this and this, unfortunately is vital for the application to be a success. 到目前为止,我已经能够提取除此以外的所有其他我需要的信息,不幸的是,对于应用程序的成功至关重要。

This is the code that I'm currently working with: 这是我目前正在使用的代码:

List<ClusterSummary> summaries = clusters.getClusters();
        for (ClusterSummary cs: summaries) {
            if (cs.getName().equals("test") && WHITELIST.contains(cs.getStatus().getState())) {
                ListInstancesResult instances = emr.listInstances(new ListInstancesRequest().withClusterId(cs.getId()));
                clusterHostName = instances.getInstances().get(0).toString();
                jobFlowId = cs.getId();
            }
        }

I've removed the get for PublicIpAddress as wanted the full toString for testing. 我已经删除了PublicIpAddress的get,因为想要完整的toString进行测试。 I should be clear in that this method does give me the DNS that I need but I have no way of differentiating between them. 我应该明确一点,这种方法确实为我提供了所需的DNS,但我无法区分它们。

If my EMR has 4 machines, I don't know which position in the list that Instance will be. 如果我的EMR有4台计算机,则我不知道实例将在列表中的哪个位置。 For my basic trial I've only got two machines, 1 master and a worker. 对于我的基本试用,我只有两台机器,一台主机和一台工人。 .get(0) has returned both the values for master and the worker on successive runs. .get(0)在连续运行中已返回master和worker的值。

The information that I'm able to obtain from these is below - my only option that I can see at the moment is to use the 'ReadyDateTime' as an identifier as the master 'should' always be ready first, but this feels hacky and I was hoping on a cleaner solution. 我可以从这些信息中获取以下信息-我目前唯一看到的选择是使用“ ReadyDateTime”作为标识符,因为主机“应该”总是先准备好了,但这感觉很棘手,我希望有一个更清洁的解决方案。

    {Id: id,
Ec2InstanceId: id,
PublicDnsName: ec2-54--143.compute-1.amazonaws.com,
PublicIpAddress: 54..143,
PrivateDnsName: ip-10--158.ec2.internal,
PrivateIpAddress: 10..158,
Status: {State: RUNNING,StateChangeReason: {},
Timeline: {CreationDateTime: Tue Feb 21 09:18:08 GMT 2017,
ReadyDateTime: Tue Feb 21 09:25:11 GMT 2017,}},
InstanceGroupId: id,
EbsVolumes: []}

{Id: id,
Ec2InstanceId: id,
PublicDnsName: ec2-54--33.compute-1.amazonaws.com,
PublicIpAddress: 54..33,
PrivateDnsName: ip-10--95.ec2.internal,
PrivateIpAddress: 10..95,
Status: {State: RUNNING,StateChangeReason: {},
Timeline: {CreationDateTime: Tue Feb 21 09:18:08 GMT 2017,
ReadyDateTime: Tue Feb 21 09:22:48 GMT 2017,}},
InstanceGroupId: id
EbsVolumes: []}

Don't use ListInstances. 不要使用ListInstances。 Instead, use DescribeCluster, which returns as one of the fields MasterPublicDnsName. 而是使用DescribeCluster,它作为MasterPublicDnsName字段之一返回。

To expand on what was mentioned by Jonathon: 为了扩展乔纳森提到的内容:

        AmazonEC2Client ec2 = new AmazonEC2Client(cred);
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances(new DescribeInstancesRequest().withInstanceIds(clusterInstanceIds));
    List<Reservation> reservations = describeInstancesResult.getReservations();
    for (Reservation res : reservations) {
        for (GroupIdentifier group : res.getGroups()) {
            if (group.getGroupName().equals("ElasticMapReduce-master")) { // yaaaaaaaaah, Wahay!
                masterDNS = res.getInstances().get(0).getPublicDnsName();
            }
        }
    }
 AWSCredentials credentials_profile = null;
 credentials_profile = new 
 DefaultAWSCredentialsProviderChain().getCredentials();

 AmazonElasticMapReduceClient emr = new 
 AmazonElasticMapReduceClient(credentials_profile);
    Region euWest1 = Region.getRegion(Regions.US_EAST_1);
    emr.setRegion(euWest1);
 DescribeClusterFunction fun = new DescribeClusterFunction(emr);
    DescribeClusterResult res = fun.apply(new 
 DescribeClusterRequest().withClusterId(clusterId));
    String publicDNSName =res.getCluster().getMasterPublicDnsName();

Below is the working code to get the public DNS name. 以下是获取公共DNS名称的工作代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM