

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

# Kit SDK DAX pour Go
<a name="DAX.client.run-application-go-2"></a>

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**

1. Configurez le kit SDK pour Go sur votre instance Amazon EC2 :

   1. Installez le langage de programmation Go (`Golang`).

      ```
      sudo yum install -y golang
      ```

   1. Testez que Golang est installé et fonctionne correctement.

      ```
      go version
      ```

      Un message comme celui-ci devrait s’afficher.

      ```
      go version go1.23.4 linux/amd64
      ```

1. Installez l’exemple d’application Golang.

   ```
   go get github.com/aws-samples/sample-aws-dax-go-v2
   ```

1. 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-table
   ```

   ```
   go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command put-item
   ```

1. 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-item
   ```

   ```
   go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command query
   ```

   ```
   go run ~/go/pkg/mod/github.com/aws-samples/sample-aws-dax-go-v2@v1.0.0/try_dax.go -service dynamodb -command scan
   ```

   Notez les informations de durée, soit le nombre de millisecondes requis pour les tests `GetItem`, `Query` et `Scan`.

1. 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`, `Query` et `Scan` sont 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
     ```
   + **À l'aide** de 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:8111
   ```

   ```
   go 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:8111
   ```

   ```
   go 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:8111
   ```

   ```
   go 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:8111
   ```

   ```
   go 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:8111
   ```

   ```
   go 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:8111
   ```

   Observez le reste de la sortie et notez les informations de durée. Les délais écoulés pour `GetItem`, `Query` et `Scan` devraient être sensiblement inférieurs avec DAX qu’avec DynamoDB.

1. 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 comparables à celles de la AWS SDK pour Go V2
<a name="DAX.client.run-application-go-features-not-in-parity"></a>

Middleware Stack — DAX Go V2 ne prend pas en charge l'utilisation de Middleware Stacks through. APIoptions Pour plus d'informations, voir [Personnalisation des demandes du client AWS SDK pour Go v2 avec un intergiciel](https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/middleware.html#:~:text=You%20can%20customize%20AWS%20SDK,step's%20input%20and%20output%20types).

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
```