20
20
import org .apache .seatunnel .api .table .type .RowKind ;
21
21
import org .apache .seatunnel .common .utils .JsonUtils ;
22
22
import org .apache .seatunnel .connectors .seatunnel .redis .config .RedisParameters ;
23
+ import org .apache .seatunnel .connectors .seatunnel .redis .exception .RedisConnectorException ;
24
+ import org .apache .seatunnel .connectors .seatunnel .redis .exception .RedisErrorCode ;
23
25
24
26
import org .apache .commons .collections4 .CollectionUtils ;
25
27
26
28
import redis .clients .jedis .Jedis ;
27
29
import redis .clients .jedis .Pipeline ;
28
30
import redis .clients .jedis .Response ;
31
+ import redis .clients .jedis .exceptions .JedisException ;
29
32
30
33
import java .util .ArrayList ;
31
34
import java .util .List ;
@@ -142,69 +145,76 @@ public List<List<String>> batchGetZset(List<String> keys) {
142
145
@ Override
143
146
public void batchWriteString (
144
147
List <RowKind > rowKinds , List <String > keys , List <String > values , long expireSeconds ) {
148
+ List <Response <?>> responses = new ArrayList <>();
145
149
Pipeline pipelined = jedis .pipelined ();
146
150
int size = keys .size ();
147
151
for (int i = 0 ; i < size ; i ++) {
148
152
RowKind rowKind = rowKinds .get (i );
149
153
String key = keys .get (i );
150
154
String value = values .get (i );
151
155
if (rowKind == RowKind .DELETE || rowKind == RowKind .UPDATE_BEFORE ) {
152
- pipelined .del (key );
156
+ responses . add ( pipelined .del (key ) );
153
157
} else {
154
- pipelined .set (key , value );
158
+ responses . add ( pipelined .set (key , value ) );
155
159
if (expireSeconds > 0 ) {
156
- pipelined .expire (key , expireSeconds );
160
+ responses . add ( pipelined .expire (key , expireSeconds ) );
157
161
}
158
162
}
159
163
}
160
164
pipelined .sync ();
165
+ processResponses (responses );
161
166
}
162
167
163
168
@ Override
164
169
public void batchWriteList (
165
170
List <RowKind > rowKinds , List <String > keys , List <String > values , long expireSeconds ) {
171
+ List <Response <?>> responses = new ArrayList <>();
166
172
Pipeline pipelined = jedis .pipelined ();
167
173
int size = keys .size ();
168
174
for (int i = 0 ; i < size ; i ++) {
169
175
RowKind rowKind = rowKinds .get (i );
170
176
String key = keys .get (i );
171
177
String value = values .get (i );
172
178
if (rowKind == RowKind .DELETE || rowKind == RowKind .UPDATE_BEFORE ) {
173
- pipelined .lrem (key , 1 , value );
179
+ responses . add ( pipelined .lrem (key , 1 , value ) );
174
180
} else {
175
- pipelined .lpush (key , value );
181
+ responses . add ( pipelined .lpush (key , value ) );
176
182
if (expireSeconds > 0 ) {
177
- pipelined .expire (key , expireSeconds );
183
+ responses . add ( pipelined .expire (key , expireSeconds ) );
178
184
}
179
185
}
180
186
}
181
187
pipelined .sync ();
188
+ processResponses (responses );
182
189
}
183
190
184
191
@ Override
185
192
public void batchWriteSet (
186
193
List <RowKind > rowKinds , List <String > keys , List <String > values , long expireSeconds ) {
194
+ List <Response <?>> responses = new ArrayList <>();
187
195
Pipeline pipelined = jedis .pipelined ();
188
196
int size = keys .size ();
189
197
for (int i = 0 ; i < size ; i ++) {
190
198
RowKind rowKind = rowKinds .get (i );
191
199
String key = keys .get (i );
192
200
String value = values .get (i );
193
201
if (rowKind == RowKind .DELETE || rowKind == RowKind .UPDATE_BEFORE ) {
194
- pipelined .srem (key , value );
202
+ responses . add ( pipelined .srem (key , value ) );
195
203
} else {
196
- pipelined .sadd (key , value );
204
+ responses . add ( pipelined .sadd (key , value ) );
197
205
if (expireSeconds > 0 ) {
198
- pipelined .expire (key , expireSeconds );
206
+ responses . add ( pipelined .expire (key , expireSeconds ) );
199
207
}
200
208
}
201
209
}
202
210
pipelined .sync ();
211
+ processResponses (responses );
203
212
}
204
213
205
214
@ Override
206
215
public void batchWriteHash (
207
216
List <RowKind > rowKinds , List <String > keys , List <String > values , long expireSeconds ) {
217
+ List <Response <?>> responses = new ArrayList <>();
208
218
Pipeline pipelined = jedis .pipelined ();
209
219
int size = keys .size ();
210
220
for (int i = 0 ; i < size ; i ++) {
@@ -214,36 +224,50 @@ public void batchWriteHash(
214
224
Map <String , String > fieldsMap = JsonUtils .toMap (value );
215
225
if (rowKind == RowKind .DELETE || rowKind == RowKind .UPDATE_BEFORE ) {
216
226
for (Map .Entry <String , String > entry : fieldsMap .entrySet ()) {
217
- pipelined .hdel (key , entry .getKey ());
227
+ responses . add ( pipelined .hdel (key , entry .getKey () ));
218
228
}
219
229
} else {
220
- pipelined .hset (key , fieldsMap );
230
+ responses . add ( pipelined .hset (key , fieldsMap ) );
221
231
if (expireSeconds > 0 ) {
222
- pipelined .expire (key , expireSeconds );
232
+ responses . add ( pipelined .expire (key , expireSeconds ) );
223
233
}
224
234
}
225
235
}
226
236
pipelined .sync ();
237
+ processResponses (responses );
227
238
}
228
239
229
240
@ Override
230
241
public void batchWriteZset (
231
242
List <RowKind > rowKinds , List <String > keys , List <String > values , long expireSeconds ) {
243
+ List <Response <?>> responses = new ArrayList <>();
232
244
Pipeline pipelined = jedis .pipelined ();
233
245
int size = keys .size ();
234
246
for (int i = 0 ; i < size ; i ++) {
235
247
RowKind rowKind = rowKinds .get (i );
236
248
String key = keys .get (i );
237
249
String value = values .get (i );
238
250
if (rowKind == RowKind .DELETE || rowKind == RowKind .UPDATE_BEFORE ) {
239
- pipelined .zrem (key , value );
251
+ responses . add ( pipelined .zrem (key , value ) );
240
252
} else {
241
- pipelined .zadd (key , 1 , value );
253
+ responses . add ( pipelined .zadd (key , 1 , value ) );
242
254
if (expireSeconds > 0 ) {
243
- pipelined .expire (key , expireSeconds );
255
+ responses . add ( pipelined .expire (key , expireSeconds ) );
244
256
}
245
257
}
246
258
}
247
259
pipelined .sync ();
260
+ processResponses (responses );
261
+ }
262
+
263
+ private void processResponses (List <Response <?>> responseList ) {
264
+ try {
265
+ for (Response <?> response : responseList ) {
266
+ // If the response is an exception object, it will be thrown
267
+ response .get ();
268
+ }
269
+ } catch (JedisException e ) {
270
+ throw new RedisConnectorException (RedisErrorCode .GET_RESPONSE_FAILED , e );
271
+ }
248
272
}
249
273
}
0 commit comments