|
| 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 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.redis.client; |
| 19 | + |
| 20 | +import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisDataType; |
| 21 | +import org.apache.seatunnel.connectors.seatunnel.redis.config.RedisParameters; |
| 22 | + |
| 23 | +import org.apache.commons.collections4.CollectionUtils; |
| 24 | + |
| 25 | +import redis.clients.jedis.Jedis; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +public class RedisClusterClient extends RedisClient { |
| 33 | + public RedisClusterClient(RedisParameters redisParameters, Jedis jedis) { |
| 34 | + super(redisParameters, jedis); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public List<String> batchGetString(List<String> keys) { |
| 39 | + if (CollectionUtils.isEmpty(keys)) { |
| 40 | + return new ArrayList<>(); |
| 41 | + } |
| 42 | + List<String> result = new ArrayList<>(keys.size()); |
| 43 | + for (String key : keys) { |
| 44 | + result.add(jedis.get(key)); |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public List<List<String>> batchGetList(List<String> keys) { |
| 51 | + if (CollectionUtils.isEmpty(keys)) { |
| 52 | + return new ArrayList<>(); |
| 53 | + } |
| 54 | + List<List<String>> result = new ArrayList<>(keys.size()); |
| 55 | + for (String key : keys) { |
| 56 | + result.add(jedis.lrange(key, 0, -1)); |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public List<Set<String>> batchGetSet(List<String> keys) { |
| 63 | + if (CollectionUtils.isEmpty(keys)) { |
| 64 | + return new ArrayList<>(); |
| 65 | + } |
| 66 | + List<Set<String>> result = new ArrayList<>(keys.size()); |
| 67 | + for (String key : keys) { |
| 68 | + result.add(jedis.smembers(key)); |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public List<Map<String, String>> batchGetHash(List<String> keys) { |
| 75 | + if (CollectionUtils.isEmpty(keys)) { |
| 76 | + return new ArrayList<>(); |
| 77 | + } |
| 78 | + List<Map<String, String>> result = new ArrayList<>(keys.size()); |
| 79 | + for (String key : keys) { |
| 80 | + Map<String, String> map = jedis.hgetAll(key); |
| 81 | + map.put("hash_key", key); |
| 82 | + result.add(map); |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public List<List<String>> batchGetZset(List<String> keys) { |
| 89 | + if (CollectionUtils.isEmpty(keys)) { |
| 90 | + return new ArrayList<>(); |
| 91 | + } |
| 92 | + List<List<String>> result = new ArrayList<>(keys.size()); |
| 93 | + for (String key : keys) { |
| 94 | + result.add(jedis.zrange(key, 0, -1)); |
| 95 | + } |
| 96 | + return result; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void batchWriteString(List<String> keys, List<String> values, long expireSeconds) { |
| 101 | + int size = keys.size(); |
| 102 | + for (int i = 0; i < size; i++) { |
| 103 | + RedisDataType.STRING.set(this, keys.get(i), values.get(i), expireSeconds); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void batchWriteList(List<String> keys, List<String> values, long expireSeconds) { |
| 109 | + int size = keys.size(); |
| 110 | + for (int i = 0; i < size; i++) { |
| 111 | + RedisDataType.LIST.set(this, keys.get(i), values.get(i), expireSeconds); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void batchWriteSet(List<String> keys, List<String> values, long expireSeconds) { |
| 117 | + int size = keys.size(); |
| 118 | + for (int i = 0; i < size; i++) { |
| 119 | + RedisDataType.SET.set(this, keys.get(i), values.get(i), expireSeconds); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void batchWriteHash(List<String> keys, List<String> values, long expireSeconds) { |
| 125 | + int size = keys.size(); |
| 126 | + for (int i = 0; i < size; i++) { |
| 127 | + RedisDataType.HASH.set(this, keys.get(i), values.get(i), expireSeconds); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void batchWriteZset(List<String> keys, List<String> values, long expireSeconds) { |
| 133 | + int size = keys.size(); |
| 134 | + for (int i = 0; i < size; i++) { |
| 135 | + RedisDataType.ZSET.set(this, keys.get(i), values.get(i), expireSeconds); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments