

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.

# Étape 6 : Création de requêtes de données
<a name="step6-hierarchical-model"></a>

Après avoir défini vos modèles d'accès et conçu votre modèle de données, vous pouvez interroger des données hiérarchiques dans la base de données DynamoDB. Afin de réduire les coûts et de garantir les performances, les exemples suivants utilisent uniquement l'opération de requête sans`Scan`.
+ **Trouvez les ancêtres d'un composant.**

  Pour trouver les ancêtres (parent, grand-parent, arrière-grand-parent, etc.) du composant CM8, interrogez la table de base à l'aide de `ComponentId = "CM8"`. La requête va renvoyer l'enregistrement suivant.

  Pour réduire la taille des données de résultat, vous pouvez utiliser une expression de projection pour renvoyer uniquement l'attribut `Path`.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **Trouvez les enfants immédiats d'un composant.**

  Pour obtenir tous les composants secondaires immédiats ou d'un niveau en aval du composant CM2, interrogez GSI1 à l'aide de. `ParentId = "CM2"` La requête va renvoyer l'enregistrement suivant.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **Trouvez tous les composants enfants en aval à l'aide d'un composant de niveau supérieur.**

  Pour obtenir tous les composants enfants ou descendants du composant de niveau supérieur CM1, interrogez GSI2 à l'aide de `GraphId = "CM1#1"` et`begins_with("Path", "CM1|")`, puis utilisez une expression de projection avec. `ComponentId` Elle renverra tous les composants liés à cette arborescence.

  Cet exemple comporte une arborescence unique, avec CM1 comme composant supérieur. En réalité, vous pourriez avoir des millions de composants de niveau supérieur dans la même table.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)
+ **Trouvez tous les composants enfants en aval à l'aide d'un composant de niveau intermédiaire.**

  Pour obtenir tous les composants enfants ou descendants de manière récursive pour le composant CM2, vous avez deux options. Vous pouvez interroger de manière récursive niveau par niveau ou vous pouvez interroger l'index GSI2.
  + Interrogez GSI1, niveau par niveau, de manière récursive, jusqu'à atteindre le dernier niveau des composants enfants.

    1. Interrogez GSI1 en utilisant `ParentId = "CM2"`. Cela va renvoyer l'enregistrement suivant.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. Encore une fois, interrogez GSI1 en utilisant `ParentId = "CM4"`. Cela va renvoyer l'enregistrement suivant.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. Encore une fois, interrogez GSI1 en utilisant `ParentId = "CM5"`. Cela va renvoyer l'enregistrement suivant.

       Continuez la boucle : interrogez pour chaque `ComponentId` jusqu'à ce que vous atteigniez le dernier niveau. Lorsqu'une requête utilisant `ParentId = "<ComponentId>"` ne renvoie aucun résultat, le résultat précédent provenait du dernier niveau de l'arborescence.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. Fusionnez tous les résultats.

        

       résultat= [CM4, CM5] \+ [CM8, CM9] \+ [CM10]

                = [CM4, CM5, CM8, CM9, CM10]
  + Interrogez GSI2, qui stocke une arborescence hiérarchique pour un composant de niveau supérieur (une voiture ou CM1).

    1. Tout d'abord, recherchez le composant de niveau supérieur ou l'ancêtre supérieur et `Path` de CM2. Pour cela, interrogez la table de base en utilisant `ComponentId = "CM2"` pour trouver le chemin de ce composant dans l'arborescence hiérarchique. Sélectionnez les attributs `GraphId` et `Pat` h. La requête va renvoyer l'enregistrement suivant.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. Interrogez GSI2 en utilisant. `GraphId = "CM1#1" AND BEGINS_WITH("Path", "CM1|CM2|")` La requête va renvoyer les résultats suivants.    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/fr_fr/prescriptive-guidance/latest/dynamodb-data-modeling/step6-hierarchical-model.html)

    1. Sélectionnez l'attribut `ComponentId` afin de renvoyer tous les composants enfants pour CM2.