{"id":208136,"date":"2025-07-08T14:39:34","date_gmt":"2025-07-08T06:39:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208136\/"},"modified":"2025-07-08T14:39:34","modified_gmt":"2025-07-08T06:39:34","slug":"dynamodb-%e8%8a%b1%e8%b4%b9%e5%a4%aa%e5%a4%9a%e6%97%b6%e9%97%b4%e6%9d%a5%e5%93%8d%e5%ba%94%e5%8d%95%e4%b8%aa%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208136\/","title":{"rendered":"DynamoDB \u82b1\u8d39\u592a\u591a\u65f6\u95f4\u6765\u54cd\u5e94\u5355\u4e2a\u9879\u76ee"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>DynamoDB \u82b1\u8d39\u592a\u591a\u65f6\u95f4\u6765\u54cd\u5e94\u5355\u4e2a\u9879\u76ee<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 13:55:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8Golang\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aDynamoDB \u82b1\u8d39\u592a\u591a\u65f6\u95f4\u6765\u54cd\u5e94\u5355\u4e2a\u9879\u76ee\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a go rest \u670d\u52a1\uff0c\u5b83\u8fde\u63a5\u5230 dynamodb \u4ee5\u68c0\u7d22\u4e00\u4e9b\u4ea7\u54c1\u3002\u8be5\u8868\u5305\u542b\u5927\u7ea6 100.000 \u4e2a\u4ea7\u54c1\uff0c\u6211\u901a\u5e38\u68c0\u7d22\u5927\u7ea6 20.0000 \u4e2a\u4ea7\u54c1\uff08\u7136\u540e\u5728\u5b9e\u9645\u56de\u590d\u4e4b\u524d\u88ab\u5176\u4ed6\u8fdb\u7a0b\u8fc7\u6ee4\u6389\uff0c\u4f46\u8fd9\u4e0e\u6211\u7684\u95ee\u9898\u65e0\u5173\uff09\u3002<\/p>\n<p>\u6211\u770b\u5230\u7684\u95ee\u9898\u662f\u6211\u7684\u6574\u4e2a\u8bf7\u6c42\u5927\u7ea6\u9700\u8981 4 \u79d2\uff0c\u8fd9\u5bf9\u4e8e\u6211\u7684\u76ee\u6807\u6765\u8bf4\u975e\u5e38\u9ad8\u3002\u6211\u5f00\u59cb\u8bb0\u5f55\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6bcf\u4e00\u5c42\uff0c\u76f4\u5230\u6700\u7ec8\u5230\u8fbe\u6700\u5c0f\u7684\u90e8\u5206\uff0c\u5373\u4ece\u6570\u636e\u5e93\u4e2d\u5355\u72ec\u68c0\u7d22\u4e00\u4e2a\u9879\u76ee\u3002<\/p>\n<p>\u4ee4\u6211\u60ca\u8bb6\u7684\u662f\uff0c<strong>\u6bcf\u4e2a\u9879\u76ee\u68c0\u7d22\u5927\u7ea6\u9700\u8981 3.6 \u79d2<\/strong>??!!.<\/p>\n<p>\u65e0\u8bba\u4e0b\u9762\u7684\u4ee3\u7801\u5982\u4f55\uff0c\u5bfc\u81f4\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u53ef\u80fd\u662f\u4ec0\u4e48\uff1f\u6211\u5728\u53d1\u7535\u673a\u914d\u7f6e\u4e0a\u7f3a\u5c11\u4ec0\u4e48\u5417\uff1f<\/p>\n<pre>func (db dynamorepository) fetchproducts(c *gin.context, pid string) ([]models.product, error) {\n    output := make([]models.product, 0)\n    chproduct := make(chan *models.product)\n\n    sem := make(chan struct{}, 40000)\n    wait := sync.waitgroup{}\n    wait.add(1)\n    go func (wg *sync.waitgroup) {\n        defer wg.done()\n        for i := 0; i &lt; len(itemids); i++ {\n            p := &lt;-chproduct\n            if p != nil {\n                output = append(output, *p)\n            }\n        }\n        close(chproduct)\n    }(&amp;wait)\n    for index := range itemids {\n        sem &lt;- struct{}{}\n        go func(ix int, s chan struct{}, ch chan *models.product) {\n            defer func() {\n               &lt;- s\n            }()\n            infoslice := strings.split(itemids[ix], \"_\")\n            if len(infoslice) != 2 {\n                ch &lt;- nil\n                return\n            }\n            key := map[string]*dynamodb.attributevalue{\n                \"pk\": {\n                    n: aws.string(pid),\n                },\n                \"sk\": {\n                    s: aws.string(\"product\"),\n                },\n            }\n            start := time.now()\n            result, err := db.db.getbykey(c, db.config.aws.table, key)\n            fmt.printf(\"item %s took %d\\n\", productid, time.since(start).milliseconds())\n            if err != nil {\n                ch &lt;- nil\n                return\n            }\n            if result.item == nil {\n                ch &lt;- nil\n                return\n            }\n            var product models.product\n            err = dynamodbattribute.unmarshalmap(result.item, &amp;product)\n            if err != nil {\n                ch &lt;- nil\n                return\n            }\n            ch &lt;- &amp;product\n        }(index, sem, chproduct)\n    }\n    wait.wait()\n\n    if len(output) == 0 {\n        return nil, nil\n    }\n    return output, nil\n}<\/pre>\n<p>getbykey\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre>func (provider *DynamoDBProvider) GetByKey(ctx context.Context, tableName string, key map[string]*dynamodb.AttributeValue) (*dynamodb.GetItemOutput, error) {\n    input := &amp;dynamodb.GetItemInput{\n        TableName: aws.String(tableName),\n        Key:       key,\n    }\n\n    result, err := provider.client.GetItem(input)\n\n    return result, err\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u5c31\u662f\u6211\u6700\u7ec8\u6240\u505a\u7684\uff0c\u5b83\u6781\u5927\u5730\u6539\u5584\u4e86\u5ef6\u8fdf\u3002\u6211\u6309\u7167\u672c\u6307\u5357\u521b\u5efa\u4e86\u81ea\u5b9a\u4e49 HTTP \u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p>\u5ef6\u8fdf\u4ece\u8fd1 4 \u79d2\u53d8\u4e3a 500 \u6beb\u79d2~\u3002\u4e3a\u4ec0\u4e48\uff1f\u6211\u4e0d\u77e5\u9053\uff0c\u4f46\u5b83\u6709\u6548\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aDynamoDB \u82b1\u8d39\u592a\u591a\u65f6\u95f4\u6765\u54cd\u5e94\u5355\u4e2a\u9879\u76ee\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208136","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208136"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208136\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}