如何使用分页列出表中的所有项目

huangapple go评论100阅读模式
英文:

How to list all the items in a table with pagination

问题

我正在尝试使用分页列出 DynamoDB 表中的所有项,以下是我的尝试:

const tableName = "RecordingTable"

type Recording struct {
	ID        string `dynamodbav:"id"`
	CreatedAt string `dynamodbav:"createdAt"`
	UpdatedAt string `dynamodbav:"updatedAt"`
	Duration  int    `dynamodbav:"duration"`
}

type RecordingRepository struct {
	ctx context.Context
	svc *dynamodb.Client
}

func NewRecordingRepository(ctx context.Context) (*RecordingRepository, error) {
	cfg, err := config.LoadDefaultConfig(ctx)
	if err != nil {
		return nil, err
	}

	return &RecordingRepository{ctx, dynamodb.NewFromConfig(cfg)}, nil
}

func (r *RecordingRepository) List(page int, size int) ([]Recording, error) {
	size32 := int32(size)
	queryInput := &dynamodb.QueryInput{
		TableName: aws.String(tableName),
		Limit:     &size32,
	}

	recordings := []Recording{}
	queryPaginator := dynamodb.NewQueryPaginator(r.svc, queryInput)

	for i := 0; queryPaginator.HasMorePages(); i++ {
		result, err := queryPaginator.NextPage(r.ctx)
		if err != nil {
			return nil, err
		}

		if i == page {
			if result.Count > 0 {
				for _, v := range result.Items {
					recording := Recording{}
					if err := attributevalue.UnmarshalMap(v, &recording); err != nil {
						return nil, err
					}
					recordings = append(recordings, recording)
				}
			}
			break
		}
	}

	return recordings, nil
}

当我运行上述代码时,我收到以下错误消息:

api error ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

但是,为什么我在获取所有项时需要指定 KeyConditionExpression?是否有其他方法或解决方法可以解决这个问题?

英文:

I'm trying to list all the items in a DynamoDB table with pagination, and here below is my attempt:

const tableName = "RecordingTable"
type Recording struct {
ID string `dynamodbav:"id"`
CreatedAt string `dynamodbav:"createdAt"`
UpdatedAt string `dynamodbav:"updatedAt"`
Duration int `dynamodbav:"duration"`
}
type RecordingRepository struct {
ctx context.Context
svc *dynamodb.Client
}
func NewRecordingRepository(ctx context.Context) (*RecordingRepository, error) {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
return &RecordingRepository{ctx, dynamodb.NewFromConfig(cfg)}, nil
}
func (r *RecordingRepository) List(page int, size int) ([]Recording, error) {
size32 := int32(size)
queryInput := &dynamodb.QueryInput{
TableName: aws.String(tableName),
Limit:     &size32,
}
recordings := []Recording{}
queryPaginator := dynamodb.NewQueryPaginator(r.svc, queryInput)
for i := 0; queryPaginator.HasMorePages(); i++ {
result, err := queryPaginator.NextPage(r.ctx)
if err != nil {
return nil, err
}
if i == page {
if result.Count > 0 {
for _, v := range result.Items {
recording := Recording{}
if err := attributevalue.UnmarshalMap(v, &recording); err != nil {
return nil, err
}
recordings = append(recordings, recording)
}
}
break
}
}
return recordings, nil
}

When I run the code above, I get the following error message:

api error ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

But why should I specify a KeyConditionExpression when I want to get all the items? Is there another way to go or a workaround this?

答案1

得分: 4

Query需要使用您的密钥。它用于在DynamoDB中查找_特定_项目。要获取DynamoDB中的_所有_项目,您需要使用Scan操作。

您可以轻松地在代码中进行修复。

请使用ScanInput替代QueryInput,并使用NewScanPaginator替代NewQueryPaginator

英文:

Query does need your keys. It is meant to find specific items in your DynamoDB. To get all items in your DynamoDB, you need to use the Scan operation.

This should be easily fixed in your code.

Instead of QueryInput use ScanInput and instead of NewQueryPaginator use NewScanPaginator.

答案2

得分: 0

刚刚将QueryInput替换为ScanInput,将QueryPaginator替换为ScanPaginator

英文:

Just replaced QueryInput with ScanInput and QueryPaginator with ScanPaginator.

huangapple
  • 本文由 发表于 2021年5月30日 17:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/67759700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定