Webアプリケーションでは、ページテーブルを実装したいと考えています。 DynamoDBの「レイアウト」とは、ユーザーに複数のアイテムがあることです。そのため、partition key=user
とsort key=created
(タイムスタンプ)を選択しました。 UIは数100項目から合計50項目の項目をページに表示する。REST経由のDynamoDBのPaginatedList
項目は、REST-Api呼び出しによってUIに渡されます。私はquery
またはscan
の項目だけのページで、テーブル全体ではない。ページネーションは前後に可能でなければならない。
これまでのところ、私はDynamoDBMapper
を使用して、次のを作ってみた:
/**
* Returns the next page of items DEPENDENT OF THE USER. Note: This method internally uses
* DynamoDB QUERY. Thus it requires "user" as a parameter. The "created" parameter is optional.
* If provided, both parameters form the startKey for the pagination.
*
* @param user - mandatory: The user for which to get the next page
* @param created - optional: for providing a starting point
* @param limit - the returned page will contain (up to) this number of items
* @return
*/
public List<SampleItem> getNextPageForUser(final String user, final Long created, final int limit) {
// To iterate DEPENDENT on the user we use QUERY. The DynamoDB QUERY operation
// always require the partition key (=user).
final SampleItem hashKeyObject = new SampleItem();
hashKeyObject.setUser(user);
// The created is optional. If provided, it references the starting point
if (created == null) {
final DynamoDBQueryExpression<SampleItem> pageExpression = new DynamoDBQueryExpression<SampleItem>()//
.withHashKeyValues(hashKeyObject)//
.withScanIndexForward(true) //
.withLimit(limit);
return mapper.queryPage(SampleItem.class, pageExpression).getResults();
} else {
final Map<String, AttributeValue> startKey = new HashMap<String, AttributeValue>();
startKey.put(SampleItem.USER, new AttributeValue().withS(user));
startKey.put(SampleItem.CREATED, new AttributeValue().withN(created.toString()));
final DynamoDBQueryExpression<SampleItem> pageExpression = new DynamoDBQueryExpression<SampleItem>()//
.withHashKeyValues(hashKeyObject)//
.withExclusiveStartKey(startKey)//
.withScanIndexForward(true) //
.withLimit(limit);
return mapper.queryPage(SampleItem.class, pageExpression).getResults();
}
}
以前のコードは、それがwithScanIndexForward(false)
を使用するだけで、似ています。私はこのアプローチで、車輪を再発明していた場合、私は疑問に思う
@RequestMapping(value = "/page/{user}/{created}", method = RequestMethod.GET)
public List<SampleDTO> listQueriesForUserWithPagination(//
@RequestParam(required = true) final String user,//
@RequestParam(required = true) final Long created,//
@RequestParam(required = false) final Integer n,//
@RequestParam(required = false) final Boolean isBackward//
) {
final int nrOfItems = n == null ? 100 : n;
if (isBackward != null && isBackward.booleanValue()) {
return item2dto(myRepo.getPrevQueriesForUser(user, created, nrOfItems));
} else {
return item2dto(myRepo.getNextQueriesForUser(user, created, nrOfItems));
}
}
:私は、単一のメソッドを提供します私のREST-APIコントローラで
。
DynamoDBのPaginatedQueryList
またはPaginatedScanList
をREST経由でUIに渡すことができますか。そうすれば、javascriptページネーションがアイテムにアクセスすると遅延ロードされます。 他のDBとの連携から、私はDBエントリオブジェクトを転送していないので、私のコードスニペットがデータ(item2dto
)を再パックしているのです。
さらに、DynamoDBによるページネーションはちょっと変わったように見えます。これまでのところ、アイテムの合計数をUIに提供する可能性はありませんでした。したがって、UIには、実際に何ページ続くかを知ることなく、「次のページ」と「前のページ」のボタンしかありません。したがって、ページ5に直接ジャンプすることはできません。