샘플

설명

이전 단계의 결과에서 비결정적 샘플을 반환합니다.

지원되는 모드는 두 가지입니다.

  • DOCUMENTS 모드에서는 정해진 수의 문서를 샘플링할 수 있습니다.
    • 이 모드는 크기가 n인 샘플을 출력한다는 점에서 GoogleSQL.RESERVOIR와 유사하며, 크기가 n인 모든 샘플이 동일하게 가능합니다.
  • PERCENT 모드에서는 일정 비율의 문서를 샘플링할 수 있습니다.
    • 이 모드는 각 문서가 동일한 percent 확률로 독립적으로 선택된다는 점에서 GoogleSQL.BERNOULLI와 유사합니다. 따라서 평균적으로 #documents * percent / 100개의 문서가 반환됩니다.

구문

Node.js

  const sampled = await db.pipeline()
    .database()
    .sample(50)
    .execute();

  const sampled = await db.pipeline()
    .database()
    .sample({ percent: 0.5 })
    .execute();

동작

문서 모드

문서 모드는 지정된 수의 문서를 무작위 순서로 가져옵니다. 지정된 숫자는 음이 아닌 INT64 값이어야 합니다.

다음 컬렉션을 예로 들어보겠습니다.

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();

이 예에서는 무작위로 문서 1개만 반환됩니다.

  {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'}

고객 예시:

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();

비율 모드

비율 모드에서는 각 문서가 반환될 확률이 percent로 지정됩니다. 문서 모드와 달리 순서는 무작위가 아니며 기존 문서 순서를 유지합니다. 이 비율 입력은 0.0에서 1.0 사이의 double 값이어야 합니다.

각 문서는 독립적으로 선택되므로 출력은 비결정적이며 평균적으로 #documents * percent / 100개의 문서가 반환됩니다.

다음 컬렉션을 예로 들어보겠습니다.

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'});

비율 모드의 샘플 단계는 컬렉션 단계에서 문서의 50%(평균)를 가져오는 데 사용할 수 있습니다.

Node.js

  const sampled = await db.pipeline()
    .collection("/cities")
    .sample({ percent: 0.5 })
    .execute();

이렇게 하면 cities 컬렉션의 문서 중 (평균적으로) 50%가 비결정적 샘플로 추출됩니다. 다음은 가능한 출력 중 하나입니다.

  {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();