8

I have a requirement in which I just need a single row to be returned while querying a table in Dynamodb. I can see a parameter in aws-cli named 'max-items' which apparently limits the result size of the query. Here is the sample query:

aws dynamodb query --table-name testTable --key-condition-expression "CompositePartitionKey = :pk" 
--expression-attribute-values '{ ":pk": { "S": "1234_125" }, ":ps": { "S": "SOME_STATE" }
    }' 
--filter-expression 'StateAttribute IN (:ps) AND attribute_not_exists(AnotherAttribute)' 
--index-name GSI_PK_SK --endpoint-url http://localhost:8000    --max-items 1

But I am not able to figure out any similar keyword/attribute in Go.

Here is what I could find relevant: How to set limit of matching items returned by DynamoDB using Java?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
sunil khatri
  • 81
  • 1
  • 1
  • 4

3 Answers3

9

As you can see in DynamoDB's official description of the "Query" operation - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html - the parameter you are looking for is "Limit". Please consult your Go library's documentation on how exactly to pass this parameter to the query.

By the way, note that Limit doesn't quite limit the number of returned results, but rather the number of rows read at the server side. If your query has a filter, it can return fewer than Limit results. I don't know whether this matters to you or not.

Nadav Har'El
  • 11,785
  • 1
  • 24
  • 45
  • 5
    Thank you for your response. You have mentioned correctly what 'Limit' does. **But my use case is different from Limit. I want to limit the results returned after the query has been evaluated(similar to what limit does in Mysql)** But what 'Limit' does in Dynamodb is, it restricts the rows to be evaluated to the given limit size (in which case I might not even get a single result from query, even though actually there were relevant rows in the database). One more thing, I am using 1.13.1 version of Go and I don't see any such method/way provided in the documentation. Further help appreciated. – sunil khatri Nov 18 '19 at 09:39
  • 1
    There is no way to do what you want, unfortunately. If you use filtering, it is always possible that even the largest possible page size for the query (if Limit isn't passed, it defaults to 1MB reads) nothing at all will be returned after filtering. Something you have to remember is that you pay for the amount of data read **before** filtering. If filtering reads a lot of data and filters most of it out, this will cost you a lot of money - even if the amount of items passed over the network is just one item. – Nadav Har'El Nov 18 '19 at 09:47
  • Thank you for this update. I guess then I have only one option i.e. to get all the data and process just the first one. – sunil khatri Nov 19 '19 at 03:21
  • 1
    Yes, just that you don't need to get "all the data", you can also set a Limit just be prepared for the possibility that you can get zero results and need to fetch further pages until you get that first result you are looking for. – Nadav Har'El Nov 19 '19 at 07:57
2

You might want to look into pagination. You can use page-size to control how much you can get in each query.

More details on pagination can be found on Paginating Table Query Results.

Bennett McElwee
  • 24,740
  • 6
  • 54
  • 63
chepukha
  • 2,371
  • 3
  • 28
  • 40
-1

You need to paginate through DynamoDB's responses until your "user limit" is fulfilled.