|
| 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 | +package com.dtstack.flink.sql.sink.solr; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.factory.DTThreadFactory; |
| 22 | +import com.dtstack.flink.sql.outputformat.AbstractDtRichOutputFormat; |
| 23 | +import com.dtstack.flink.sql.sink.solr.client.CloudSolrClientProvider; |
| 24 | +import com.dtstack.flink.sql.sink.solr.options.KerberosOptions; |
| 25 | +import com.dtstack.flink.sql.sink.solr.options.SolrClientOptions; |
| 26 | +import com.dtstack.flink.sql.sink.solr.options.SolrWriteOptions; |
| 27 | +import org.apache.flink.api.java.tuple.Tuple2; |
| 28 | +import org.apache.flink.configuration.Configuration; |
| 29 | +import org.apache.flink.types.Row; |
| 30 | +import org.apache.solr.client.solrj.SolrServerException; |
| 31 | +import org.apache.solr.common.SolrInputDocument; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | +import sun.security.krb5.KrbException; |
| 35 | + |
| 36 | +import javax.security.auth.login.LoginException; |
| 37 | +import java.io.IOException; |
| 38 | +import java.security.PrivilegedActionException; |
| 39 | +import java.util.concurrent.ScheduledExecutorService; |
| 40 | +import java.util.concurrent.ScheduledFuture; |
| 41 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.atomic.AtomicInteger; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author wuren |
| 47 | + * @program flinkStreamSQL |
| 48 | + * @create 2021/05/17 |
| 49 | + */ |
| 50 | +public class SolrOutputFormat extends AbstractDtRichOutputFormat<Tuple2<Boolean, Row>> { |
| 51 | + |
| 52 | + private static final Logger LOG = LoggerFactory.getLogger(SolrOutputFormat.class); |
| 53 | + |
| 54 | + // private transient SolrClient solrClient; |
| 55 | + private final SolrClientOptions solrClientOptions; |
| 56 | + private final SolrWriteOptions solrWriteOptions; |
| 57 | + private final KerberosOptions kerberosOptions; |
| 58 | + private transient AtomicInteger rowCount; |
| 59 | + protected String[] fieldNames; |
| 60 | + private transient ScheduledExecutorService scheduler; |
| 61 | + private transient ScheduledFuture<?> scheduledFuture; |
| 62 | + CloudSolrClientProvider provider; |
| 63 | + |
| 64 | + public SolrOutputFormat( |
| 65 | + SolrClientOptions solrClientOptions, |
| 66 | + SolrWriteOptions solrWriteOptions, |
| 67 | + KerberosOptions kerberosOptions, |
| 68 | + String[] fieldNames) { |
| 69 | + this.solrClientOptions = solrClientOptions; |
| 70 | + this.solrWriteOptions = solrWriteOptions; |
| 71 | + this.kerberosOptions = kerberosOptions; |
| 72 | + this.fieldNames = fieldNames; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void configure(Configuration parameters) {} |
| 77 | + |
| 78 | + @Override |
| 79 | + public void open(int taskNumber, int numTasks) throws IOException { |
| 80 | + provider = new CloudSolrClientProvider(solrClientOptions, kerberosOptions); |
| 81 | + try { |
| 82 | + provider.getClient(); |
| 83 | + } catch (KrbException | LoginException e) { |
| 84 | + LOG.error("", e); |
| 85 | + } |
| 86 | + initMetric(); |
| 87 | + initIntervalFlush(); |
| 88 | + rowCount = new AtomicInteger(0); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void writeRecord(Tuple2<Boolean, Row> record) throws IOException { |
| 93 | + Row row = record.getField(1); |
| 94 | + SolrInputDocument solrDocument = new SolrInputDocument(); |
| 95 | + int columnIndex = 0; |
| 96 | + for (String name : fieldNames) { |
| 97 | + solrDocument.setField(name, row.getField(columnIndex)); |
| 98 | + columnIndex++; |
| 99 | + } |
| 100 | + try { |
| 101 | + provider.add(solrDocument); |
| 102 | + } catch (SolrServerException | PrivilegedActionException e) { |
| 103 | + LOG.error("", e); |
| 104 | + } |
| 105 | + if (rowCount.incrementAndGet() >= solrWriteOptions.getBufferFlushMaxRows()) { |
| 106 | + flush(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void close() throws IOException { |
| 112 | + flush(); |
| 113 | + try { |
| 114 | + provider.close(); |
| 115 | + } catch (PrivilegedActionException e) { |
| 116 | + LOG.error("", e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void initIntervalFlush() { |
| 121 | + if (solrWriteOptions.getBufferFlushIntervalMillis() != 0 |
| 122 | + && solrWriteOptions.getBufferFlushMaxRows() != 1) { |
| 123 | + this.scheduler = |
| 124 | + new ScheduledThreadPoolExecutor(1, new DTThreadFactory("solr-batch-flusher")); |
| 125 | + this.scheduledFuture = |
| 126 | + this.scheduler.scheduleWithFixedDelay( |
| 127 | + () -> { |
| 128 | + flush(); |
| 129 | + }, |
| 130 | + solrWriteOptions.getBufferFlushIntervalMillis(), |
| 131 | + solrWriteOptions.getBufferFlushIntervalMillis(), |
| 132 | + TimeUnit.MILLISECONDS); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private synchronized void flush() { |
| 137 | + try { |
| 138 | + provider.commit(); |
| 139 | + rowCount.set(0); |
| 140 | + } catch (SolrServerException | IOException | PrivilegedActionException e) { |
| 141 | + LOG.error("", e); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments