Membuat jenis penghitung dan sharding kustom untuk penghitung terdistribusi Firestore
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Membuat jenis penghitung dan sharding kustom untuk penghitung terdistribusi Firestore
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]