|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +package com.dtstack.flink.sql.sink.elasticsearch; |
| 22 | + |
| 23 | +import org.apache.commons.lang3.StringUtils; |
| 24 | +import org.apache.flink.api.common.functions.RuntimeContext; |
| 25 | +import org.apache.flink.api.java.tuple.Tuple2; |
| 26 | +import org.apache.flink.metrics.Counter; |
| 27 | +import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction; |
| 28 | +import org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer; |
| 29 | +import org.apache.flink.types.Row; |
| 30 | + |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | +import transwarp.org.elasticsearch.action.index.IndexRequest; |
| 34 | +import transwarp.org.elasticsearch.client.Requests; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +/** |
| 41 | + * Reason: |
| 42 | + * Date: 2017/7/19 |
| 43 | + * Company: www.dtstack.com |
| 44 | + * @author xuchao |
| 45 | + */ |
| 46 | + |
| 47 | +public class CustomerSinkFunc implements ElasticsearchSinkFunction<Tuple2> { |
| 48 | + |
| 49 | + private final Logger logger = LoggerFactory.getLogger(CustomerSinkFunc.class); |
| 50 | + |
| 51 | + private static final String ID_VALUE_SPLIT = "_"; |
| 52 | + |
| 53 | + private String index; |
| 54 | + |
| 55 | + private String type; |
| 56 | + |
| 57 | + private List<Integer> idFieldIndexList; |
| 58 | + |
| 59 | + private List<String> fieldNames; |
| 60 | + |
| 61 | + private List<String> fieldTypes; |
| 62 | + |
| 63 | + public transient Counter outRecords; |
| 64 | + |
| 65 | + public transient Counter outDirtyRecords; |
| 66 | + |
| 67 | + /** 默认分隔符为'_' */ |
| 68 | + private char sp = '_'; |
| 69 | + |
| 70 | + public CustomerSinkFunc(String index, String type, List<String> fieldNames, List<String> fieldTypes, List<Integer> idFieldIndexes){ |
| 71 | + this.index = index; |
| 72 | + this.type = type; |
| 73 | + this.fieldNames = fieldNames; |
| 74 | + this.fieldTypes = fieldTypes; |
| 75 | + this.idFieldIndexList = idFieldIndexes; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void process(Tuple2 tuple2, RuntimeContext ctx, RequestIndexer indexer) { |
| 80 | + try{ |
| 81 | + Tuple2<Boolean, Row> tupleTrans = tuple2; |
| 82 | + Boolean retract = tupleTrans.getField(0); |
| 83 | + Row element = tupleTrans.getField(1); |
| 84 | + if(!retract){ |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + indexer.add(createIndexRequest(element)); |
| 89 | + outRecords.inc(); |
| 90 | + }catch (Exception e){ |
| 91 | + outDirtyRecords.inc(); |
| 92 | + logger.error("Failed to store source data {}. ", tuple2.getField(1)); |
| 93 | + logger.error("Failed to create index request exception. ", e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public void setOutRecords(Counter outRecords) { |
| 98 | + this.outRecords = outRecords; |
| 99 | + } |
| 100 | + |
| 101 | + public void setOutDirtyRecords(Counter outDirtyRecords) { |
| 102 | + this.outDirtyRecords = outDirtyRecords; |
| 103 | + } |
| 104 | + |
| 105 | + private IndexRequest createIndexRequest(Row element) { |
| 106 | + String idFieldStr = ""; |
| 107 | + if (null != idFieldIndexList) { |
| 108 | + // index start at 1, |
| 109 | + idFieldStr = idFieldIndexList.stream() |
| 110 | + .filter(index -> index > 0 && index <= element.getArity()) |
| 111 | + .map(index -> element.getField(index - 1).toString()) |
| 112 | + .collect(Collectors.joining(ID_VALUE_SPLIT)); |
| 113 | + } |
| 114 | + |
| 115 | + Map<String, Object> dataMap = EsUtil.rowToJsonMap(element,fieldNames,fieldTypes); |
| 116 | + int length = Math.min(element.getArity(), fieldNames.size()); |
| 117 | + for(int i=0; i<length; i++){ |
| 118 | + dataMap.put(fieldNames.get(i), element.getField(i)); |
| 119 | + } |
| 120 | + |
| 121 | + if (StringUtils.isEmpty(idFieldStr)) { |
| 122 | + return Requests.indexRequest() |
| 123 | + .index(index) |
| 124 | + .type(type) |
| 125 | + .source(dataMap); |
| 126 | + } |
| 127 | + |
| 128 | + return Requests.indexRequest() |
| 129 | + .index(index) |
| 130 | + .type(type) |
| 131 | + .id(idFieldStr) |
| 132 | + .source(dataMap); |
| 133 | + } |
| 134 | +} |
0 commit comments