בדף הזה מוצגות דוגמאות לשימוש ב-Dataflow כדי לבצע פעולות בכמות גדולה ב-Cloud Firestore בצינור עיבוד נתונים של Apache Beam. Apache Beam תומך במחבר ל-Cloud Firestore. אתם יכולים להשתמש במחבר הזה כדי להריץ פעולות באצווה ובסטרימינג ב-Dataflow.
מומלץ להשתמש ב-Dataflow וב-Apache Beam לעומסי עבודה של עיבוד נתונים בקנה מידה גדול.
המחבר של Cloud Firestore ל-Apache Beam זמין ב-Java. מידע נוסף על המחבר של Cloud Firestore זמין במאמר Apache Beam SDK for Java.
לפני שמתחילים
לפני שאתם קוראים את הדף הזה, כדאי שתכירו את מודל התכנות של Apache Beam.
כדי להריץ את הדוגמאות, צריך להפעיל את Dataflow API.דוגמאות לצינורות עיבוד נתונים ב-Cloud Firestore
הדוגמאות הבאות ממחישות צינור עיבוד נתונים שכותב נתונים וצינור עיבוד נתונים שקורא ומסנן נתונים. אתם יכולים להשתמש בדוגמאות האלה כנקודת התחלה לצינורות עיבוד נתונים משלכם.
הרצת צינורות עיבוד הנתונים לדוגמה
קוד המקור של הדוגמאות זמין במאגר 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
תמחור
הפעלת עומס עבודה של Cloud Firestore ב-Dataflow כרוכה בעלויות של שימוש ב-Cloud Firestore וב-Dataflow. החיוב על השימוש ב-Dataflow הוא על המשאבים שבהם משתמשים במשימות. לפרטים נוספים, קראו את דף התמחור של Dataflow. למידע על התמחור של Cloud Firestore, קראו את דף התמחור.
המאמרים הבאים
- דוגמה נוספת לצינור עיבוד נתונים מופיעה במאמר שימוש ב-Firestore וב-Apache Beam לעיבוד נתונים.
- מידע נוסף על Dataflow ו-Apache Beam זמין במסמכי התיעוד של Dataflow.