AWS PutItem在数据库中没有创建条目。

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

AWS PutItem not creating entries in DB

问题

我正在尝试编写一个 Lambda 函数,该函数将字符串数组写入一个通用集合,其中主键是集合的类型。它应该始终用新的项替换当前项(如果存在,则创建一个新项)。

这是我的处理函数:

func Handler(ctx context.Context) (Response, error) {
	item := lib.SkillsCollection{
		Collection: lib.SkillsCollectionName,
		Value: lib.Skills{
			"test",
			"test 2",
		},
	}

	log.Printf("Prepare item %v", item)

	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))
	svc := dynamodb.New(sess)

	log.Printf("Created session")

	av, err := dynamodbattribute.MarshalMap(item)

	if err != nil {
		log.Fatalf("Marshall error mapping value %s", err)
	}

	log.Printf("Marshalled item %v", av)

	input := &dynamodb.PutItemInput{
		TableName: aws.String(lib.DbCollectionsTable),
		Item:      av,
	}

	log.Println("Start scanning collections")

	_, err = svc.PutItem(input)

	if err != nil {
		log.Fatalf("Error during put %v", err)
	}

	var buf bytes.Buffer

	body, err := json.Marshal(map[string]interface{}{
		"message": "Go Serverless v1.0! Your function executed successfully!",
	})
	if err != nil {
		return Response{StatusCode: 404}, err
	}
	json.HTMLEscape(&buf, body)

	resp := Response{
		StatusCode:      200,
		IsBase64Encoded: false,
		Body:            buf.String(),
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	}

	return resp, nil
}

根据日志,一切都正常。没有抛出错误,并且我收到了状态码为 200 的响应。

这是日志:

AWS PutItem在数据库中没有创建条目。

然而,在此之后,我在 DynamoDB 表中看不到任何项。它是空的。我无法找出问题的根源在哪里。与此同时,我可以看到表中有写入操作。

AWS PutItem在数据库中没有创建条目。

我已经按照 AWS 文档中的示例重新编写了解决方案,但没有成功。

如果有关系的话,这是结构体:

type Skills []string

type SkillsCollection struct {
	Collection string `dynamodbav:"collection" json:"collection"`
	Value      Skills `dynamodbav:"value" json:"value"`
}

以及数据库设置配置:

CollectionsTable:
  Type: AWS::DynamoDB::Table
  Properties:
    AttributeDefinitions:
      - AttributeName: collection
        AttributeType: S
    KeySchema:
      - AttributeName: collection
        KeyType: HASH
    BillingMode: PAY_PER_REQUEST
    TableName: ${self:custom.collectionsTableName}
英文:

I'm trying to write a lambda function which writes an array of strings to a generic collection where the Primary key is the type of a collection. It should always replace the current item (if exists, otherwise create one) with a new one.

This is my handler function

func Handler(ctx context.Context) (Response, error) {
item := lib.SkillsCollection{
Collection: lib.SkillsCollectionName,
Value: lib.Skills{
"test",
"test 2",
},
}
log.Printf("Prepare item %v", item)
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
log.Printf("Created session")
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
log.Fatalf("Marshall error mapping value %s", err)
}
log.Printf("Marshalled item %v", av)
input := &dynamodb.PutItemInput{
TableName: aws.String(lib.DbCollectionsTable),
Item:      av,
}
log.Println("Start scanning collections")
_, err = svc.PutItem(input)
if err != nil {
log.Fatalf("Error during put %v", err)
}
var buf bytes.Buffer
body, err := json.Marshal(map[string]interface{}{
"message": "Go Serverless v1.0! Your function executed successfully!",
})
if err != nil {
return Response{StatusCode: 404}, err
}
json.HTMLEscape(&buf, body)
resp := Response{
StatusCode:      200,
IsBase64Encoded: false,
Body:            buf.String(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return resp, nil
}

According to the logs everything is OK. No errors are thrown and I get response with status 200

These are the logs
AWS PutItem在数据库中没有创建条目。

However, after that I don't see any items in the DynamoDB table. It's empty. I can't figure out where to look for the root of the issue. At the same time I can see that there were writes to the table.

AWS PutItem在数据库中没有创建条目。

I've already rewritten the solution to follow the example in the AWS docs but no luck with that.

The struct in case it matters

type Skills []string
type SkillsCollection struct {
Collection string `dynamodbav:"collection" json:"collection"`
Value      Skills `dynamodbav:"value" json:"value"`
}

And the DB setup config

CollectionsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: collection
AttributeType: S
KeySchema:
- AttributeName: collection
KeyType: HASH
BillingMode: PAY_PER_REQUEST
TableName: ${self:custom.collectionsTableName}

答案1

得分: 2

你还没有证明表中没有任何项目。你是否看到"项目数量"为0,并认为这是及时的?请注意,在"项目摘要"标题下面,它说数量只会每6小时更新一次。尝试点击"获取实时项目数量"按钮,我敢打赌你会看到一些项目。

为什么项目数量不是持续维护的?因为它是一个可以存储数万亿个项目的大型分布式数据库。获取实时项目数量并不总是一个轻量级的过程。

英文:

You haven't demonstrated that there are no items in the table. Are you seeing that "Item count" of 0 and thinking it's timely? Notice under the Item Summary header is says the count is only updated every 6 hours. Try hitting the "Get live item count" button and I bet you'll see some items.

Why isn't the item count continuously maintained? Because it's a large distributed database which can store many trillions of items. Getting a live item count isn't always a lightweight process.

huangapple
  • 本文由 发表于 2022年5月2日 04:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/72080217.html
匿名

发表评论

匿名网友

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

确定