有限制的 AWS dynamoDb 分页
AWS dynamoDb pagination with limit
我正在尝试对在 dynamoDb 中创建的 table 中的条目进行分页
无论如何检查是否超过 table 项。
例如,如果我调用从 table 中获取项目,我会收到以下错误
PANIC: runtime error: invalid memory address or nil pointer dereference
goroutine 51 [running]:
github.com/urfave/negroni.(*Recovery).ServeHTTP.func1(0xdfae028, 0xc000186028, 0xc0002162d0, 0xc000154200)
/Users/hammadali/go/pkg/mod/github.com/urfave/negroni@v1.0.0/recovery.go:159 +0xcb
panic(0x498ea20, 0x51f4410)
/usr/local/Cellar/go/1.14.5/libexec/src/runtime/panic.go:969 +0x166
github.com/prohousing-as/ph-supplier-service/application.(*SupplierService).GetAllSuppliers(0xc0002cc300, 0xc00002a270, 0x24, 0xbb8, 0xc0004403e0, 0x0, 0x0)
/Users/hammadali/source/ph-supplier-service/application/supplier_service.go:41 +0x330
github.com/prohousing-as/ph-supplier-service/ui.(*SupplierController).GetAllSupplier(0xc000526ed0, 0xdfae028, 0xc000186028, 0xc0003be300)
/Users/hammadali/source/ph-supplier-service/ui/supplier_controller.go:40 +0x139
net/http.HandlerFunc.ServeHTTP(0xc000527170, 0xdfae028, 0xc000186028, 0xc0003be300)
/usr/local/Cellar/go/1.14.5/libexec/src/net/http/server.go:2041 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0002e40c0, 0xdfae028, 0xc000186028, 0xc0003be100)
/Users/hammadali/go/pkg/mod/github.com/gorilla/mux@v1.7.4/mux.go:210 +0xe2
获取所有项目的函数
// fetches the items from dynamoDb and stores it in res
res, err := s.SupplierRepo.GetAllSupplier(uuid, limit)
if err != nil {
fmt.Println("from application: ", err)
return nil, err
}
item := &domain.SupplierTableItems{}
all := &domain.AllSupplierItems{}
// Looping over the result and appending it on the list
for _, i := range res.Items {
err = dynamodbattribute.UnmarshalMap(i, item)
all.Collection = append(all.Collection, *item)
if err != nil {
fmt.Println(err)
}
}
// storing lastEvaluatedKey for pagination
all.LastEvaluatedKey = *res.LastEvaluatedKey["uuid"].S
return all, nil
有没有办法检查为获取项目所做的调用是否超过了 table 个条目,并且只提供剩余的条目数。
您的错误与 DynamoDB 的分页没有太大关系,您只是想访问 /Users/hammadali/source/ph-supplier-service/application/supplier_service.go:41
附近某处不存在的内容
以其他方式回答您的问题:是的,DynamoDB 会在分页完成时告诉您。 From the docs:
- If the result contains a LastEvaluatedKey element and it's non-null, proceed to step 2.
- If there is not a LastEvaluatedKey in the result, there are no more items to be retrieved.
我正在尝试对在 dynamoDb 中创建的 table 中的条目进行分页 无论如何检查是否超过 table 项。
例如,如果我调用从 table 中获取项目,我会收到以下错误
PANIC: runtime error: invalid memory address or nil pointer dereference
goroutine 51 [running]:
github.com/urfave/negroni.(*Recovery).ServeHTTP.func1(0xdfae028, 0xc000186028, 0xc0002162d0, 0xc000154200)
/Users/hammadali/go/pkg/mod/github.com/urfave/negroni@v1.0.0/recovery.go:159 +0xcb
panic(0x498ea20, 0x51f4410)
/usr/local/Cellar/go/1.14.5/libexec/src/runtime/panic.go:969 +0x166
github.com/prohousing-as/ph-supplier-service/application.(*SupplierService).GetAllSuppliers(0xc0002cc300, 0xc00002a270, 0x24, 0xbb8, 0xc0004403e0, 0x0, 0x0)
/Users/hammadali/source/ph-supplier-service/application/supplier_service.go:41 +0x330
github.com/prohousing-as/ph-supplier-service/ui.(*SupplierController).GetAllSupplier(0xc000526ed0, 0xdfae028, 0xc000186028, 0xc0003be300)
/Users/hammadali/source/ph-supplier-service/ui/supplier_controller.go:40 +0x139
net/http.HandlerFunc.ServeHTTP(0xc000527170, 0xdfae028, 0xc000186028, 0xc0003be300)
/usr/local/Cellar/go/1.14.5/libexec/src/net/http/server.go:2041 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0002e40c0, 0xdfae028, 0xc000186028, 0xc0003be100)
/Users/hammadali/go/pkg/mod/github.com/gorilla/mux@v1.7.4/mux.go:210 +0xe2
获取所有项目的函数
// fetches the items from dynamoDb and stores it in res
res, err := s.SupplierRepo.GetAllSupplier(uuid, limit)
if err != nil {
fmt.Println("from application: ", err)
return nil, err
}
item := &domain.SupplierTableItems{}
all := &domain.AllSupplierItems{}
// Looping over the result and appending it on the list
for _, i := range res.Items {
err = dynamodbattribute.UnmarshalMap(i, item)
all.Collection = append(all.Collection, *item)
if err != nil {
fmt.Println(err)
}
}
// storing lastEvaluatedKey for pagination
all.LastEvaluatedKey = *res.LastEvaluatedKey["uuid"].S
return all, nil
有没有办法检查为获取项目所做的调用是否超过了 table 个条目,并且只提供剩余的条目数。
您的错误与 DynamoDB 的分页没有太大关系,您只是想访问 /Users/hammadali/source/ph-supplier-service/application/supplier_service.go:41
以其他方式回答您的问题:是的,DynamoDB 会在分页完成时告诉您。 From the docs:
- If the result contains a LastEvaluatedKey element and it's non-null, proceed to step 2.
- If there is not a LastEvaluatedKey in the result, there are no more items to be retrieved.