37
37
import com .datastax .driver .core .policies .DowngradingConsistencyRetryPolicy ;
38
38
import com .datastax .driver .core .policies .RetryPolicy ;
39
39
import com .dtstack .flink .sql .enums .ECacheContentType ;
40
- import com .dtstack .flink .sql .side .AsyncReqRow ;
40
+ import com .dtstack .flink .sql .side .BaseAsyncReqRow ;
41
41
import com .dtstack .flink .sql .side .CacheMissVal ;
42
42
import com .dtstack .flink .sql .side .FieldInfo ;
43
43
import com .dtstack .flink .sql .side .JoinInfo ;
44
- import com .dtstack .flink .sql .side .SideTableInfo ;
44
+ import com .dtstack .flink .sql .side .AbstractSideTableInfo ;
45
45
import com .dtstack .flink .sql .side .cache .CacheObj ;
46
46
import com .dtstack .flink .sql .side .cassandra .table .CassandraSideTableInfo ;
47
47
import com .google .common .base .Function ;
67
67
*
68
68
* @author xuqianjin
69
69
*/
70
- public class CassandraAsyncReqRow extends AsyncReqRow {
70
+ public class CassandraAsyncReqRow extends BaseAsyncReqRow {
71
71
72
72
private static final long serialVersionUID = 6631584128079864735L ;
73
73
@@ -83,7 +83,7 @@ public class CassandraAsyncReqRow extends AsyncReqRow {
83
83
private transient ListenableFuture session ;
84
84
private transient CassandraSideTableInfo cassandraSideTableInfo ;
85
85
86
- public CassandraAsyncReqRow (RowTypeInfo rowTypeInfo , JoinInfo joinInfo , List <FieldInfo > outFieldInfoList , SideTableInfo sideTableInfo ) {
86
+ public CassandraAsyncReqRow (RowTypeInfo rowTypeInfo , JoinInfo joinInfo , List <FieldInfo > outFieldInfoList , AbstractSideTableInfo sideTableInfo ) {
87
87
super (new com .dtstack .flink .sql .side .cassandra .CassandraAsyncSideInfo (rowTypeInfo , joinInfo , outFieldInfoList , sideTableInfo ));
88
88
}
89
89
@@ -162,61 +162,14 @@ private void connCassandraDB(CassandraSideTableInfo tableInfo) {
162
162
}
163
163
164
164
@ Override
165
- public void asyncInvoke (CRow input , ResultFuture <CRow > resultFuture ) throws Exception {
166
- CRow inputCopy = new CRow (input .row (), input .change ());
167
- JsonArray inputParams = new JsonArray ();
168
- StringBuffer stringBuffer = new StringBuffer ();
169
- String sqlWhere = " where " ;
170
-
171
- for (int i = 0 ; i < sideInfo .getEqualFieldList ().size (); i ++) {
172
- Integer conValIndex = sideInfo .getEqualValIndex ().get (i );
173
- Object equalObj = inputCopy .row ().getField (conValIndex );
174
- if (equalObj == null ) {
175
- dealMissKey (inputCopy , resultFuture );
176
- return ;
177
- }
178
- inputParams .add (equalObj );
179
- StringBuffer sqlTemp = stringBuffer .append (sideInfo .getEqualFieldList ().get (i ))
180
- .append (" = " );
181
- if (equalObj instanceof String ) {
182
- sqlTemp .append ("'" + equalObj + "'" )
183
- .append (" and " );
184
- } else {
185
- sqlTemp .append (equalObj )
186
- .append (" and " );
187
- }
188
-
189
- }
165
+ public void handleAsyncInvoke (Map <String , Object > inputParams , CRow input , ResultFuture <CRow > resultFuture ) throws Exception {
190
166
191
167
String key = buildCacheKey (inputParams );
192
- sqlWhere = sqlWhere + stringBuffer .toString ().substring (0 , stringBuffer .lastIndexOf (" and " ));
193
-
194
- if (openCache ()) {
195
- CacheObj val = getFromCache (key );
196
- if (val != null ) {
197
-
198
- if (ECacheContentType .MissVal == val .getType ()) {
199
- dealMissKey (inputCopy , resultFuture );
200
- return ;
201
- } else if (ECacheContentType .MultiLine == val .getType ()) {
202
- List <CRow > rowList = Lists .newArrayList ();
203
- for (Object jsonArray : (List ) val .getContent ()) {
204
- Row row = fillData (inputCopy .row (), jsonArray );
205
- rowList .add (new CRow (row , inputCopy .change ()));
206
- }
207
- resultFuture .complete (rowList );
208
- } else {
209
- throw new RuntimeException ("not support cache obj type " + val .getType ());
210
- }
211
- return ;
212
- }
213
- }
214
-
215
168
//connect Cassandra
216
169
connCassandraDB (cassandraSideTableInfo );
217
170
218
- String sqlCondition = sideInfo .getSqlCondition () + " " + sqlWhere + " ALLOW FILTERING " ;
219
- System . out . println ("sqlCondition:" + sqlCondition );
171
+ String sqlCondition = sideInfo .getSqlCondition () + " " + buildWhereCondition ( inputParams ) + " ALLOW FILTERING " ;
172
+ LOG . info ("sqlCondition:{} " + sqlCondition );
220
173
221
174
ListenableFuture <ResultSet > resultSet = Futures .transformAsync (session ,
222
175
new AsyncFunction <Session , ResultSet >() {
@@ -242,18 +195,18 @@ public void onSuccess(List<com.datastax.driver.core.Row> rows) {
242
195
List <com .datastax .driver .core .Row > cacheContent = Lists .newArrayList ();
243
196
List <CRow > rowList = Lists .newArrayList ();
244
197
for (com .datastax .driver .core .Row line : rows ) {
245
- Row row = fillData (inputCopy .row (), line );
198
+ Row row = fillData (input .row (), line );
246
199
if (openCache ()) {
247
200
cacheContent .add (line );
248
201
}
249
- rowList .add (new CRow (row ,inputCopy .change ()));
202
+ rowList .add (new CRow (row , input .change ()));
250
203
}
251
204
resultFuture .complete (rowList );
252
205
if (openCache ()) {
253
206
putCache (key , CacheObj .buildCacheObj (ECacheContentType .MultiLine , cacheContent ));
254
207
}
255
208
} else {
256
- dealMissKey (inputCopy , resultFuture );
209
+ dealMissKey (input , resultFuture );
257
210
if (openCache ()) {
258
211
putCache (key , CacheMissVal .getMissKeyObj ());
259
212
}
@@ -265,13 +218,30 @@ public void onSuccess(List<com.datastax.driver.core.Row> rows) {
265
218
public void onFailure (Throwable t ) {
266
219
LOG .error ("Failed to retrieve the data: %s%n" ,
267
220
t .getMessage ());
268
- System .out .println ("Failed to retrieve the data: " + t .getMessage ());
269
221
cluster .closeAsync ();
270
222
resultFuture .completeExceptionally (t );
271
223
}
272
224
});
273
225
}
274
226
227
+ @ Override
228
+ public String buildCacheKey (Map <String , Object > inputParams ) {
229
+ StringBuilder sb = new StringBuilder ();
230
+ for (Object ele : inputParams .values ()) {
231
+ sb .append (ele .toString ()).append ("_" );
232
+ }
233
+ return sb .toString ();
234
+ }
235
+
236
+ private String buildWhereCondition (Map <String , Object > inputParams ){
237
+ StringBuilder sb = new StringBuilder (" where " );
238
+ for (Map .Entry <String , Object > entry : inputParams .entrySet ()){
239
+ Object value = entry .getValue () instanceof String ? "'" + entry .getValue () + "'" : entry .getValue ();
240
+ sb .append (String .format ("%s = %s" , entry .getKey (), value ));
241
+ }
242
+ return sb .toString ();
243
+ }
244
+
275
245
@ Override
276
246
public Row fillData (Row input , Object line ) {
277
247
com .datastax .driver .core .Row rowArray = (com .datastax .driver .core .Row ) line ;
@@ -306,14 +276,4 @@ public void close() throws Exception {
306
276
cluster = null ;
307
277
}
308
278
}
309
-
310
- public String buildCacheKey (JsonArray jsonArray ) {
311
- StringBuilder sb = new StringBuilder ();
312
- for (Object ele : jsonArray .getList ()) {
313
- sb .append (ele .toString ())
314
- .append ("_" );
315
- }
316
-
317
- return sb .toString ();
318
- }
319
279
}
0 commit comments