fapaul commented on a change in pull request #17363: URL: https://github.com/apache/flink/pull/17363#discussion_r718239254
########## File path: flink-connectors/flink-connector-elasticsearch7/src/main/java/org/apache/flink/connector/elasticsearch/sink/ElasticsearchSinkBuilder.java ########## @@ -0,0 +1,276 @@ +/* + * 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.flink.connector.elasticsearch.sink; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.connector.base.DeliveryGuarantee; +import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkBase; +import org.apache.flink.util.InstantiationUtil; + +import org.apache.http.HttpHost; + +import java.util.Arrays; +import java.util.List; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Builder to construct a {@link ElasticsearchSink}. + * + * <p>The following example shows the minimum setup to create a ElasticsearchSink that submits + * actions on checkpoint. + * + * <pre>{@code + * Elasticsearch<String> sink = Elasticsearch + * .builder() + * .setHosts(MY_ELASTICSEARCH_HOSTS) + * .setProcessor(MY_ELASTICSEARCH_PROCESSOR) + * .setDeliveryGuarantee(DeliveryGuarantee.AT_LEAST_ONCE) + * .build(); + * }</pre> + * + * @param <IN> type of the records converted to Elasticsearch actions + */ +@PublicEvolving +public class ElasticsearchSinkBuilder<IN> { + + private int bulkFlushMaxActions = -1; + private int bulkFlushMaxMb = -1; + private long bulkFlushInterval = -1; + private ElasticsearchSinkBase.FlushBackoffType bulkFlushBackoffType; + private int bulkFlushBackoffRetries = -1; + private long bulkFlushBackOffDelay = -1; + private DeliveryGuarantee deliveryGuarantee = DeliveryGuarantee.NONE; + private List<HttpHost> hosts; + private ElasticsearchProcessor<? extends IN> processor; + private String username; + private String password; + private String connectionPathPrefix; + + ElasticsearchSinkBuilder() {} + + /** + * Sets the hosts where the Elasticsearch cluster nodes are reachable. + * + * @param hosts http addresses describing the node locations + */ + public ElasticsearchSinkBuilder<IN> setHosts(HttpHost... hosts) { + checkNotNull(hosts); + checkState(hosts.length > 0, "Hosts cannot be empty."); + this.hosts = Arrays.asList(hosts); + return this; + } + + /** + * Sets the processor which is invoked on every record to convert it to Elasticsearch actions. + * + * @param processor to process records into Elasticsearch actions. + * @return {@link ElasticsearchSinkBuilder} + */ + public <T extends IN> ElasticsearchSinkBuilder<T> setProcessor( + ElasticsearchProcessor<? extends T> processor) { + checkNotNull(processor); + checkState( + InstantiationUtil.isSerializable(processor), + "The elasticsearch processor must be serializable."); + final ElasticsearchSinkBuilder<T> self = self(); + self.processor = processor; + return self; + } + + /** + * Sets the wanted {@link DeliveryGuarantee}. The default delivery guarantee is {@link + * #deliveryGuarantee}. + * + * @param deliveryGuarantee which describes the record emission behaviour + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setDeliveryGuarantee(DeliveryGuarantee deliveryGuarantee) { + checkState( + deliveryGuarantee != DeliveryGuarantee.EXACTLY_ONCE, + "Elasticsearch sink does not support the EXACTLY_ONCE guarantee."); + this.deliveryGuarantee = checkNotNull(deliveryGuarantee); + return this; + } + + /** + * Sets the maximum number of actions to buffer for each bulk request. You can pass -1 to + * disable it. + * + * @param numMaxActions the maximum number of actions to buffer per bulk request. + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushMaxActions(int numMaxActions) { + checkState( + numMaxActions == -1 || numMaxActions > 0, + "Max number of buffered actions must be larger than 0."); + this.bulkFlushMaxActions = numMaxActions; + return this; + } + + /** + * Sets the maximum size of buffered actions, in mb, per bulk request. You can pass -1 to + * disable it. + * + * @param maxSizeMb the maximum size of buffered actions, in mb. + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushMaxSizeMb(int maxSizeMb) { + checkState( + maxSizeMb == -1 || maxSizeMb > 0, + "Max size of buffered actions must be larger than 0."); + this.bulkFlushMaxMb = maxSizeMb; + return this; + } + + /** + * Sets the bulk flush interval, in milliseconds. You can pass -1 to disable it. + * + * @param intervalMillis the bulk flush interval, in milliseconds. + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushInterval(long intervalMillis) { + checkState( + intervalMillis == -1 || intervalMillis >= 0, + "Interval (in milliseconds) between each flush must be larger than " + + "or equal to 0."); + this.bulkFlushInterval = intervalMillis; + return this; + } + + /** + * Sets the type of back of to use when flushing bulk requests. + * + * @param flushBackoffType the backoff type to use. + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushBackoffType( + ElasticsearchSinkBase.FlushBackoffType flushBackoffType) { + this.bulkFlushBackoffType = checkNotNull(flushBackoffType); + return this; + } + + /** + * Sets the maximum number of retries for a backoff attempt when flushing bulk requests. + * + * @param maxRetries the maximum number of retries for a backoff attempt when flushing bulk + * requests + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushBackoffRetries(int maxRetries) { + checkState(maxRetries > 0, "Max number of backoff attempts must be larger than 0."); + this.bulkFlushBackoffRetries = maxRetries; + return this; + } + + /** + * Sets the amount of delay between each backoff attempt when flushing bulk requests, in + * milliseconds. + * + * @param delayMillis the amount of delay between each backoff attempt when flushing bulk + * requests, in milliseconds. + * @return {@link ElasticsearchSinkBuilder} + */ + public ElasticsearchSinkBuilder<IN> setBulkFlushBackoffDelay(long delayMillis) { + checkState( + delayMillis >= 0, + "Delay (in milliseconds) between each backoff attempt must be larger " + + "than or equal to 0."); + this.bulkFlushBackOffDelay = delayMillis; + return this; + } Review comment: Definitely 👍 -- 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]
