डेटा प्रवाह के साथ डेटा को बल्क में प्रोसेस करें

यह पृष्ठ अपाचे बीम पाइपलाइन में बल्क क्लाउड फायरस्टोर संचालन करने के लिए डेटाफ़्लो का उपयोग करने के उदाहरण देता है। अपाचे बीम क्लाउड फायरस्टोर के लिए एक कनेक्टर का समर्थन करता है। आप डेटाफ्लो में बैच और स्ट्रीमिंग ऑपरेशन चलाने के लिए इस कनेक्टर का उपयोग कर सकते हैं।

हम बड़े पैमाने पर डेटा प्रोसेसिंग वर्कलोड के लिए डेटाफ्लो और अपाचे बीम का उपयोग करने की सलाह देते हैं।

अपाचे बीम के लिए क्लाउड फायरस्टोर कनेक्टर जावा में उपलब्ध है। क्लाउड फायरस्टोर कनेक्टर के बारे में अधिक जानकारी के लिए, जावा के लिए अपाचे बीम एसडीके देखें।

शुरू करने से पहले

इस पृष्ठ को पढ़ने से पहले, आपको अपाचे बीम के प्रोग्रामिंग मॉडल से परिचित होना चाहिए।

नमूने चलाने के लिए, आपको डेटाफ्लो एपीआई सक्षम करना होगा।

उदाहरण क्लाउड फायरस्टोर पाइपलाइन

नीचे दिए गए उदाहरण एक पाइपलाइन को प्रदर्शित करते हैं जो डेटा लिखती है और एक पाइपलाइन जो डेटा को पढ़ती और फ़िल्टर करती है। आप इन नमूनों को अपनी पाइपलाइनों के लिए शुरुआती बिंदु के रूप में उपयोग कर सकते हैं।

नमूना पाइपलाइन चलाना

नमूनों का स्रोत कोड googleapis/java-firestore GitHub रिपॉजिटरी में उपलब्ध है। इन नमूनों को चलाने के लिए, स्रोत कोड डाउनलोड करें और README देखें।

उदाहरण पाइपलाइन Write

निम्नलिखित उदाहरण cities-beam-sample संग्रह में दस्तावेज़ बनाता है:



public class ExampleFirestoreBeamWrite {
  private static final FirestoreOptions FIRESTORE_OPTIONS = FirestoreOptions.getDefaultInstance();

  public static void main(String[] args) {
    runWrite(args, "cities-beam-sample");
  }

  public static void runWrite(String[] args, String collectionId) {
    // create pipeline options from the passed in arguments
    PipelineOptions options =
        PipelineOptionsFactory.fromArgs(args).withValidation().as(PipelineOptions.class);
    Pipeline pipeline = Pipeline.create(options);

    RpcQosOptions rpcQosOptions =
        RpcQosOptions.newBuilder()
            .withHintMaxNumWorkers(options.as(DataflowPipelineOptions.class).getMaxNumWorkers())
            .build();

    // create some writes
    Write write1 =
        Write.newBuilder()
            .setUpdate(
                Document.newBuilder()
                    // resolves to
                    // projects/<projectId>/databases/<databaseId>/documents/<collectionId>/NYC
                    .setName(createDocumentName(collectionId, "NYC"))
                    .putFields("name", Value.newBuilder().setStringValue("New York City").build())
                    .putFields("state", Value.newBuilder().setStringValue("New York").build())
                    .putFields("country", Value.newBuilder().setStringValue("USA").build()))
            .build();

    Write write2 =
        Write.newBuilder()
            .setUpdate(
                Document.newBuilder()
                    // resolves to
                    // projects/<projectId>/databases/<databaseId>/documents/<collectionId>/TOK
                    .setName(createDocumentName(collectionId, "TOK"))
                    .putFields("name", Value.newBuilder().setStringValue("Tokyo").build())
                    .putFields("country", Value.newBuilder().setStringValue("Japan").build())
                    .putFields("capital", Value.newBuilder().setBooleanValue(true).build()))
            .build();

    // batch write the data
    pipeline
        .apply(Create.of(write1, write2))
        .apply(FirestoreIO.v1().write().batchWrite().withRpcQosOptions(rpcQosOptions).build());

    // run the pipeline
    pipeline.run().waitUntilFinish();
  }

  private static String createDocumentName(String collectionId, String cityDocId) {
    String documentPath =
        String.format(
            "projects/%s/databases/%s/documents",
            FIRESTORE_OPTIONS.getProjectId(), FIRESTORE_OPTIONS.getDatabaseId());

    return documentPath + "/" + collectionId + "/" + cityDocId;
  }
}

उदाहरण पाइपलाइन को कॉन्फ़िगर करने और चलाने के लिए निम्नलिखित तर्कों का उपयोग करता है:

GOOGLE_CLOUD_PROJECT=project-id
REGION=region
TEMP_LOCATION=gs://temp-bucket/temp/
NUM_WORKERS=number-workers
MAX_NUM_WORKERS=max-number-workers

उदाहरण Read पाइपलाइन

निम्नलिखित उदाहरण पाइपलाइन cities-beam-sample संग्रह से दस्तावेजों को पढ़ती है, उन दस्तावेजों के लिए एक फ़िल्टर लागू करती है जहां फ़ील्ड country USA पर सेट है, और मिलान दस्तावेजों के नाम लौटाता है।



public class ExampleFirestoreBeamRead {

  public static void main(String[] args) {
    runRead(args, "cities-beam-sample");
  }

  public static void runRead(String[] args, String collectionId) {
    FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance();

    PipelineOptions options =
        PipelineOptionsFactory.fromArgs(args).withValidation().as(PipelineOptions.class);
    Pipeline pipeline = Pipeline.create(options);

    RpcQosOptions rpcQosOptions =
        RpcQosOptions.newBuilder()
            .withHintMaxNumWorkers(options.as(DataflowPipelineOptions.class).getMaxNumWorkers())
            .build();

    pipeline
        .apply(Create.of(collectionId))
        .apply(
            new FilterDocumentsQuery(
                firestoreOptions.getProjectId(), firestoreOptions.getDatabaseId()))
        .apply(FirestoreIO.v1().read().runQuery().withRpcQosOptions(rpcQosOptions).build())
        .apply(
            ParDo.of(
                // transform each document to its name
                new DoFn<RunQueryResponse, String>() {
                  @ProcessElement
                  public void processElement(ProcessContext c) {
                    c.output(Objects.requireNonNull(c.element()).getDocument().getName());
                  }
                }))
        .apply(
            ParDo.of(
                // print the document name
                new DoFn<String, Void>() {
                  @ProcessElement
                  public void processElement(ProcessContext c) {
                    System.out.println(c.element());
                  }
                }));

    pipeline.run().waitUntilFinish();
  }

  private static final class FilterDocumentsQuery
      extends PTransform<PCollection<String>, PCollection<RunQueryRequest>> {

    private final String projectId;
    private final String databaseId;

    public FilterDocumentsQuery(String projectId, String databaseId) {
      this.projectId = projectId;
      this.databaseId = databaseId;
    }

    @Override
    public PCollection<RunQueryRequest> expand(PCollection<String> input) {
      return input.apply(
          ParDo.of(
              new DoFn<String, RunQueryRequest>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                  // select from collection "cities-collection-<uuid>"
                  StructuredQuery.CollectionSelector collection =
                      StructuredQuery.CollectionSelector.newBuilder()
                          .setCollectionId(Objects.requireNonNull(c.element()))
                          .build();
                  // filter where country is equal to USA
                  StructuredQuery.Filter countryFilter =
                      StructuredQuery.Filter.newBuilder()
                          .setFieldFilter(
                              StructuredQuery.FieldFilter.newBuilder()
                                  .setField(
                                      StructuredQuery.FieldReference.newBuilder()
                                          .setFieldPath("country")
                                          .build())
                                  .setValue(Value.newBuilder().setStringValue("USA").build())
                                  .setOp(StructuredQuery.FieldFilter.Operator.EQUAL))
                          .buildPartial();

                  RunQueryRequest runQueryRequest =
                      RunQueryRequest.newBuilder()
                          .setParent(DocumentRootName.format(projectId, databaseId))
                          .setStructuredQuery(
                              StructuredQuery.newBuilder()
                                  .addFrom(collection)
                                  .setWhere(countryFilter)
                                  .build())
                          .build();
                  c.output(runQueryRequest);
                }
              }));
    }
  }
}

उदाहरण पाइपलाइन को कॉन्फ़िगर करने और चलाने के लिए निम्नलिखित तर्कों का उपयोग करता है:

GOOGLE_CLOUD_PROJECT=project-id
REGION=region
TEMP_LOCATION=gs://temp-bucket/temp/
NUM_WORKERS=number-workers
MAX_NUM_WORKERS=max-number-workers

मूल्य निर्धारण

डेटाफ्लो में क्लाउड फायरस्टोर वर्कलोड चलाने से क्लाउड फायरस्टोर के उपयोग और डेटाफ्लो के उपयोग की लागत आती है। डेटाफ़्लो उपयोग का बिल उन संसाधनों के लिए किया जाता है जिनका उपयोग आपकी नौकरियों में होता है। विवरण के लिए डेटाफ्लो मूल्य निर्धारण पृष्ठ देखें। क्लाउड फायरस्टोर मूल्य निर्धारण के लिए, मूल्य निर्धारण पृष्ठ देखें।

आगे क्या होगा