Skip to content

Commit 71bdfac

Browse files
author
Kyle Dong
committed
Added logger table sink
1 parent 7b3febc commit 71bdfac

File tree

4 files changed

+155
-5
lines changed

4 files changed

+155
-5
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
# flink-hello-world
2-
Flink 示例程序包, 用于快速构建和运行一个 Flink JAR 作业。
2+
Flink 示例程序包(自定义函数 UDF、自定义 Source 和 Sink 等),可扩展 Flink 程序的功能。
3+
4+
5+
6+
## Flink 程序示例
37

4-
目前包含如下用例:
58
1. WordCount
6-
2. DecodeLatin1 示例 UDF
7-
3. EncodeLatin1 示例 UDF
8-
4. PrintChar 示例 UDF
9+
10+
11+
12+
## UDF 示例
13+
14+
1. DecodeLatin1
15+
2. EncodeLatin1
16+
3. PrintChar
17+
18+
19+
20+
## Sink 示例
21+
22+
1. LoggerTableSinkFactory
23+

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<scope>provided</scope>
4343
</dependency>
4444

45+
<dependency>
46+
<groupId>org.apache.flink</groupId>
47+
<artifactId>flink-table-api-java-bridge_2.11</artifactId>
48+
<version>${flink.version}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
4552
<dependency>
4653
<groupId>org.apache.flink</groupId>
4754
<artifactId>flink-table-common</artifactId>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
com.tencent.cloud.oceanus.sink.LoggerTableSinkFactory

0 commit comments

Comments
 (0)