Skip to content

Question on Querying Dynamic Hierarchy in Map type column of AWS DynamoDB

0

I am using AWS DynamoDB to store list of files and folder hierarchy of computer Disk in a Map type column. This table will hold information on files that are available on disks of different machines in specific user folders. I want the ability to be able to query this Map column for rows that contain specific file name or file attribute like file size but this file can be at any level of folder hierarchy so I cannot use fixed ExpressionAttributeNames from within DynamoDBContext.QueryAsync method in my C#.Net code which queries this DynamoDB table. How can I query such information. Furthermore I want to optimize this query to retrieve only specific cut down portion of Map type column value which match with my query criteria and not the entire column value. Is it possible to do so? Does DynamoDB allow user to query Map type column for XML/JSON style data that has Tree type data values?

For more details you can check the table named FileArchivesV2 created under my AWS Account in DynamodDB. I want to experiment using it for storing and querying Family tree style hierarchical data.

asked a year ago208 views

2 Answers
1

Querying hierarchical data stored in a Map type column in DynamoDB presents several challenges, especially when you need to search for items at any level of the hierarchy.

DynamoDB does support querying nested attributes within Map type columns, but there are some limitations to consider:

  1. Querying nested attributes: You can use document path expressions with dot notation to access nested attributes in your filter expressions. For example, if your file hierarchy is stored as FileSystem.Users.JohnDoe.Documents.file1.txt, you could reference a specific attribute using FileSystem.Users.JohnDoe.Documents.file1\.txt.size.

  2. Dynamic hierarchical queries: For truly dynamic hierarchical data where you don't know the exact path beforehand, you have a few options:

  • Use a recursive approach where you query level by level
  • Design your data model to include additional attributes that make hierarchical queries easier
  • Consider using a Global Secondary Index (GSI) that stores hierarchical paths in a flattened format
  1. Optimizing query results: To retrieve only specific portions of your Map column that match your criteria, you can use projection expressions to specify which attributes to return. This helps reduce the amount of data transferred.

For hierarchical data like file systems or family trees, a common pattern is to model the hierarchy using a combination of:

  • A path attribute that represents the full path (e.g., "root|folder1|folder2")
  • Parent-child relationships where each item has a parent ID
  • A GraphId attribute to identify which hierarchy tree an item belongs to

For your specific use case of querying for files with specific attributes at any level, you might need to:

  1. Add additional attributes to your items that make them queryable (like a flattened list of all files)
  2. Use a scan operation with a filter expression (though this is less efficient for large tables)
  3. Consider redesigning your data model to better support your access patterns

Note that while DynamoDB can store and query hierarchical data, it doesn't have native support for XML/JSON path-style queries like some document databases. Your query expressions need to reference specific attribute paths.
Sources
Step 6: Create data queries - AWS Prescriptive Guidance
How can i query dynamodb objects that contain a nested object list? | AWS re:Post
Query a DynamoDB table with nested attributes using an AWS SDK - AWS SDK Code Examples
Query a DynamoDB table with nested attributes using an AWS SDK - Amazon DynamoDB

answered a year ago

EXPERT

reviewed a year ago

0

Querying deeply nested, dynamically structured data within a single Map column in DynamoDB, like a varying file hierarchy or family tree, presents challenges because DynamoDB's primary query operations (Query and Scan) and secondary indexes primarily operate on top-level attributes or predefined nested paths. Directly traversing a tree-like structure within a single item's Map attribute is not natively supported in a performant, index-driven way. You can use FilterExpressions with dot notation to check for nested attributes (e.g., FileSystem.Users.JohnDoe.Documents.file1.txt.size), but a Query operation still requires a KeyConditionExpression on the table's primary key or a GSI, meaning you cannot query arbitrary nested values across all items without scanning the entire table.

For optimal retrieval of specific matching portions of a Map type column, you can utilize Projection Expressions. These expressions allow you to specify exactly which attributes, including nested ones using dot notation (e.g., MyMap.NestedKey.SpecificAttribute), should be returned in the query or scan result. This minimizes data transfer and improves efficiency by fetching only the relevant data, rather than the entire large Map attribute.

For truly dynamic, arbitrary querying of hierarchical data where paths are unknown, or for searching specific keywords/attributes across all levels of your "file system" or "family tree," a common best practice is to denormalize relevant attributes into top-level attributes or dedicated Global Secondary Indexes (GSIs). For instance, you could store a flattened path string or a list of all file names and sizes as top-level attributes, which can then be indexed and queried. For complex full-text search or flexible deep querying on dynamic nested JSON-like structures (which DynamoDB Map columns essentially are), the recommended approach is to integrate DynamoDB with Amazon OpenSearch Service (formerly Elasticsearch) via DynamoDB Streams. This offloads the complex search requirements to OpenSearch, which is designed for full-text and nested document queries.

Reference Links:

Working with JSON data in Amazon DynamoDB (AWS Blog): https://aws.amazon.com/blogs/database/working-with-json-data-in-amazon-dynamodb/ (Covers using dot notation for nested attributes)

Condition and filter expressions, operators, and functions in DynamoDB: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html (Details Filter Expressions and dot notation)

Projection Expressions in DynamoDB: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ProjectionExpressions.html (Explains how to retrieve specific attributes, including nested ones)

Modeling Hierarchical Data in DynamoDB (AWS Prescriptive Guidance): https://docs.aws.amazon.com/prescriptive-guidance/latest/dynamodb-data-modeling/hierarchical-data-modeling.html (Discusses strategies for hierarchical data, often involving denormalization or adjacency lists)

Using an OpenSearch Ingestion pipeline with Amazon DynamoDB: https://docs.aws.amazon.com/opensearch-service/latest/developerguide/configure-client-ddb.html (Explains how to stream DynamoDB data to OpenSearch for advanced searching, including nested queries)

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.