Kit SDK DAX pour Go
Pour exécuter l’exemple d’application du kit SDK Amazon DynamoDB Accelerator (DAX) pour Go sur votre instance Amazon EC2, procédez comme suit.
Pour exécuter l’exemple du kit SDK pour Go pour DAX
-
Configurez le kit SDK pour Go sur votre instance Amazon EC2 :
-
Installez le langage de programmation Go (
Golang).sudo yum install -y golang -
Testez que Golang est installé et fonctionne correctement.
go versionUn message comme celui-ci devrait s’afficher.
go version go1.23.4 linux/amd64
-
-
Installez l’exemple d’application Golang.
go get github.com/aws-samples/sample-aws-dax-go-v2 -
Exécutez les programmes Golang suivants. Le premier programme crée une table DynamoDB nommée
TryDaxGoTable. Le deuxième programme écrit des données dans la table.go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command create-tablego run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command put-item -
Exécutez les programmes Golang suivants.
go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command get-itemgo run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command querygo run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command scanNotez les informations de durée, soit le nombre de millisecondes requis pour les tests
GetItem,QueryetScan. -
A l’étape précédente, vous avez exécuté les programmes par rapport au point de terminaison DynamoDB. A présent, réexécutez-les mais, cette fois, les opérations
GetItem,QueryetScansont traitées par votre cluster DAX.Pour déterminer le point de terminaison de votre cluster DAX, choisissez l’une des options suivantes :
-
Utilisation de la console DynamoDB – Choisissez votre cluster DAX. Le point de terminaison du cluster s’affiche dans la console, comme dans l’exemple suivant.
dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com -
Utilisation de l’AWS CLI – Entrez la commande suivante.
aws dax describe-clusters --query "Clusters[*].ClusterDiscoveryEndpoint"Le point de terminaison du cluster apparaît dans la sortie, comme dans l’exemple suivant.
{ "Address": "my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com", "Port": 8111, "URL": "dax://my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com" }
À présent, réexécutez les programmes, mais cette fois, spécifiez le point de terminaison du cluster en tant que paramètre de ligne de commande.
go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command get-item -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command query -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command scan -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command paginated-scan -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command paginated-query -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dax -command paginated-batch-get -endpoint my-cluster.l6fzcv.dax-clusters.us-east-1.amazonaws.com:8111Observez le reste de la sortie et notez les informations de durée. Les délais écoulés pour
GetItem,QueryetScandevraient être sensiblement inférieurs avec DAX par rapport à DynamoDB. -
-
Exécutez le programme Golang suivant pour supprimer
TryDaxGoTable.go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command delete-table
Fonctionnalités non identiques à celles de AWS SDK pour Go V2
Pile d’intergiciel : DAX Go V2 ne prend pas en charge l’utilisation de piles d’intergiciel via APIoptions. Pour plus d’informations, consultez Customizing the AWS SDK pour Go v2 Client Requests with Middleware.
Exemple :
// Custom middleware implementation type customSerializeMiddleware struct{} // ID returns the identifier for the middleware func (m *customSerializeMiddleware) ID() string { return "CustomMiddleware" } // HandleSerialize implements the serialize middleware handler func (m *customSerializeMiddleware) HandleSerialize( ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler, ) ( out middleware.SerializeOutput, metadata middleware.Metadata, err error, ) { // Add your custom logic here before the request is serialized fmt.Printf("Executing custom middleware for request: %v\n", in) // Call the next handler in the middleware chain return next.HandleSerialize(ctx, in) } func executeGetItem(ctx context.Context) error { client, err := initItemClient(ctx) if err != nil { os.Stderr.WriteString(fmt.Sprintf("failed to initialize client: %v\n", err)) return err } st := time.Now() for c := 0; c < iterations; c++ { for i := 0; i < pkMax; i++ { for j := 0; j < skMax; j++ { // Create key using attributevalue.Marshal for type safety pk, err := attributevalue.Marshal(fmt.Sprintf("%s_%d", keyPrefix, i)) if err != nil { return fmt.Errorf("error marshaling pk: %v", err) } sk, err := attributevalue.Marshal(fmt.Sprintf("%d", j)) if err != nil { return fmt.Errorf("error marshaling sk: %v", err) } key := map[string]types.AttributeValue{ "pk": pk, "sk": sk, } in := &dynamodb.GetItemInput{ TableName: aws.String(table), Key: key, } // Custom middleware option customMiddleware := func(o *dynamodb.Options) { o.APIOptions = append(o.APIOptions, func(stack *middleware.Stack) error { // Add custom middleware to the stack return stack.Serialize.Add(&customSerializeMiddleware{}, middleware.After) }) } // Apply options to the GetItem call out, err := client.GetItem(ctx, in, customMiddleware) if err != nil { return err } writeVerbose(out) } } } d := time.Since(st) os.Stdout.WriteString(fmt.Sprintf("Total Time: %v, Avg Time: %v\n", d, d/iterations)) return nil }
Sortie :
failed to execute command: custom middleware through APIOptions is not supported in DAX client exit status 1