Skip to content

Commit 9934c61

Browse files
committed
[FLINK-15571] Add basic Redis Streams sink.
1 parent 14f1250 commit 9934c61

File tree

15 files changed

+1491
-0
lines changed

15 files changed

+1491
-0
lines changed

flink-connector-redis-streams/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>flink-connector-redis-parent</artifactId>
7+
<groupId>org.apache.flink</groupId>
8+
<version>3.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>flink-connector-redis-streams</artifactId>
13+
<name>Flink : Connectors : Redis : Streams</name>
14+
15+
<packaging>jar</packaging>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>redis.clients</groupId>
20+
<artifactId>jedis</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.apache.flink</groupId>
25+
<artifactId>flink-connector-base</artifactId>
26+
<version>${flink.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.apache.flink</groupId>
31+
<artifactId>flink-streaming-java</artifactId>
32+
</dependency>
33+
</dependencies>
34+
35+
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.flink.connector.redis.streams.sink;
18+
19+
import org.apache.flink.api.common.functions.Function;
20+
import org.apache.flink.connector.redis.streams.sink.command.RedisCommand;
21+
22+
import java.io.Serializable;
23+
24+
/**
25+
* Function that creates the description how the input data should be mapped to redis type.
26+
*
27+
* @param <T> The type of the element handled by this {@code RedisSerializer}
28+
*/
29+
public interface RedisSerializer<T> extends Function, Serializable {
30+
31+
RedisCommand getMessage(T input);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.flink.connector.redis.streams.sink;
18+
19+
import org.apache.flink.api.connector.sink2.Sink;
20+
import org.apache.flink.api.connector.sink2.SinkWriter;
21+
import org.apache.flink.connector.redis.streams.sink.config.JedisConfig;
22+
import org.apache.flink.connector.redis.streams.sink.connection.JedisConnector;
23+
import org.apache.flink.connector.redis.streams.sink.connection.JedisConnectorBuilder;
24+
25+
import java.io.IOException;
26+
27+
public class RedisStreamsSink<T> implements Sink<T> {
28+
29+
private final JedisConfig jedisConfig;
30+
private final RedisSerializer<T> serializer;
31+
32+
public RedisStreamsSink(JedisConfig jedisConfig, RedisSerializer<T> serializer) {
33+
this.jedisConfig = jedisConfig;
34+
this.serializer = serializer;
35+
}
36+
37+
@Override
38+
public SinkWriter<T> createWriter(InitContext initContext) throws IOException {
39+
JedisConnector connection = JedisConnectorBuilder.build(jedisConfig);
40+
return new RedisStreamsWriter<>(connection, this.serializer);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.flink.connector.redis.streams.sink;
18+
19+
import org.apache.flink.api.connector.sink2.SinkWriter;
20+
import org.apache.flink.connector.redis.streams.sink.command.RedisCommand;
21+
import org.apache.flink.connector.redis.streams.sink.connection.JedisConnector;
22+
23+
import java.io.IOException;
24+
import java.util.ArrayDeque;
25+
import java.util.Queue;
26+
27+
public class RedisStreamsWriter<T> implements SinkWriter<T> {
28+
29+
private final JedisConnector jedisConnector;
30+
private final RedisSerializer<T> serializer;
31+
private final Queue<RedisCommand> queue = new ArrayDeque<>();
32+
33+
public RedisStreamsWriter(JedisConnector jedisConnector, RedisSerializer<T> serializer) {
34+
this.jedisConnector = jedisConnector;
35+
this.serializer = serializer;
36+
}
37+
38+
39+
@Override
40+
public void write(T input, Context context) throws IOException, InterruptedException {
41+
RedisCommand message = serializer.getMessage(input);
42+
queue.add(message);
43+
}
44+
45+
@Override
46+
public void flush(boolean endOfInput) throws IOException, InterruptedException {
47+
flush();
48+
}
49+
50+
private void flush() {
51+
while(!this.queue.isEmpty()) {
52+
RedisCommand element = this.queue.remove();
53+
element.send(this.jedisConnector);
54+
}
55+
}
56+
57+
@Override
58+
public void close() throws Exception {
59+
jedisConnector.close();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.flink.connector.redis.streams.sink.command;
18+
19+
import org.apache.flink.connector.redis.streams.sink.connection.JedisConnector;
20+
21+
import java.io.Serializable;
22+
23+
public interface RedisCommand extends Serializable {
24+
void send(JedisConnector connector);
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.flink.connector.redis.streams.sink.command;
18+
19+
20+
import org.apache.flink.connector.redis.streams.sink.connection.JedisConnector;
21+
import redis.clients.jedis.StreamEntryID;
22+
23+
import java.util.Map;
24+
25+
public class StreamRedisCommand implements RedisCommand {
26+
public final String key;
27+
public final Map<String, String> value;
28+
29+
private StreamRedisCommand(String key, Map<String, String> value) {
30+
this.key = key;
31+
this.value = value;
32+
}
33+
34+
public static Builder builder() {
35+
return new Builder();
36+
}
37+
38+
@Override
39+
public void send(JedisConnector connector) {
40+
connector.getJedisCommands().xadd(key, StreamEntryID.NEW_ENTRY, value);
41+
}
42+
43+
public static class Builder {
44+
private String key;
45+
private Map<String, String> value;
46+
47+
public Builder withKey(String key) {
48+
this.key = key;
49+
return this;
50+
}
51+
52+
public Builder withValue(Map<String, String> value) {
53+
this.value = value;
54+
return this;
55+
}
56+
57+
public StreamRedisCommand build() {
58+
return new StreamRedisCommand(key, value);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)