Créer des types de partitions et de compteurs personnalisés pour les compteurs distribués Firestore
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Créer des types de partitions et de compteurs personnalisés pour les compteurs distribués Firestore
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]