1
- <<<<<<< HEAD
2
- =======
3
1
/*
4
2
* © Copyright IBM Corp. 2025
5
3
*/
6
4
7
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
8
5
package com .getindata .connectors .http .internal .table .lookup .querycreators ;
9
6
10
7
import java .io .IOException ;
13
10
import java .nio .charset .StandardCharsets ;
14
11
import java .util .*;
15
12
16
- <<<<<<< HEAD
17
- import lombok .Builder ;
18
13
import lombok .extern .slf4j .Slf4j ;
19
- =======
20
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
21
14
import org .apache .flink .annotation .VisibleForTesting ;
22
15
import org .apache .flink .api .common .serialization .SerializationSchema ;
23
16
import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .core .JsonProcessingException ;
24
17
import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .databind .JsonNode ;
25
18
import org .apache .flink .shaded .jackson2 .com .fasterxml .jackson .databind .node .ObjectNode ;
26
- <<<<<<< HEAD
27
- import org .apache .flink .table .data .RowData ;
28
- import org .apache .flink .util .FlinkRuntimeException ;
29
- import org .apache .flink .util .Preconditions ;
30
- =======
31
19
import org .apache .flink .table .api .DataTypes .Field ;
32
20
import org .apache .flink .table .data .GenericRowData ;
33
21
import org .apache .flink .table .data .RowData ;
36
24
import org .apache .flink .types .Row ;
37
25
import org .apache .flink .util .FlinkRuntimeException ;
38
26
import org .apache .flink .util .Preconditions ;
39
- import org .apache .logging .log4j .LogManager ;
40
- import org .apache .logging .log4j .Logger ;
41
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
42
27
43
28
import com .getindata .connectors .http .LookupArg ;
44
29
import com .getindata .connectors .http .LookupQueryCreator ;
47
32
import com .getindata .connectors .http .internal .utils .SerializationSchemaUtils ;
48
33
49
34
/**
50
- <<<<<<< HEAD
51
- * <p>Generic JSON and URL query creator; in addition to be able to map columns to json requests,
52
- * it allows url inserts to be mapped to column names using templating.</p>
53
- * <p>For GETs, column names are mapped to query parameters. e.g. for
54
- * <code>GenericJsonAndUrlQueryCreator.REQUEST_PARAM_FIELDS</code> = "id1;id2"
55
- * and url of http://base. At lookup time with values of id1=1 and id2=2 a call of
56
- * http/base?id1=1&id2=2 will be issued.</p>
57
- * <p>For PUT and POST, parameters are mapped to the json body e.g. for
58
- * REQUEST_PARAM_FIELDS = "id1;id2" and url of http://base. At lookup time with values of id1=1 and
59
- * id2=2 as call of http/base will be issued with a json payload of {"id1":1,"id2":2}</p>
60
- * <p>For all http methods, url segments can be used to include lookup up values. Using the map from
61
- =======
62
35
* Generic JSON and URL query creator; in addition to be able to map columns to json requests,
63
36
* it allows url inserts to be mapped to column names using templating.
64
37
* <br>
72
45
* id2=2 as call of http/base will be issued with a json payload of {"id1":1,"id2":2}
73
46
* <br>
74
47
* For all http methods, url segments can be used to include lookup up values. Using the map from
75
- >>>>>>> 6c68722 (HTTP-99 Generic Json url query creator)
76
48
* <code>GenericJsonAndUrlQueryCreator.REQUEST_URL_MAP</code> which has a key of the insert and the
77
49
* value of the associated column.
78
50
* e.g. for <code>GenericJsonAndUrlQueryCreator.REQUEST_URL_MAP</code> = "key1":"col1"
79
51
* and url of http://base/{key1}. At lookup time with values of col1="aaaa" a call of
80
- <<<<<<< HEAD
81
- * http/base/aaaa will be issued.</p>
82
- */
83
- @ Slf4j
84
- @ Builder
85
- public class GenericJsonAndUrlQueryCreator implements LookupQueryCreator {
86
- private static final long serialVersionUID = 1L ;
87
- private final String httpMethod ;
88
- // not final so we can mutate for unit test
89
- private SerializationSchema <RowData > serializationSchema ;
90
- private final List <String > requestQueryParamsFields ;
91
- private boolean schemaOpened = false ;
92
- private final List <String > requestBodyFields ;
93
- private final Map <String , String > requestUrlMap ;
94
- private final LookupRow lookupRow ;
95
- =======
96
52
* http/base/aaaa will be issued.
97
53
*
98
54
*/
55
+ @ Slf4j
99
56
public class GenericJsonAndUrlQueryCreator implements LookupQueryCreator {
100
57
private static final long serialVersionUID = 1L ;
101
- private static final Logger log = LogManager .getLogger (GenericJsonAndUrlQueryCreator .class );
102
58
103
59
// not final so we can mutate for unit test
104
60
private SerializationSchema <RowData > serializationSchema ;
@@ -133,7 +89,6 @@ public GenericJsonAndUrlQueryCreator(final String httpMethod,
133
89
this .requestBodyFields = requestBodyFields ;
134
90
this .requestUrlMap = requestUrlMap ;
135
91
}
136
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
137
92
@ VisibleForTesting
138
93
void setSerializationSchema (SerializationSchema <RowData >
139
94
serializationSchema ) {
@@ -153,13 +108,9 @@ public LookupQueryInfo createLookupQuery(final RowData lookupDataRow) {
153
108
jsonObject = (ObjectNode ) ObjectMapperAdapter .instance ().readTree (
154
109
serializationSchema .serialize (lookupDataRow ));
155
110
} catch (IOException e ) {
156
- <<<<<<< HEAD
157
- throw new RuntimeException ("Unable to parse the lookup arguments to json." , e );
158
- =======
159
111
String message = "Unable to parse the lookup arguments to json." ;
160
112
log .error (message , e );
161
113
throw new RuntimeException (message , e );
162
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
163
114
}
164
115
// Parameters are encoded as query params for GET and none GET.
165
116
// Later code will turn these query params into the body for PUTs and POSTs
@@ -180,34 +131,24 @@ public LookupQueryInfo createLookupQuery(final RowData lookupDataRow) {
180
131
lookupQuery = ObjectMapperAdapter .instance ()
181
132
.writeValueAsString (jsonObject .retain (requestBodyFields ));
182
133
} catch (JsonProcessingException e ) {
183
- <<<<<<< HEAD
184
- throw new RuntimeException ("Unable to convert Json Object to a string" , e );
185
- =======
186
134
final String message = "Unable to convert Json Object to a string" ;
187
135
log .error (message , e );
188
136
throw new RuntimeException (message ,e );
189
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
190
137
}
191
138
// body parameters
192
139
// use the request json object to scope the required fields and the lookupArgs as values
193
140
bodyBasedUrlQueryParams = createBodyBasedParams (lookupArgs ,
194
141
jsonObjectForQueryParams );
195
142
}
196
143
// add the path map
197
- <<<<<<< HEAD
198
- final Map <String , String > pathBasedUrlParams = createURLPathBasedParams (lookupArgs ,
199
- =======
200
144
final Map <String , String > pathBasedUrlParams = createPathBasedParams (lookupArgs ,
201
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
202
145
requestUrlMap );
203
146
204
147
return new LookupQueryInfo (lookupQuery , bodyBasedUrlQueryParams , pathBasedUrlParams );
205
148
206
149
}
207
150
208
151
/**
209
- <<<<<<< HEAD
210
- =======
211
152
* Create a Row from a RowData and DataType
212
153
* @param lookupRowData the lookup RowData
213
154
* @param rowType the datatype
@@ -230,19 +171,14 @@ static Row rowDataToRow(final RowData lookupRowData, final DataType rowType) {
230
171
}
231
172
232
173
/**
233
- >>>>>>> 6c68722 (HTTP-99 Generic Json url query creator)
234
174
* Create map of the json key to the lookup argument
235
175
* value. This is used for body based content.
236
176
* @param args lookup arguments
237
177
* @param objectNode object node
238
178
* @return map of field content to the lookup argument value.
239
179
*/
240
180
private Map <String , String > createBodyBasedParams (final Collection <LookupArg > args ,
241
- <<<<<<< HEAD
242
- ObjectNode objectNode ) {
243
- =======
244
181
ObjectNode objectNode ) {
245
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
246
182
Map <String , String > mapOfJsonKeyToLookupArg = new LinkedHashMap <>();
247
183
Iterator <Map .Entry <String , JsonNode >> iterator = objectNode .fields ();
248
184
iterator .forEachRemaining (field -> {
@@ -258,45 +194,26 @@ private Map<String, String> createBodyBasedParams(final Collection<LookupArg> ar
258
194
return mapOfJsonKeyToLookupArg ;
259
195
}
260
196
/**
261
- <<<<<<< HEAD
262
- * Create map of insert name to column name for path inserts
263
- =======
264
197
* Create map of the json key to the lookup argument
265
198
* value. This is used for body based content.
266
- >>>>>>> 6c68722 (HTTP-99 Generic Json url query creator)
267
199
* @param args lookup arguments
268
200
* @param urlMap map of insert name to column name
269
201
* @return map of field content to the lookup argument value.
270
202
*/
271
- <<<<<<< HEAD
272
- private Map <String , String > createURLPathBasedParams (final Collection <LookupArg > args ,
273
- Map <String ,
274
- String > urlMap ) {
275
- Map <String , String > mapOfinsertKeyToLookupArg = new LinkedHashMap <>();
276
- =======
277
203
private Map <String , String > createPathBasedParams (final Collection <LookupArg > args ,
278
204
Map <String , String > urlMap ) {
279
205
Map <String , String > mapOfJsonKeyToLookupArg = new LinkedHashMap <>();
280
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
281
206
if (urlMap != null ) {
282
207
for (String key : urlMap .keySet ()) {
283
208
for (final LookupArg arg : args ) {
284
209
if (arg .getArgName ().equals (key )) {
285
- <<<<<<< HEAD
286
- mapOfinsertKeyToLookupArg .put (
287
- =======
288
210
mapOfJsonKeyToLookupArg .put (
289
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
290
211
urlMap .get (key ), arg .getArgValue ());
291
212
}
292
213
}
293
214
}
294
215
}
295
- <<<<<<< HEAD
296
- return mapOfinsertKeyToLookupArg ;
297
- =======
298
216
return mapOfJsonKeyToLookupArg ;
299
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
300
217
}
301
218
/**
302
219
* Convert json object to query params string
@@ -317,23 +234,13 @@ static String convertToQueryParameters(final ObjectNode jsonObject, String enc)
317
234
result .add (fieldName + "="
318
235
+ URLEncoder .encode (fieldValue , enc ));
319
236
} catch (UnsupportedEncodingException e ) {
320
- <<<<<<< HEAD
321
- throw new RuntimeException ("Failed to encode the value of the query parameter name "
322
- + fieldName
323
- + ": "
324
- + fieldValue
325
- + " using encoding "
326
- + enc ,
327
- e );
328
- =======
329
237
final String message =
330
238
"Failed to encode the value of the query parameter name "
331
239
+ fieldName
332
240
+ ": "
333
241
+ fieldValue ;
334
242
log .error (message , e );
335
243
throw new RuntimeException (message , e );
336
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
337
244
}
338
245
});
339
246
@@ -349,16 +256,11 @@ private void checkOpened() {
349
256
GenericJsonAndUrlQueryCreator .class ));
350
257
this .schemaOpened = true ;
351
258
} catch (final Exception e ) {
352
- <<<<<<< HEAD
353
- throw new FlinkRuntimeException ("Failed to initialize serialization schema for "
354
- + GenericJsonAndUrlQueryCreator .class , e );
355
- =======
356
259
final String message =
357
260
"Failed to initialize serialization schema for "
358
261
+ GenericJsonAndUrlQueryCreator .class ;
359
262
log .error (message , e );
360
263
throw new FlinkRuntimeException (message , e );
361
- >>>>>>> 6 c68722 (HTTP -99 Generic Json url query creator )
362
264
}
363
265
}
364
266
}
0 commit comments