3030import com .google .common .collect .Maps ;
3131import org .apache .calcite .sql .JoinType ;
3232import org .apache .commons .collections .CollectionUtils ;
33- import org .apache .commons .lang3 .StringUtils ;
3433import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
3534import org .apache .flink .api .java .typeutils .RowTypeInfo ;
3635import org .apache .flink .table .dataformat .BaseRow ;
4847import java .io .Closeable ;
4948import java .io .IOException ;
5049import java .sql .SQLException ;
50+ import java .util .Arrays ;
5151import java .util .Calendar ;
5252import java .util .HashSet ;
5353import java .util .List ;
5454import java .util .Map ;
55+ import java .util .Objects ;
5556import java .util .Set ;
5657import java .util .TreeSet ;
5758import java .util .concurrent .atomic .AtomicReference ;
@@ -117,9 +118,9 @@ protected void reloadCache() {
117118 public void flatMap (BaseRow input , Collector <BaseRow > out ) throws Exception {
118119 GenericRow genericRow = (GenericRow ) input ;
119120 Map <String , Object > inputParams = Maps .newHashMap ();
120- for (Integer conValIndex : sideInfo .getEqualValIndex ()){
121+ for (Integer conValIndex : sideInfo .getEqualValIndex ()) {
121122 Object equalObj = genericRow .getField (conValIndex );
122- if (equalObj == null ){
123+ if (equalObj == null ) {
123124 if (sideInfo .getJoinType () == JoinType .LEFT ) {
124125 BaseRow data = fillData (input , null );
125126 RowDataComplete .collectBaseRow (out , data );
@@ -133,11 +134,11 @@ public void flatMap(BaseRow input, Collector<BaseRow> out) throws Exception {
133134
134135 Map <String , String > cacheMap = cacheRef .get ().get (key );
135136
136- if (cacheMap == null ){
137- if (sideInfo .getJoinType () == JoinType .LEFT ){
137+ if (cacheMap == null ) {
138+ if (sideInfo .getJoinType () == JoinType .LEFT ) {
138139 BaseRow data = fillData (input , null );
139140 RowDataComplete .collectBaseRow (out , data );
140- }else {
141+ } else {
141142 return ;
142143 }
143144
@@ -187,49 +188,54 @@ private JedisCommands getJedis(RedisSideTableInfo tableInfo) {
187188 String url = tableInfo .getUrl ();
188189 String password = tableInfo .getPassword ();
189190 String database = tableInfo .getDatabase () == null ? "0" : tableInfo .getDatabase ();
191+ String masterName = tableInfo .getMasterName ();
190192 int timeout = tableInfo .getTimeout ();
191- if (timeout == 0 ){
192- timeout = 1000 ;
193- }
194-
195- String [] nodes = StringUtils .split (url , "," );
196- String [] firstIpPort = StringUtils .split (nodes [0 ], ":" );
197- String firstIp = firstIpPort [0 ];
198- String firstPort = firstIpPort [1 ];
199- Set <HostAndPort > addresses = new HashSet <>();
200- Set <String > ipPorts = new HashSet <>();
201-
202- // 对ipv6 支持
203- for (String node : nodes ) {
204- ipPorts .add (node );
205- Matcher matcher = HOST_PORT_PATTERN .matcher (node );
206- if (matcher .find ()) {
207- String host = matcher .group ("host" ).trim ();
208- String portStr = matcher .group ("port" ).trim ();
209- if (StringUtils .isNotBlank (host ) && StringUtils .isNotBlank (portStr )) {
210- // 转化为int格式的端口
211- int port = Integer .parseInt (portStr );
212- addresses .add (new HostAndPort (host , port ));
213- }
214- }
193+ if (timeout == 0 ) {
194+ timeout = 10000 ;
215195 }
216196
197+ String [] nodes = url .split ("," );
217198 JedisCommands jedis = null ;
218199 GenericObjectPoolConfig poolConfig = setPoolConfig (tableInfo .getMaxTotal (), tableInfo .getMaxIdle (), tableInfo .getMinIdle ());
219- switch (RedisType .parse (tableInfo .getRedisType ())){
220- //单机
200+ switch (RedisType .parse (tableInfo .getRedisType ())) {
221201 case STANDALONE :
222- pool = new JedisPool (poolConfig , firstIp , Integer .parseInt (firstPort ), timeout , password , Integer .parseInt (database ));
223- jedis = pool .getResource ();
202+ String firstIp = null ;
203+ String firstPort = null ;
204+ Matcher standalone = HOST_PORT_PATTERN .matcher (nodes [0 ]);
205+ if (standalone .find ()) {
206+ firstIp = standalone .group ("host" ).trim ();
207+ firstPort = standalone .group ("port" ).trim ();
208+ }
209+ if (Objects .nonNull (firstIp )) {
210+ pool = new JedisPool (poolConfig , firstIp , Integer .parseInt (firstPort ), timeout , password , Integer .parseInt (database ));
211+ jedis = pool .getResource ();
212+ } else {
213+ throw new IllegalArgumentException (
214+ String .format ("redis url error. current url [%s]" , nodes [0 ]));
215+ }
224216 break ;
225- //哨兵
226217 case SENTINEL :
227- jedisSentinelPool = new JedisSentinelPool (tableInfo .getMasterName (), ipPorts , poolConfig , timeout , password , Integer .parseInt (database ));
218+ Set <String > ipPorts = new HashSet <>(Arrays .asList (nodes ));
219+ jedisSentinelPool = new JedisSentinelPool (masterName , ipPorts , poolConfig , timeout , password , Integer .parseInt (database ));
228220 jedis = jedisSentinelPool .getResource ();
229221 break ;
230- //集群
231222 case CLUSTER :
232- jedis = new JedisCluster (addresses , timeout , timeout ,1 , poolConfig );
223+ Set <HostAndPort > addresses = new HashSet <>();
224+ // 对ipv6 支持
225+ for (String node : nodes ) {
226+ Matcher matcher = HOST_PORT_PATTERN .matcher (node );
227+ if (matcher .find ()) {
228+ String host = matcher .group ("host" ).trim ();
229+ String portStr = matcher .group ("port" ).trim ();
230+ if (org .apache .commons .lang3 .StringUtils .isNotBlank (host ) && org .apache .commons .lang3 .StringUtils .isNotBlank (portStr )) {
231+ // 转化为int格式的端口
232+ int port = Integer .parseInt (portStr );
233+ addresses .add (new HostAndPort (host , port ));
234+ }
235+ }
236+ }
237+ jedis = new JedisCluster (addresses , timeout , timeout , 10 , password , poolConfig );
238+ break ;
233239 default :
234240 break ;
235241 }
@@ -257,35 +263,32 @@ private JedisCommands getJedisWithRetry(int retryNum) {
257263 return null ;
258264 }
259265
260- private Set <String > getRedisKeys (RedisType redisType , JedisCommands jedis , String keyPattern ){
261- if (!redisType .equals (RedisType .CLUSTER )){
266+ private Set <String > getRedisKeys (RedisType redisType , JedisCommands jedis , String keyPattern ) {
267+ if (!redisType .equals (RedisType .CLUSTER )) {
262268 return ((Jedis ) jedis ).keys (keyPattern );
263269 }
264270 Set <String > keys = new TreeSet <>();
265- Map <String , JedisPool > clusterNodes = ((JedisCluster )jedis ).getClusterNodes ();
266- for (String k : clusterNodes .keySet ()){
271+ Map <String , JedisPool > clusterNodes = ((JedisCluster ) jedis ).getClusterNodes ();
272+ for (String k : clusterNodes .keySet ()) {
267273 JedisPool jp = clusterNodes .get (k );
268- Jedis connection = jp .getResource ();
269- try {
274+ try (Jedis connection = jp .getResource ()) {
270275 keys .addAll (connection .keys (keyPattern ));
271- } catch (Exception e ){
272- LOG .error ("Getting keys error: {}" , e );
273- } finally {
274- connection .close ();
276+ } catch (Exception e ) {
277+ LOG .error ("Getting keys error" , e );
275278 }
276279 }
277280 return keys ;
278281 }
279282
280- private GenericObjectPoolConfig setPoolConfig (String maxTotal , String maxIdle , String minIdle ){
283+ private GenericObjectPoolConfig setPoolConfig (String maxTotal , String maxIdle , String minIdle ) {
281284 GenericObjectPoolConfig config = new GenericObjectPoolConfig ();
282- if (maxTotal != null ){
285+ if (maxTotal != null ) {
283286 config .setMaxTotal (Integer .parseInt (maxTotal ));
284287 }
285- if (maxIdle != null ){
288+ if (maxIdle != null ) {
286289 config .setMaxIdle (Integer .parseInt (maxIdle ));
287290 }
288- if (minIdle != null ){
291+ if (minIdle != null ) {
289292 config .setMinIdle (Integer .parseInt (minIdle ));
290293 }
291294 return config ;
0 commit comments