|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.dtstack.flink.sql.sink.elasticsearch; |
| 19 | + |
| 20 | +import com.dtstack.flink.sql.sink.elasticsearch.table.ElasticsearchTableInfo; |
| 21 | +import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchApiCallBridge; |
| 22 | +import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkBase; |
| 23 | +import org.apache.flink.streaming.connectors.elasticsearch.util.ElasticsearchUtils; |
| 24 | +import org.apache.flink.util.IOUtils; |
| 25 | +import org.apache.flink.util.Preconditions; |
| 26 | +import org.elasticsearch.action.bulk.BackoffPolicy; |
| 27 | +import org.elasticsearch.action.bulk.BulkItemResponse; |
| 28 | +import org.elasticsearch.action.bulk.BulkProcessor; |
| 29 | +import org.elasticsearch.client.transport.TransportClient; |
| 30 | +import org.elasticsearch.common.network.NetworkModule; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.common.transport.TransportAddress; |
| 33 | +import org.elasticsearch.common.unit.TimeValue; |
| 34 | +import org.elasticsearch.transport.Netty3Plugin; |
| 35 | +import org.elasticsearch.transport.client.PreBuiltTransportClient; |
| 36 | +import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +import javax.annotation.Nullable; |
| 41 | +import java.net.InetSocketAddress; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | + |
| 45 | +/** |
| 46 | + * @date 2019/11/16 |
| 47 | + * @author xiuzhu |
| 48 | + * @Company: www.dtstack.com |
| 49 | + */ |
| 50 | + |
| 51 | +public class ExtendES5ApiCallBridge implements ElasticsearchApiCallBridge<TransportClient> { |
| 52 | + private static final long serialVersionUID = -5222683870097809633L; |
| 53 | + |
| 54 | + private static final Logger LOG = LoggerFactory.getLogger(ExtendES5ApiCallBridge.class); |
| 55 | + |
| 56 | + private final List<InetSocketAddress> transportAddresses; |
| 57 | + |
| 58 | + protected ElasticsearchTableInfo esTableInfo; |
| 59 | + |
| 60 | + public ExtendES5ApiCallBridge(List<InetSocketAddress> transportAddresses, ElasticsearchTableInfo esTableInfo) { |
| 61 | + Preconditions.checkArgument(transportAddresses != null && !transportAddresses.isEmpty()); |
| 62 | + this.transportAddresses = transportAddresses; |
| 63 | + this.esTableInfo = esTableInfo; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public TransportClient createClient(Map<String, String> clientConfig) { |
| 68 | + Settings settings = Settings.builder().put(clientConfig) |
| 69 | + //.put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME) |
| 70 | + //.put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME) |
| 71 | + .build(); |
| 72 | + |
| 73 | + TransportClient transportClient; |
| 74 | + if (esTableInfo.isAuthMesh()) { |
| 75 | + transportClient = new PreBuiltXPackTransportClient(settings); |
| 76 | + }else { |
| 77 | + transportClient = new PreBuiltTransportClient(settings); |
| 78 | + } |
| 79 | + for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) { |
| 80 | + transportClient.addTransportAddress(transport); |
| 81 | + } |
| 82 | + |
| 83 | + // verify that we actually are connected to a cluster |
| 84 | + if (transportClient.connectedNodes().isEmpty()) { |
| 85 | + |
| 86 | + // close the transportClient here |
| 87 | + IOUtils.closeQuietly(transportClient); |
| 88 | + |
| 89 | + throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!"); |
| 90 | + } |
| 91 | + |
| 92 | + if (LOG.isInfoEnabled()) { |
| 93 | + LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes()); |
| 94 | + } |
| 95 | + |
| 96 | + return transportClient; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public BulkProcessor.Builder createBulkProcessorBuilder(TransportClient client, BulkProcessor.Listener listener) { |
| 101 | + return BulkProcessor.builder(client, listener); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Throwable extractFailureCauseFromBulkItemResponse(BulkItemResponse bulkItemResponse) { |
| 106 | + if (!bulkItemResponse.isFailed()) { |
| 107 | + return null; |
| 108 | + } else { |
| 109 | + return bulkItemResponse.getFailure().getCause(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void configureBulkProcessorBackoff( |
| 115 | + BulkProcessor.Builder builder, |
| 116 | + @Nullable ElasticsearchSinkBase.BulkFlushBackoffPolicy flushBackoffPolicy) { |
| 117 | + |
| 118 | + BackoffPolicy backoffPolicy; |
| 119 | + if (flushBackoffPolicy != null) { |
| 120 | + switch (flushBackoffPolicy.getBackoffType()) { |
| 121 | + case CONSTANT: |
| 122 | + backoffPolicy = BackoffPolicy.constantBackoff( |
| 123 | + new TimeValue(flushBackoffPolicy.getDelayMillis()), |
| 124 | + flushBackoffPolicy.getMaxRetryCount()); |
| 125 | + break; |
| 126 | + case EXPONENTIAL: |
| 127 | + default: |
| 128 | + backoffPolicy = BackoffPolicy.exponentialBackoff( |
| 129 | + new TimeValue(flushBackoffPolicy.getDelayMillis()), |
| 130 | + flushBackoffPolicy.getMaxRetryCount()); |
| 131 | + } |
| 132 | + } else { |
| 133 | + backoffPolicy = BackoffPolicy.noBackoff(); |
| 134 | + } |
| 135 | + |
| 136 | + builder.setBackoffPolicy(backoffPolicy); |
| 137 | + } |
| 138 | +} |
0 commit comments