nitinware commented on code in PR #40648: URL: https://github.com/apache/beam/pull/40648#discussion_r4763444964
########## sdks/java/io/couchbase/src/main/java/org/apache/beam/sdk/io/couchbase/CouchbaseIO.java: ########## @@ -0,0 +1,734 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.io.couchbase; + +import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull; +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; + +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.Cluster; +import com.couchbase.client.java.ClusterOptions; +import com.couchbase.client.java.ReactiveCollection; +import com.couchbase.client.java.json.JsonArray; +import com.couchbase.client.java.json.JsonObject; +import com.couchbase.client.java.query.QueryOptions; +import com.couchbase.client.java.query.QueryScanConsistency; +import com.couchbase.client.java.query.ReactiveQueryResult; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.auto.value.AutoValue; +import java.io.Serializable; +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.apache.beam.sdk.coders.CannotProvideCoderException; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.CoderRegistry; +import org.apache.beam.sdk.coders.RowCoder; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.Impulse; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.Redistribute; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.transforms.SerializableFunctions; +import org.apache.beam.sdk.transforms.display.DisplayData; +import org.apache.beam.sdk.util.RowJson.RowJsonDeserializer; +import org.apache.beam.sdk.util.RowJsonUtils; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PBegin; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.PDone; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TypeDescriptors; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * IO to read and write on <a href="https://www.couchbase.com/">Couchbase</a>. + * + * <p>Supports Couchbase Server 8.0 and later, using the Couchbase Java SDK 3.x. Use a {@code + * couchbases://} connection string to connect over TLS. + * + * <h3>Reading from Couchbase</h3> + * + * <p>{@link CouchbaseIO#read()} runs a SQL++ query and emits each result row as a {@link + * JsonObject}. If the {@link ConnectionConfiguration} sets a bucket, the query runs in its scope + * and can name collections without qualifying them: + * + * <pre>{@code + * PCollection<JsonObject> airlines = + * pipeline.apply(CouchbaseIO.read() + * .withConnectionConfiguration( + * CouchbaseIO.ConnectionConfiguration.create("couchbase://host") + * .withUsername(username) + * .withPassword(password) + * .withBucket("travel") + * .withScope("inventory")) + * .withQuery("SELECT a.* FROM airline a WHERE a.country = $country") + * .withNamedParameter("country", "United Kingdom")); + * }</pre> + * + * <p>Pass values with {@link Read#withNamedParameter} or {@link Read#withPositionalParameters} + * rather than building them into the query string. Use {@link Read#withScanConsistency} with {@link + * QueryScanConsistency#REQUEST_PLUS} when the read must see all prior mutations. + * + * <p>Use {@link Read#withParseFn} to convert each row to another type, and {@link Read#withCoder} + * to set its coder when Beam can't infer one: + * + * <pre>{@code + * PCollection<String> names = + * pipeline.apply(CouchbaseIO.read() + * .withConnectionConfiguration(config) + * .withQuery("SELECT a.name FROM airline a") + * .withParseFn(row -> row.getString("name")) + * .withCoder(StringUtf8Coder.of())); + * }</pre> + * + * <p>{@link CouchbaseIO#readRows(Schema)} emits {@link Row}s with the given schema, so the output + * can be used with Beam SQL and other schema-aware transforms. Fields not in the schema are + * ignored; a field missing from a row is null if the schema declares it nullable, otherwise the + * read fails. + * + * <p>The query runs as a single request on one worker, because the client can't split a SQL++ + * query. Use {@link Read#withRedistribute} to spread the output across workers for the steps that + * follow. If the read is retried, the query runs again from the start; with the default scan + * consistency the retry may see a different result. + * + * <p>The user needs the Query Select role on the collections the query reads. + * + * <h3>Writing to Couchbase</h3> + * + * <p>{@link CouchbaseIO#write()} upserts each element of a {@link PCollection} as a JSON document + * into a collection. You provide a {@link ConnectionConfiguration}, a function that returns the + * document ID for an element, and a function that returns its content as a {@link JsonObject}: + * + * <pre>{@code + * pipeline + * .apply(...) + * .apply(CouchbaseIO.<Airline>write() + * .withConnectionConfiguration( + * CouchbaseIO.ConnectionConfiguration.create("couchbase://host") + * .withUsername(username) + * .withPassword(password) + * .withBucket("travel") + * .withScope("inventory") + * .withCollection("airline")) + * .withIdFn(airline -> airline.getId()) + * .withContentFn(airline -> JsonObject.create().put("name", airline.getName()))); Review Comment: Renamed withContentFn() to withFormatFunction() and updated the implementation, examples, and tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
