Comments (6)
this was a long time ago so I'm not sure if the API has changed since then, but you'd use it the same way as you'd use query
.
If you modify the function to accept table
as a param, it would look like:
val operations = for {
_ <- table.putAll(...)
result <- queryPaginated(table, 'something -> 123, limit, Some(startKey))
} yield result
ScanamoAsync.exec(client)(operations)
or for nicer syntax you could define an implicit class that wraps table
implicit class TableOps(val table: Table[_]) extends AnyVal {
def queryPaginated[T: DynamoFormat](...) = // same function as earlier
}
then you could do table.queryPaginated('something -> 123, limit, Some(startKey))
from scanamo.
mostly for client-side pagination, where the front-end client only needs to display few items at a time, and a way to resume from where they left off (in my case, since the client can't pass in a java.util.Map[String, AttributeValue]
, I'd encode/decode it as a String
)
in my own code, I ended up just doing something like:
case class DynamoPage[T](
items: Seq[T],
lastEvaluatedKey: java.util.Map[String, AttributeValue]
)
val table: Table[Something] = ???
def queryPaginated[T: DynamoFormat](
query: Query[_],
limit: Option[Int],
startKey: Option[java.util.Map[String, AttributeValue]]
): ScanamoOps[DynamoPage[T]] = {
val req = new QueryRequest().withTableName(table.name)
limit.foreach(req.setLimit(_))
startKey.foreach(key ⇒ req.setExclusiveStartKey(key))
ScanamoOps.query(query(req)).map { qr: QueryResult ⇒
val items = qr.getItems.asScala.map(ScanamoFree.read[T])
val key = Option(qr.getLastEvaluatedKey)
DynamoPage(items, key)
}
}
that's a simplified version to get the gist of it, though the actual code I have is more complicated:
- has a typeclass
PageKeyFormat
to convert betweenj.u.Map[String, AttributeValue]
and a typeK
(Key) - the lastEvaluatedKey in
DynamoPage
is actually anOption[K]
- I define a
PageKeyFormat
to convert between the java Map to a compressed JSON string, which is the string key that the clients pass in to specify theirlastEvaluatedKey
from scanamo.
I believe this is a very common use case where a query returns many records and clients need a way to paginate through it. It would be nice if we can pass the paging params (or last evaluated key) to dynamo and have dynamo only return results specified by limit param.
from scanamo.
I did consider this when implementing limit support, but was struggling to think of a good use case for this. Could you describe what you'd want it for in more detail?
Would it be enough to be able to provide the next batch of results for a given batch of results?
from scanamo.
@williamho could you be able to provide some extra details on how to use your example outside of scanamo? Do you define your own interpreter?
from scanamo.
You can now use table.query0
or table.scan0
to access the raw dynamodb result, get the last evaluated key, and feed that into table.from
in the next loop iteration.
from scanamo.
Related Issues (20)
- Extend transactPutAll, transactUpdateAll and transactDeleteAll with optional ConditionExpression argument HOT 2
- Extend scan operation with Segments
- Adopt `sbt-dynamodb` into the Scanamo organisation? HOT 3
- Not able to use >= on string sort key
- Publish latest?
- Question: Should I be instantiating a different client per service?
- migrate to github action HOT 6
- Conditionally apply a filter
- [error] clienterror: an error occurred (validationexception) when calling the putitem operation: one or more parameter values were invalid: contains duplicates. HOT 1
- PutReturn.NewValue is not supported by AWS DynamoDB
- Scala 3 HOT 3
- Scanamo query timeouts...
- Null Pointer Exception when trying to call any methods accessing DynamoDb
- Where is the API documentation? HOT 1
- Pagination against GSIs is cumbersome with the current API.
- Add support for BigInt
- Pekko integration HOT 1
- ZIO 2 support
- Let's release v1.0.0 and drop milestone releases! HOT 3
- Error when case class has more than 36 properties
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scanamo.