How to create an EMR cluster using AWS SDK for Go

huangapple go评论97阅读模式
英文:

How to create an EMR cluster using AWS SDK for Go

问题

我可以帮你翻译这段内容。以下是翻译好的内容:

我想使用AWS SDK for Go创建EMR集群,但在官方文档中找不到相关方法。

Package: emr — AWS SDK for Go

你能帮我提供详细的代码吗?

英文:

I want to create EMR clusters using AWS SDK for Go, but I can't find a way in the official documentation.

Package: emr — AWS SDK for Go

Cound you please help me with a detailed code?

答案1

得分: 5

实际上,针对相同的问题,文档中有一种描述的方法。对我来说,这也不是很直观,因为措辞不同。看起来,“运行作业流”基本上等同于创建一个集群并向其添加步骤。

所以你想要的是在这里找到的RunJobFlow函数:

https://docs.aws.amazon.com/sdk-for-go/api/service/emr/EMR.html#RunJobFlow-instance_method

下面是一个简单的代码示例,创建一个没有步骤的集群(确保你已经配置了正确的凭据):

package main

import (
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/emr"
)

func main() {
    sess := session.New(&aws.Config{Region: aws.String("eu-west-1")})
    svc := emr.New(sess)

    params := &emr.RunJobFlowInput{
        Instances: &emr.JobFlowInstancesConfig{ // Required
            Ec2KeyName:                    aws.String("keyname"),
            HadoopVersion:                 aws.String("2.7.2"),
            InstanceCount:                 aws.Int64(1),
            KeepJobFlowAliveWhenNoSteps: aws.Bool(true),
            MasterInstanceType:          aws.String("m1.medium"),
            Placement: &emr.PlacementType{
                AvailabilityZone: aws.String("eu-west-1a"), // Required
            },
            TerminationProtected:       aws.Bool(true),
        },
        Name:           aws.String("Go Test Cluster"), // Required
        Applications: []*emr.Application{
            { // Required
                Name:    aws.String("Ganglia"),
            },
            { 
                Name: aws.String("Spark"),
            },
            // More values...
        },
        JobFlowRole: aws.String("EMR_EC2_DefaultRole"),
        LogUri:      aws.String("s3://aws-logs-0000000000-eu-west-1/elasticmapreduce/"),
        ReleaseLabel: aws.String("emr-4.6.0"),
        ServiceRole:  aws.String("EMR_DefaultRole"),
        VisibleToAllUsers: aws.Bool(true),
    }
    resp, err := svc.RunJobFlow(params)

    if err != nil {
        // Print the error, cast err to awserr.Error to get the Code and
        // Message from an error.
        fmt.Println(err.Error())
        return
    }

    // Pretty-print the response data.
    fmt.Println(resp)
}

希望对你有所帮助!

英文:

Actually, coming up to the same problem, there is a way which is described in the documentation. For me, it was not straightforward as well because the wording is different. It appears that a "running a job flow" is basically what equals creating a cluster and adding steps to it.

So what you want is the function RunJobFlow found here:

https://docs.aws.amazon.com/sdk-for-go/api/service/emr/EMR.html#RunJobFlow-instance_method

So, a simple code example which creates a cluster without steps is the following (make sure you have correct credentials configured):

package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/emr"
)
func main() {
sess := session.New(&aws.Config{Region: aws.String("eu-west-1")})
svc := emr.New(sess)
params := &emr.RunJobFlowInput{
Instances: &emr.JobFlowInstancesConfig{ // Required
Ec2KeyName:                    aws.String("keyname"),
HadoopVersion:                 aws.String("2.7.2"),
InstanceCount:                 aws.Int64(1),
KeepJobFlowAliveWhenNoSteps: aws.Bool(true),
MasterInstanceType:          aws.String("m1.medium"),
Placement: &emr.PlacementType{
AvailabilityZone: aws.String("eu-west-1a"), // Required
},
TerminationProtected:       aws.Bool(true),
},
Name:           aws.String("Go Test Cluster"), // Required
Applications: []*emr.Application{
{ // Required
Name:    aws.String("Ganglia"),
},
{ 
Name: aws.String("Spark"),
},
// More values...
},
JobFlowRole: aws.String("EMR_EC2_DefaultRole"),
LogUri:      aws.String("s3://aws-logs-0000000000-eu-west-1/elasticmapreduce/"),
ReleaseLabel: aws.String("emr-4.6.0"),
ServiceRole:  aws.String("EMR_DefaultRole"),
VisibleToAllUsers: aws.Bool(true),
}
resp, err := svc.RunJobFlow(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}

huangapple
  • 本文由 发表于 2016年3月24日 17:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/36196875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定