|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.dataflow; |
| 18 | + |
| 19 | +// [START dataflow_apache_iceberg_cdc_read] |
| 20 | +import com.google.auth.oauth2.GoogleCredentials; |
| 21 | +import com.google.common.collect.ImmutableMap; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Map; |
| 24 | +import org.apache.beam.sdk.Pipeline; |
| 25 | +import org.apache.beam.sdk.coders.RowCoder; |
| 26 | +import org.apache.beam.sdk.extensions.gcp.options.GcpOptions; |
| 27 | +import org.apache.beam.sdk.managed.Managed; |
| 28 | +import org.apache.beam.sdk.options.Default; |
| 29 | +import org.apache.beam.sdk.options.Description; |
| 30 | +import org.apache.beam.sdk.options.PipelineOptionsFactory; |
| 31 | +import org.apache.beam.sdk.options.Validation; |
| 32 | +import org.apache.beam.sdk.schemas.Schema; |
| 33 | +import org.apache.beam.sdk.transforms.MapElements; |
| 34 | +import org.apache.beam.sdk.transforms.Sum; |
| 35 | +import org.apache.beam.sdk.transforms.windowing.FixedWindows; |
| 36 | +import org.apache.beam.sdk.transforms.windowing.Window; |
| 37 | +import org.apache.beam.sdk.values.KV; |
| 38 | +import org.apache.beam.sdk.values.PCollection; |
| 39 | +import org.apache.beam.sdk.values.Row; |
| 40 | +import org.apache.beam.sdk.values.TypeDescriptors; |
| 41 | +import org.joda.time.Duration; |
| 42 | + |
| 43 | +/** |
| 44 | + * A streaming pipeline that reads CDC events from an Iceberg table, aggregates user clicks, and |
| 45 | + * writes the results to another Iceberg table. |
| 46 | + * |
| 47 | + * <p>This pipeline can be used to process the output of {@link |
| 48 | + * ApacheIcebergRestCatalogStreamingWrite}. |
| 49 | + */ |
| 50 | +public class ApacheIcebergCdcRead { |
| 51 | + |
| 52 | + // Schema for the source table containing click events. |
| 53 | + public static final Schema SOURCE_SCHEMA = |
| 54 | + Schema.builder().addStringField("user_id").addInt64Field("click_count").build(); |
| 55 | + |
| 56 | + // Schema for the destination table containing aggregated click counts. |
| 57 | + public static final Schema DESTINATION_SCHEMA = |
| 58 | + Schema.builder().addStringField("user_id").addInt64Field("total_clicks").build(); |
| 59 | + |
| 60 | + /** Pipeline options for this example. */ |
| 61 | + public interface Options extends GcpOptions { |
| 62 | + @Description("The source Iceberg table to read CDC events from") |
| 63 | + @Validation.Required |
| 64 | + String getSourceTable(); |
| 65 | + |
| 66 | + void setSourceTable(String sourceTable); |
| 67 | + |
| 68 | + @Description("The destination Iceberg table to write aggregated results to") |
| 69 | + @Validation.Required |
| 70 | + String getDestinationTable(); |
| 71 | + |
| 72 | + void setDestinationTable(String destinationTable); |
| 73 | + |
| 74 | + @Description("Warehouse location for the Iceberg catalog") |
| 75 | + @Validation.Required |
| 76 | + String getWarehouse(); |
| 77 | + |
| 78 | + void setWarehouse(String warehouse); |
| 79 | + |
| 80 | + @Description("The URI for the REST catalog") |
| 81 | + @Default.String("https://biglake.googleapis.com/iceberg/v1beta/restcatalog") |
| 82 | + String getCatalogUri(); |
| 83 | + |
| 84 | + void setCatalogUri(String value); |
| 85 | + |
| 86 | + @Description("The name of the Iceberg catalog") |
| 87 | + @Validation.Required |
| 88 | + String getCatalogName(); |
| 89 | + |
| 90 | + void setCatalogName(String catalogName); |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) throws IOException { |
| 94 | + Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); |
| 95 | + |
| 96 | + Map<String, String> catalogProps = |
| 97 | + ImmutableMap.<String, String>builder() |
| 98 | + .put("type", "rest") |
| 99 | + .put("uri", options.getCatalogUri()) |
| 100 | + .put("warehouse", options.getWarehouse()) |
| 101 | + .put("header.x-goog-user-project", options.getProject()) |
| 102 | + .put( |
| 103 | + "header.Authorization", |
| 104 | + "Bearer " |
| 105 | + + GoogleCredentials.getApplicationDefault() |
| 106 | + .createScoped("https://www.googleapis.com/auth/cloud-platform") |
| 107 | + .refreshAccessToken() |
| 108 | + .getTokenValue()) |
| 109 | + .put("rest-metrics-reporting-enabled", "false") |
| 110 | + .build(); |
| 111 | + |
| 112 | + Pipeline p = Pipeline.create(options); |
| 113 | + |
| 114 | + // Configure the Iceberg CDC read |
| 115 | + Map<String, Object> icebergReadConfig = |
| 116 | + ImmutableMap.<String, Object>builder() |
| 117 | + .put("table", options.getSourceTable()) |
| 118 | + .put("catalog_name", options.getCatalogName()) |
| 119 | + .put("catalog_properties", catalogProps) |
| 120 | + .put("streaming", Boolean.TRUE) |
| 121 | + .put("poll_interval_seconds", 20) |
| 122 | + .build(); |
| 123 | + |
| 124 | + PCollection<Row> cdcEvents = |
| 125 | + p.apply("ReadFromIceberg", Managed.read(Managed.ICEBERG_CDC).withConfig(icebergReadConfig)) |
| 126 | + .getSinglePCollection() |
| 127 | + .setRowSchema(SOURCE_SCHEMA); |
| 128 | + |
| 129 | + PCollection<Row> aggregatedRows = |
| 130 | + cdcEvents |
| 131 | + .apply("ApplyWindow", Window.into(FixedWindows.of(Duration.standardSeconds(30)))) |
| 132 | + .apply( |
| 133 | + "ExtractUserAndCount", |
| 134 | + MapElements.into( |
| 135 | + TypeDescriptors.kvs(TypeDescriptors.strings(), TypeDescriptors.longs())) |
| 136 | + .via( |
| 137 | + row -> { |
| 138 | + String userId = row.getString("user_id"); |
| 139 | + Long clickCount = row.getInt64("click_count"); |
| 140 | + return KV.of(userId, clickCount == null ? 0L : clickCount); |
| 141 | + })) |
| 142 | + .apply("SumClicksPerUser", Sum.longsPerKey()) |
| 143 | + .apply( |
| 144 | + "FormatToRow", |
| 145 | + MapElements.into(TypeDescriptors.rows()) |
| 146 | + .via( |
| 147 | + kv -> |
| 148 | + Row.withSchema(DESTINATION_SCHEMA) |
| 149 | + .withFieldValue("user_id", kv.getKey()) |
| 150 | + .withFieldValue("total_clicks", kv.getValue()) |
| 151 | + .build())) |
| 152 | + .setCoder(RowCoder.of(DESTINATION_SCHEMA)); |
| 153 | + |
| 154 | + // Configure the Iceberg write |
| 155 | + Map<String, Object> icebergWriteConfig = |
| 156 | + ImmutableMap.<String, Object>builder() |
| 157 | + .put("table", options.getDestinationTable()) |
| 158 | + .put("catalog_properties", catalogProps) |
| 159 | + .put("catalog_name", options.getCatalogName()) |
| 160 | + .put("triggering_frequency_seconds", 30) |
| 161 | + .build(); |
| 162 | + |
| 163 | + aggregatedRows.apply( |
| 164 | + "WriteToIceberg", Managed.write(Managed.ICEBERG).withConfig(icebergWriteConfig)); |
| 165 | + |
| 166 | + p.run(); |
| 167 | + } |
| 168 | +} |
| 169 | +// [END dataflow_apache_iceberg_cdc_read] |
0 commit comments