|
| 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.tencent.cloud.oceanus.sink; |
| 20 | + |
| 21 | +import org.apache.flink.annotation.PublicEvolving; |
| 22 | +import org.apache.flink.configuration.ConfigOption; |
| 23 | +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import org.apache.flink.streaming.api.functions.sink.SinkFunction; |
| 26 | +import org.apache.flink.table.connector.ChangelogMode; |
| 27 | +import org.apache.flink.table.connector.sink.DynamicTableSink; |
| 28 | +import org.apache.flink.table.connector.sink.SinkFunctionProvider; |
| 29 | +import org.apache.flink.table.factories.DynamicTableSinkFactory; |
| 30 | +import org.apache.flink.types.RowKind; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +/** |
| 38 | + * Logger table sink factory prints all input records using SLF4J loggers. |
| 39 | + * It prints both toString and JSON format in the TaskManager log files. |
| 40 | + */ |
| 41 | +@PublicEvolving |
| 42 | +public class LoggerTableSinkFactory implements DynamicTableSinkFactory { |
| 43 | + |
| 44 | + public static final String IDENTIFIER = "logger"; |
| 45 | + |
| 46 | + @Override |
| 47 | + public String factoryIdentifier() { |
| 48 | + return IDENTIFIER; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Set<ConfigOption<?>> requiredOptions() { |
| 53 | + return new HashSet<>(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Set<ConfigOption<?>> optionalOptions() { |
| 58 | + return new HashSet<>(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public DynamicTableSink createDynamicTableSink(Context context) { |
| 63 | + return new LoggerSink(); |
| 64 | + } |
| 65 | + |
| 66 | + private static class LoggerSink implements DynamicTableSink { |
| 67 | + |
| 68 | + @Override |
| 69 | + public ChangelogMode getChangelogMode(ChangelogMode requestedMode) { |
| 70 | + ChangelogMode.Builder builder = ChangelogMode.newBuilder(); |
| 71 | + for (RowKind kind : requestedMode.getContainedKinds()) { |
| 72 | + if (kind != RowKind.UPDATE_BEFORE) { |
| 73 | + builder.addContainedKind(kind); |
| 74 | + } |
| 75 | + } |
| 76 | + return builder.build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public SinkRuntimeProvider getSinkRuntimeProvider(Context context) { |
| 81 | + return SinkFunctionProvider.of(new Slf4jSink<>()); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public DynamicTableSink copy() { |
| 86 | + return new LoggerSink(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String asSummaryString() { |
| 91 | + return "Logger"; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class Slf4jSink<T> implements SinkFunction<T> { |
| 97 | + private static final Logger LOGGER = LoggerFactory.getLogger(Slf4jSink.class); |
| 98 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 99 | + |
| 100 | + private static final long serialVersionUID = 1L; |
| 101 | + |
| 102 | + @Override |
| 103 | + public void invoke(T value) { |
| 104 | + String jsonString = ""; |
| 105 | + try { |
| 106 | + jsonString = MAPPER.writeValueAsString(value); |
| 107 | + } catch (JsonProcessingException e) { |
| 108 | + LOGGER.debug("Unable to serialize value into JSON", e); |
| 109 | + } |
| 110 | + LOGGER.info("toString: {}, JSON: {}", value.toString(), jsonString); |
| 111 | + } |
| 112 | +} |
0 commit comments