snuyanzin commented on code in PR #56: URL: https://github.com/apache/flink-connector-opensearch/pull/56#discussion_r2810030837
########## flink-connector-opensearch3/src/main/java/org/apache/flink/connector/opensearch/table/RowOpensearch3Emitter.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.opensearch.table; + +import org.apache.flink.api.common.serialization.SerializationSchema; +import org.apache.flink.api.connector.sink2.SinkWriter; +import org.apache.flink.connector.opensearch.sink.Opensearch3Emitter; +import org.apache.flink.connector.opensearch.sink.Opensearch3RequestIndexer; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.metrics.groups.UnregisteredMetricsGroup; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.data.RowData; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.SimpleUserCodeClassLoader; +import org.apache.flink.util.UserCodeClassLoader; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** Sink emitter for converting upserts into OpenSearch 3.x bulk operations. */ +class RowOpensearch3Emitter implements Opensearch3Emitter<RowData> { + + private final IndexGenerator indexGenerator; + private final SerializationSchema<RowData> serializationSchema; + private final Function<RowData, String> createKey; + private transient ObjectMapper objectMapper; + + public RowOpensearch3Emitter( + IndexGenerator indexGenerator, + SerializationSchema<RowData> serializationSchema, + Function<RowData, String> createKey) { + this.indexGenerator = checkNotNull(indexGenerator); + this.serializationSchema = checkNotNull(serializationSchema); + this.createKey = checkNotNull(createKey); + } + + @Override + public void open() throws Exception { + try { + serializationSchema.open( + new SerializationSchema.InitializationContext() { + @Override + public MetricGroup getMetricGroup() { + return new UnregisteredMetricsGroup(); + } + + @Override + public UserCodeClassLoader getUserCodeClassLoader() { + return SimpleUserCodeClassLoader.create( + RowOpensearch3Emitter.class.getClassLoader()); + } + }); + } catch (Exception e) { + throw new FlinkRuntimeException("Failed to initialize serialization schema.", e); + } + indexGenerator.open(); + objectMapper = new ObjectMapper(); Review Comment: is there a reason we need to create it each time? Can we instead having just one for all (static final) ? -- 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]
