বিবরণ
আগের ধাপের ফলাফল থেকে একটি অনির্ধারক স্যাম্পেল দেখায়।
দুটি মোড কাজ করে:
documents: র্যান্ডমভাবেnটি ডকুমেন্ট বেছে নিন।percent: র্যান্ডমভাবেnশতাংশ ডকুমেন্ট বেছে নিন।
উদাহরণ
Web
let results; // Get a sample of 100 documents in a database results = await execute(db.pipeline() .database() .sample(100) ); // Randomly shuffle a list of 3 documents results = await execute(db.pipeline() .documents([ doc(db, "cities", "SF"), doc(db, "cities", "NY"), doc(db, "cities", "DC"), ]) .sample(3) );
Swift
var results: Pipeline.Snapshot // Get a sample of 100 documents in a database results = try await db.pipeline() .database() .sample(count: 100) .execute() // Randomly shuffle a list of 3 documents results = try await db.pipeline() .documents([ db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC"), ]) .sample(count: 3) .execute()
Kotlin
var results: Task<Pipeline.Snapshot> // Get a sample of 100 documents in a database results = db.pipeline() .database() .sample(100) .execute() // Randomly shuffle a list of 3 documents results = db.pipeline() .documents( db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC") ) .sample(3) .execute()
Java
Task<Pipeline.Snapshot> results; // Get a sample of 100 documents in a database results = db.pipeline() .database() .sample(100) .execute(); // Randomly shuffle a list of 3 documents results = db.pipeline() .documents( db.collection("cities").document("SF"), db.collection("cities").document("NY"), db.collection("cities").document("DC") ) .sample(3) .execute();
Python
# Get a sample of 100 documents in a database results = client.pipeline().database().sample(100).execute() # Randomly shuffle a list of 3 documents results = ( client.pipeline() .documents( client.collection("cities").document("SF"), client.collection("cities").document("NY"), client.collection("cities").document("DC"), ) .sample(3) .execute() )
জাভা
// Get a sample of 100 documents in a database Pipeline.Snapshot results1 = firestore.pipeline().database().sample(100).execute().get(); // Randomly shuffle a list of 3 documents Pipeline.Snapshot results2 = firestore .pipeline() .documents( firestore.collection("cities").document("SF"), firestore.collection("cities").document("NY"), firestore.collection("cities").document("DC")) .sample(3) .execute() .get();
Go
// Get a sample of 100 documents in a database results1, err := client.Pipeline().Database().Sample(firestore.WithDocLimit(100)).Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err } // Randomly shuffle a list of 3 documents results2, err := client.Pipeline(). Documents([]*firestore.DocumentRef{ client.Collection("cities").Doc("SF"), client.Collection("cities").Doc("NY"), client.Collection("cities").Doc("DC"), }). Sample(firestore.WithDocLimit(3)). Execute(ctx).Results().GetAll() if err != nil { fmt.Fprintf(w, "GetAll failed: %v", err) return err }
মোড
ডকুমেন্ট মোড
documents মোড ইনপুট থেকে সর্বাধিক nটি ডকুমেন্ট র্যান্ডম বেছে নেয়, যেখানে
প্রতিটি ডকুমেন্ট (ডকুমেন্টের ক্রম সহ) বেছে নেওয়ার সম্ভাবনা সমান থাকে। এটি করার জন্য, Cloud Firestore-কে এখনও সব ডকুমেন্ট স্ক্যান ও প্রসেস করতে হবে, তাই এটি এখনও একটি ব্যয়বহুল অপারেশন হতে পারে।
যেমন, নিম্নলিখিত সংগ্রহের জন্য:
Node.js
await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"});
await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"});
await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"});
ডকুমেন্ট মোডে স্যাম্পেল স্টেজ ব্যবহার করে এই কালেকশন থেকে ফলাফলের একটি নন-ডিটারমিনিস্টিক সাবসেট ফিরিয়ে আনা যেতে পারে।
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample(1)
.execute();
এই উদাহরণে, র্যান্ডমভাবে মাত্র ১টি ডকুমেন্ট ফেরত দেওয়া হবে।
{ name: "New York City", state: "New York" }
প্রদান করা সংখ্যাটি যদি ফেরত আসা ডকুমেন্টের মোট সংখ্যার চেয়ে বেশি হয়, তাহলে সব ডকুমেন্ট র্যান্ডম ক্রমে ফেরত দেওয়া হয়।
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample(5)
.execute();
এর ফলে নিম্নলিখিত ডকুমেন্ট তৈরি হবে:
{ name: "New York City", state: "New York" }
{ name: "Chicago", state: "Illinois" }
{ name: "San Francisco", state: "California" }
শতাংশ মোড
percent মোড তার ইনপুট থেকে সব ডকুমেন্টের n শতাংশ বেছে নেওয়ার চেষ্টা করে।
এর ফলে এই স্টেজে আনুমানিক # documents * percent / 100টি
ডকুমেন্ট তৈরি হয়। documents মোডের মতোই, Cloud Firestore নিশ্চিত করে যে
প্রতিটি ডকুমেন্ট সমানভাবে ফেরত পাওয়ার সম্ভাবনা রয়েছে। এর জন্য
Cloud Firestore-কে সব ডকুমেন্ট স্ক্যান ও প্রসেস করতে হয়, তাই
ফলাফল সেট ছোট হলেও এটি একটি ব্যয়বহুল অপারেশন হতে পারে।
documents মোডের মতো নয়, এখানে ক্রমটি র্যান্ডম নয় এবং পরিবর্তে আগে থেকে থাকা ডকুমেন্টের ক্রম
সংরক্ষণ করে। এই শতাংশ ইনপুট অবশ্যই 0.0 এবং 1.0-এর মধ্যে
ডবল ভ্যালু হতে হবে।
যেমন, নিম্নলিখিত সংগ্রহের জন্য:
Node.js
await db.collection("cities").doc("SF").set({name: "San Francsico", state: "California"});
await db.collection("cities").doc("NYC").set({name: "New York City", state: "New York"});
await db.collection("cities").doc("CHI").set({name: "Chicago", state: "Illinois"});
await db.collection("cities").doc("ATL").set({name: "Atlanta", state: "Georgia"});
collection(...) স্টেজ থেকে (গড়ে) ৫০% ডকুমেন্ট ফিরিয়ে আনতে শতাংশ মোডে স্যাম্পেল স্টেজ ব্যবহার করা যেতে পারে।
Node.js
const sampled = await db.pipeline()
.collection("/cities")
.sample({ percent: 0.5 })
.execute();
এর ফলে cities সংগ্রহ থেকে (গড়ে) ৫০% ডকুমেন্টের
একটি অনির্ধারক স্যাম্পেল তৈরি হবে। নিম্নলিখিতটি হল একটি সম্ভাব্য আউটপুট।
{ name: "New York City", state: "New York" }
{ name: "Chicago", state: "Illinois" }
শতাংশ মোডে, প্রতিটি ডকুমেন্ট বেছে নেওয়ার সম্ভাবনা একই থাকে, তাই কোনও ডকুমেন্ট বা সব ডকুমেন্ট ফেরত পাওয়ার সম্ভাবনা থাকে।
ক্লায়েন্টের উদাহরণ
Web
// Get a sample of on average 50% of the documents in the database const results = await execute(db.pipeline() .database() .sample({ percentage: 0.5 }) );
Swift
// Get a sample of on average 50% of the documents in the database let results = try await db.pipeline() .database() .sample(percentage: 0.5) .execute()
Kotlin
// Get a sample of on average 50% of the documents in the database val results = db.pipeline() .database() .sample(SampleStage.withPercentage(0.5)) .execute()
Java
// Get a sample of on average 50% of the documents in the database Task<Pipeline.Snapshot> results = db.pipeline() .database() .sample(SampleStage.withPercentage(0.5)) .execute();
Python
from google.cloud.firestore_v1.pipeline_stages import SampleOptions # Get a sample of on average 50% of the documents in the database results = ( client.pipeline().database().sample(SampleOptions.percentage(0.5)).execute() )
জাভা
// Get a sample of on average 50% of the documents in the database Pipeline.Snapshot results = firestore.pipeline().database().sample(Sample.withPercentage(0.5)).execute().get();
Go
// Get a sample of on average 50% of the documents in the database snapshot := client.Pipeline(). Database(). Sample(firestore.WithPercentage(0.5)). Execute(ctx)