Benutzerdefinierte Shard- und Zählertypen für verteilte Firestore-Zähler erstellen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Benutzerdefinierte Shard- und Zählertypen für verteilte Firestore-Zähler erstellen
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]