|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * 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 | + |
| 18 | +package org.apache.flink.streaming.connectors.redis; |
| 19 | + |
| 20 | +import org.apache.flink.streaming.connectors.redis.config.StartupMode; |
| 21 | + |
| 22 | +import redis.clients.jedis.Jedis; |
| 23 | +import redis.clients.jedis.StreamEntry; |
| 24 | +import redis.clients.jedis.StreamEntryID; |
| 25 | + |
| 26 | +import java.util.AbstractMap.SimpleEntry; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Map.Entry; |
| 32 | +import java.util.Properties; |
| 33 | + |
| 34 | +/** @param <T> */ |
| 35 | +public abstract class AbstractRedisStreamConsumer<T> extends RedisConsumerBase<T> { |
| 36 | + |
| 37 | + protected final Entry<String, StreamEntryID>[] streamEntryIds; |
| 38 | + private final Map<String, Integer> keyIndex = new HashMap<>(); |
| 39 | + |
| 40 | + public AbstractRedisStreamConsumer( |
| 41 | + StartupMode startupMode, String[] streamKeys, Properties configProps) { |
| 42 | + super(Arrays.asList(streamKeys), configProps); |
| 43 | + final StreamEntryID streamEntryID; |
| 44 | + switch (startupMode) { |
| 45 | + case EARLIEST: |
| 46 | + streamEntryID = new StreamEntryID(); |
| 47 | + break; |
| 48 | + case LATEST: |
| 49 | + streamEntryID = StreamEntryID.LAST_ENTRY; |
| 50 | + break; |
| 51 | + case GROUP_OFFSETS: |
| 52 | + streamEntryID = StreamEntryID.UNRECEIVED_ENTRY; |
| 53 | + break; |
| 54 | + case SPECIFIC_OFFSETS: |
| 55 | + throw new RuntimeException( |
| 56 | + "Use the constructor with 'StreamEntryID[] streamIds' as param"); |
| 57 | + case TIMESTAMP: |
| 58 | + throw new RuntimeException("Use the constructor with 'Long[] timestamps' param"); |
| 59 | + default: |
| 60 | + throw new IllegalStateException(); |
| 61 | + } |
| 62 | + this.streamEntryIds = prepareStreamEntryIds(streamKeys, streamEntryID); |
| 63 | + initializeKeyIndex(); |
| 64 | + } |
| 65 | + |
| 66 | + public AbstractRedisStreamConsumer( |
| 67 | + String[] streamKeys, Long[] timestamps, Properties configProps) { |
| 68 | + this(streamKeys, streamEntryIds(timestamps), configProps); |
| 69 | + } |
| 70 | + |
| 71 | + public AbstractRedisStreamConsumer( |
| 72 | + String[] streamKeys, StreamEntryID[] streamIds, Properties configProps) { |
| 73 | + this(prepareStreamEntryIds(streamKeys, streamIds), configProps); |
| 74 | + } |
| 75 | + |
| 76 | + private AbstractRedisStreamConsumer( |
| 77 | + Entry<String, StreamEntryID>[] streamIds, Properties configProps) { |
| 78 | + super(null, configProps); |
| 79 | + this.streamEntryIds = streamIds; |
| 80 | + initializeKeyIndex(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected final boolean readAndCollect( |
| 85 | + Jedis jedis, List<String> streamKeys, SourceContext<T> sourceContext) { |
| 86 | + boolean anyEntry = false; |
| 87 | + List<Entry<String, List<StreamEntry>>> response = read(jedis); |
| 88 | + if (response != null) { |
| 89 | + for (Entry<String, List<StreamEntry>> streamEntries : response) { |
| 90 | + String streamKey = streamEntries.getKey(); |
| 91 | + for (StreamEntry entry : streamEntries.getValue()) { |
| 92 | + anyEntry = true; |
| 93 | + collect(sourceContext, streamKey, entry); |
| 94 | + updateIdForKey(streamKey, entry.getID()); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return anyEntry; |
| 99 | + } |
| 100 | + |
| 101 | + protected abstract List<Entry<String, List<StreamEntry>>> read(Jedis jedis); |
| 102 | + |
| 103 | + protected abstract void collect( |
| 104 | + SourceContext<T> sourceContext, String streamKey, StreamEntry streamEntry); |
| 105 | + |
| 106 | + protected void updateIdForKey(String streamKey, StreamEntryID streamEntryID) { |
| 107 | + int index = keyIndex.get(streamKey); |
| 108 | + if (this.streamEntryIds[index].getValue().toString().equals(">")) { |
| 109 | + // skip |
| 110 | + } else { |
| 111 | + this.streamEntryIds[index].setValue(streamEntryID); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void initializeKeyIndex() { |
| 116 | + int index = 0; |
| 117 | + for (Entry<String, StreamEntryID> streamEntryId : streamEntryIds) { |
| 118 | + keyIndex.put(streamEntryId.getKey(), index++); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static Entry<String, StreamEntryID>[] prepareStreamEntryIds( |
| 123 | + String[] streamKeys, StreamEntryID streamId) { |
| 124 | + Entry<?, ?>[] streams = new Entry<?, ?>[streamKeys.length]; |
| 125 | + for (int i = 0; i < streamKeys.length; i++) { |
| 126 | + streams[i] = new SimpleEntry<>(streamKeys[i], streamId); |
| 127 | + } |
| 128 | + return (Entry<String, StreamEntryID>[]) streams; |
| 129 | + } |
| 130 | + |
| 131 | + private static Entry<String, StreamEntryID>[] prepareStreamEntryIds( |
| 132 | + String[] streamKeys, StreamEntryID[] streamIds) { |
| 133 | + Entry<?, ?>[] streams = new Entry<?, ?>[streamKeys.length]; |
| 134 | + for (int i = 0; i < streamKeys.length; i++) { |
| 135 | + streams[i] = new SimpleEntry<>(streamKeys[i], streamIds[i]); |
| 136 | + } |
| 137 | + return (Entry<String, StreamEntryID>[]) streams; |
| 138 | + } |
| 139 | + |
| 140 | + private static StreamEntryID[] streamEntryIds(Long[] timestamps) { |
| 141 | + StreamEntryID[] entryIds = new StreamEntryID[timestamps.length]; |
| 142 | + for (int i = 0; i < timestamps.length; i++) { |
| 143 | + entryIds[i] = new StreamEntryID(timestamps[i], 0L); |
| 144 | + } |
| 145 | + return entryIds; |
| 146 | + } |
| 147 | +} |
0 commit comments