Crea tipi di contatori e shard personalizzati per contatori distribuiti Firestore
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Crea tipi di contatori e shard personalizzati per contatori distribuiti Firestore
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]