{"id":207358,"date":"2025-07-08T11:55:13","date_gmt":"2025-07-08T03:55:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207358\/"},"modified":"2025-07-08T11:55:13","modified_gmt":"2025-07-08T03:55:13","slug":"google-cloud-datastore-%e6%8f%90%e4%ba%a4%e5%86%b2%e7%aa%81%e6%a3%80%e6%b5%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207358\/","title":{"rendered":"Google Cloud Datastore \u63d0\u4ea4\u51b2\u7a81\u68c0\u6d4b"},"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>Google Cloud Datastore \u63d0\u4ea4\u51b2\u7a81\u68c0\u6d4b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 22:18:41<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGoogle Cloud Datastore \u63d0\u4ea4\u51b2\u7a81\u68c0\u6d4b\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728\u6570\u636e\u5b58\u50a8\u6a21\u5f0f\u4e0b\u4f7f\u7528 Firestore\u3002\u6211\u6ca1\u6709\u4f7f\u7528 Google \u6216\u4efb\u4f55\u4eba\u63d0\u4f9b\u7684\u5ba2\u6237\u7aef\u5e93\uff0c\u800c\u662f\u901a\u8fc7 <code>proto grpc<\/code> \u5e93\uff08\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u751f\u6210\u7684\u5e93\uff09\u751f\u6210\uff08\u7531 Google\uff09\u76f4\u63a5\u4e0e\u6570\u636e\u5b58\u50a8\u8fdb\u884c\u901a\u4fe1\u3002\u6211\u4f7f\u7528\u5b83\u662f\u56e0\u4e3a\u9ad8\u7ea7\u5ba2\u6237\u7aef\u4e0d\u5e94\u8be5\u4f7f\u7528\u5b9e\u4f53\u7248\u672c\u548c\u51b2\u7a81\u68c0\u6d4b\u673a\u5236\u3002<\/p>\n<p>\u6211\u6b63\u5728\u6267\u884c\u4e8b\u52a1\u66f4\u65b0\u5e76\u6307\u5b9a\u51b2\u7a81\u68c0\u6d4b\u7b56\u7565\u57fa\u672c\u7248\u672c\uff08\u6bcf\u4e2a\u5b9e\u4f53\u53ef\u80fd\u5305\u542b\u5148\u524d\u5df2\u77e5\u7684\u7248\u672c\uff09\u3002<\/p>\n<p><strong>\u95ee\u9898\u662f<\/strong>\uff0c\u5f53\u4efb\u4f55\u5b9e\u4f53\u4e2d\u5b58\u5728\u7248\u672c\u51b2\u7a81\u65f6\uff0c\u6574\u4e2a\u63d0\u4ea4\u64cd\u4f5c\u4f1a\u5931\u8d25\u5417\uff1f\u5982\u679c\u5b83\u6ca1\u6709\u5931\u8d25\uff0c\u8fd9\u6837\u7684\u63d0\u4ea4\u662f\u5426\u4f1a\u5e94\u7528\u5728\u6570\u636e\u5b58\u50a8\u4e2d\uff0c\u5e76\u4e14\u6211\u4e0d\u4f1a\u901a\u8fc7\u7b80\u5355\u7684\u4e8b\u52a1\u56de\u6eda\u8c03\u7528\u8fdb\u884c\u56de\u6eda\u3002<\/p>\n<p>\u6211\u662f\u5426\u4ecd\u9700\u8981\u901a\u8fc7\u5355\u72ec\u8c03\u7528\u6570\u636e\u5b58\u50a8\u533a\u5728\u8be5\u4e8b\u52a1\u4e4b\u5916\u5b9e\u73b0\u81ea\u5df1\u7684\u72ec\u7acb\u56de\u6eda\uff1f<\/p>\n<p><em>\u7a81\u53d8\u4ea7\u751f<\/em><\/p>\n<p><em>\u627f\u8bfa\u5e76\u51b3\u5b9a\u4e0b\u4e00\u6b65\u505a\u4ec0\u4e48<\/em><\/p>\n<p>\u6211\u5df2\u7ecf\u6d4f\u89c8\u4e86\u6587\u6863\uff0c\u4f46\u8fd9\u4e2a\u95ee\u9898\u8fd8\u8fdc\u672a\u5b8c\u6210\u3002\u8fd9\u662f\u6211\u552f\u4e00\u627e\u5230\u7684\u4e1c\u897f\u3002<\/p>\n<p>https:\/\/cloud.google.com\/datastore\/docs\/reference\/data\/rpc\/google.datastore.v1#google.datastore.v1.MutationResult<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6570\u636e\u5b58\u50a8\u5bf9\u5355\u4e2a\u4e8b\u52a1\u5185\u53ef\u4ee5\u5b8c\u6210\u7684\u64cd\u4f5c\u65bd\u52a0\u9650\u5236\u3002\u901a\u5e38\uff0c\u5f53\u5b58\u5728\u63d0\u4ea4\u51b2\u7a81\u65f6\uff0c\u5e76\u53d1\u4e8b\u52a1\/\u4fee\u6539\u4f1a\u5f97\u5230\u56de\u590d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u542f\u52a8\u4e24\u4e2a\u5199\u5165\u540c\u4e00\u6587\u6863\u7684\u4e8b\u52a1\u3001\u63d0\u4ea4\u5e76\u67e5\u770b\u9884\u671f\u884c\u4e3a\u6765\u91cd\u73b0\u6b64\u60c5\u51b5\u3002<\/p>\n<p>\uff1a<\/p>\n<p>\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u4e8b\u52a1\u540c\u65f6\u5c1d\u8bd5\u4fee\u6539\u4e00\u4e2a\u6216\u591a\u4e2a\u516c\u5171\u5b9e\u4f53\u7ec4\u4e2d\u7684\u5b9e\u4f53\u65f6\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u63d0\u4ea4\u5176\u66f4\u6539\u7684\u4e8b\u52a1\u624d\u80fd\u6210\u529f\uff1b\u6240\u6709\u5176\u4ed6\u4eba\u90fd\u4f1a\u5728\u63d0\u4ea4\u65f6\u5931\u8d25\u3002\u7531\u4e8e\u8fd9\u79cd\u8bbe\u8ba1\uff0c\u4f7f\u7528\u5b9e\u4f53\u7ec4\u4f1a\u9650\u5236\u60a8\u53ef\u4ee5\u5bf9\u7ec4\u4e2d\u7684\u4efb\u4f55\u5b9e\u4f53\u6267\u884c\u7684\u5e76\u53d1\u5199\u5165\u6570\u91cf\u3002\u5f53\u4e8b\u52a1\u542f\u52a8\u65f6\uff0cdatastore \u901a\u8fc7\u68c0\u67e5\u4e8b\u52a1\u4e2d\u4f7f\u7528\u7684\u5b9e\u4f53\u7ec4\u7684\u4e0a\u6b21\u66f4\u65b0\u65f6\u95f4\u6765\u4f7f\u7528\u4e50\u89c2\u5e76\u53d1\u63a7\u5236\u3002\u63d0\u4ea4\u5b9e\u4f53\u7ec4\u4e8b\u52a1\u540e\uff0c\u6570\u636e\u5b58\u50a8\u533a\u4f1a\u518d\u6b21\u68c0\u67e5\u4e8b\u52a1\u4e2d\u4f7f\u7528\u7684\u5b9e\u4f53\u7ec4\u7684\u4e0a\u6b21\u66f4\u65b0\u65f6\u95f4\u3002\u5982\u679c\u81ea\u521d\u59cb\u68c0\u67e5\u4ee5\u6765\u5b83\u5df2\u66f4\u6539\uff0c\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<p>\u6700\u597d\u7684\u65b9\u6cd5\u662f\u5feb\u901f\u91cd\u73b0\u5b83\u5e76\u67e5\u770b\u786e\u5207\u7684\u884c\u4e3a\u3002\u95ee\u9898\u662f\uff0c\u5f53\u4efb\u4f55\u5b9e\u4f53\u4e2d\u5b58\u5728\u7248\u672c\u51b2\u7a81\u65f6\uff0c\u6574\u4e2a\u63d0\u4ea4\u64cd\u4f5c\u4f1a\u5931\u8d25\u5417\uff1f<\/p>\n<p>\u662f\u7684\u3002<\/p>\n<p>\u5305\u88c5\u5668\u5c06\u81ea\u52a8\u91cd\u8bd5\u60a8\u7684\u4ea4\u6613\u3002\u8fd0\u884c\u4e8b\u52a1\u5e93\u7684 <code>golang<\/code> \u6570\u636e\u5b58\u50a8\u5e94\u8be5\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u5176\u884c\u4e3a\u65b9\u5f0f\u3002\u6709\u5173\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u67e5\u770b\u4e0b\u9762\u7684 \u5e76\u4e86\u89e3\u5b83\u5982\u4f55\u8bc4\u4f30\u4ea4\u6613\u3002<\/p>\n<pre>func (c *Client) RunInTransaction(ctx context.Context, f func(tx *Transaction) error, opts ...TransactionOption) (cmt *Commit, err error) {\n        ctx = trace.StartSpan(ctx, \"cloud.google.com\/go\/datastore.RunInTransaction\")\n        defer func() { trace.EndSpan(ctx, err) }()\n     \n        settings := newTransactionSettings(opts)\n        for n := 0; n &lt; settings.attempts; n++ {\n     tx, err := c.newTransaction(ctx, settings)\n     if err != nil {\n     return nil, err\n     }\n     if err := f(tx); err != nil {\n     _ = tx.Rollback()\n     return nil, err\n     }\n     if cmt, err := tx.Commit(); err != ErrConcurrentTransaction {\n     return cmt, err\n     }\n     \/\/ Pass this transaction's ID to the retry transaction to preserve\n     \/\/ transaction priority.\n     if !settings.readOnly {\n     settings.prevID = tx.id\n     }\n        }\n        return nil, ErrConcurrentTransaction\n    }<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGoogle Cloud Datastore \u63d0\u4ea4\u51b2\u7a81\u68c0\u6d4b\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\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-207358","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207358","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=207358"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207358\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}