Firestore 分散カウンタのカスタム シャードとカウンタタイプを作成する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firestore 分散カウンタのカスタム シャードとカウンタタイプを作成する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Go
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
Python
Firestore に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content focuses on creating custom shard and counter types for Firestore distributed counters, which enhance the ability to support frequent updates.\u003c/p\u003e\n"],["\u003cp\u003eIt provides code samples in C#, Go, and Python, demonstrating how to implement \u003ccode\u003eShard\u003c/code\u003e and \u003ccode\u003eCounter\u003c/code\u003e classes or types for managing counts.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eShard\u003c/code\u003e represents a single document containing a count, while the \u003ccode\u003eCounter\u003c/code\u003e is a collection of \u003ccode\u003eShard\u003c/code\u003e documents allowing for high-frequency counter.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for the code samples to access Firestore requires setting up Application Default Credentials, detailed in the linked documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code samples are part of a larger set of solutions for Firestore, with links to further documentation and a code sample browser.\u003c/p\u003e\n"]]],[],null,["# Create custom shard and counter types for Firestore distributed counters\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Distributed counters](https://firebase.google.com/docs/firestore/solutions/counters)\n- [Support frequent and distributed counters](/firestore/native/docs/solutions/counters)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /// \u003csummary\u003e\n /// Shard is a document that contains the count.\n /// \u003c/summary\u003e\n [FirestoreData]\n public class Shard\n {\n [FirestoreProperty(name: \"count\")]\n public int Count { get; set; }\n }\n\n### Go\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"math/rand\"\n \t\"strconv\"\n\n \t\"cloud.google.com/go/firestore\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // Counter is a collection of documents (shards)\n // to realize counter with high frequency.\n type Counter struct {\n \tnumShards int\n }\n\n // Shard is a single counter, which is used in a group\n // of other shards within Counter.\n type Shard struct {\n \tCount int\n }\n\n### Python\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import random\n\n from google.cloud import firestore\n\n\n class Shard:\n \"\"\"\n A shard is a distributed counter. Each shard can support being incremented\n once per second. Multiple shards are needed within a Counter to allow\n more frequent incrementing.\n \"\"\"\n\n def __init__(self):\n self._count = 0\n\n def to_dict(self):\n return {\"count\": self._count}\n\n\n class Counter:\n \"\"\"\n A counter stores a collection of shards which are\n summed to return a total count. This allows for more\n frequent incrementing than a single document.\n \"\"\"\n\n def __init__(self, num_shards):\n self._num_shards = num_shards\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]