3
3
import com .google .gson .Gson ;
4
4
import io .tiledb .cloud .rest_api .ApiException ;
5
5
import io .tiledb .cloud .rest_api .api .UdfApi ;
6
- import io .tiledb .cloud .rest_api .model .GenericUDF ;
7
- import io .tiledb .cloud .rest_api .model .MultiArrayUDF ;
8
- import io .tiledb .cloud .rest_api .model .ResultFormat ;
6
+ import io .tiledb .cloud .rest_api .model .*;
9
7
import io .tiledb .java .api .Pair ;
10
8
import org .apache .arrow .compression .CommonsCompressionFactory ;
11
9
import org .apache .arrow .memory .RootAllocator ;
21
19
import java .io .ByteArrayInputStream ;
22
20
import java .io .IOException ;
23
21
import java .util .ArrayList ;
22
+ import java .util .Arrays ;
24
23
import java .util .HashMap ;
24
+ import java .util .List ;
25
+
26
+ import static io .tiledb .cloud .TileDBUtils .serializeArgs ;
25
27
26
28
public class TileDBUDF {
27
29
private TileDBClient tileDBClient ;
@@ -32,7 +34,6 @@ public TileDBUDF(TileDBClient tileDBClient, String namespace) {
32
34
this .tileDBClient = tileDBClient ;
33
35
this .namespace = namespace ;
34
36
this .apiInstance = new UdfApi (this .tileDBClient .getApiClient ());
35
-
36
37
}
37
38
38
39
/**
@@ -57,6 +58,25 @@ public String executeGeneric(GenericUDF genericUDF, HashMap<String, Object> argu
57
58
return null ;
58
59
}
59
60
61
+ /**
62
+ * Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
63
+ *
64
+ * @param genericUDF The generic UDF definition
65
+ * @return The result in String format
66
+ */
67
+ public String executeGeneric (GenericUDF genericUDF ){
68
+ try {
69
+ return apiInstance .submitGenericUDFString (namespace , genericUDF , "none" );
70
+ } catch (ApiException e ) {
71
+ System .err .println ("Exception when calling UdfApi#submitGenericUDF" );
72
+ System .err .println ("Status code: " + e .getCode ());
73
+ System .err .println ("Reason: " + e .getResponseBody ());
74
+ System .err .println ("Response headers: " + e .getResponseHeaders ());
75
+ e .printStackTrace ();
76
+ }
77
+ return null ;
78
+ }
79
+
60
80
/**
61
81
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
62
82
*
@@ -70,6 +90,18 @@ public JSONObject executeGenericJSON(GenericUDF genericUDF, HashMap<String, Obje
70
90
return new JSONObject (jsonString );
71
91
}
72
92
93
+ /**
94
+ * Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
95
+ *
96
+ * @param genericUDF The generic UDF definition
97
+ * @return The result as a JSON object
98
+ */
99
+ public JSONObject executeGenericJSON (GenericUDF genericUDF ){
100
+ genericUDF .setResultFormat (ResultFormat .JSON );
101
+ String jsonString = this .executeGeneric (genericUDF );
102
+ return new JSONObject (jsonString );
103
+ }
104
+
73
105
/**
74
106
* Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
75
107
*
@@ -79,7 +111,19 @@ public JSONObject executeGenericJSON(GenericUDF genericUDF, HashMap<String, Obje
79
111
*/
80
112
public JSONArray executeGenericJSONArray (GenericUDF genericUDF , HashMap <String , Object > arguments ){
81
113
genericUDF .setResultFormat (ResultFormat .JSON );
82
- String jsonString = this .executeGeneric (genericUDF , arguments );
114
+ String jsonString = this .executeGeneric (genericUDF , arguments );
115
+ return new JSONArray (jsonString );
116
+ }
117
+
118
+ /**
119
+ * Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
120
+ *
121
+ * @param genericUDF The generic UDF definition
122
+ * @return The result as a JSON array object
123
+ */
124
+ public JSONArray executeGenericJSONArray (GenericUDF genericUDF ){
125
+ genericUDF .setResultFormat (ResultFormat .JSON );
126
+ String jsonString = this .executeGeneric (genericUDF );
83
127
return new JSONArray (jsonString );
84
128
}
85
129
@@ -90,7 +134,8 @@ public JSONArray executeGenericJSONArray(GenericUDF genericUDF, HashMap<String,
90
134
* @param arguments The UDF arguments
91
135
* @return A pair that consists of an ArrayList of all valueVectors and the number of batches read.
92
136
*/
93
- public io .tiledb .java .api .Pair <ArrayList <ValueVector >, Integer > executeGenericArrow (GenericUDF genericUDF , HashMap <String , Object > arguments ){
137
+ public io .tiledb .java .api .Pair <ArrayList <ValueVector >, Integer > executeGenericArrow (GenericUDF genericUDF ,
138
+ HashMap <String , Object > arguments ){
94
139
String serializedArgs = serializeArgs (arguments );
95
140
genericUDF .setArgument (serializedArgs );
96
141
genericUDF .setResultFormat (ResultFormat .ARROW );
@@ -103,6 +148,24 @@ public io.tiledb.java.api.Pair<ArrayList<ValueVector>, Integer> executeGenericAr
103
148
return null ;
104
149
}
105
150
151
+ /**
152
+ * Executes a generic-UDF. A generic-UDF is a UDF that is not using a TIleDB array.
153
+ *
154
+ * @param genericUDF The generic UDF definition
155
+ * @return A pair that consists of an ArrayList of all valueVectors and the number of batches read.
156
+ */
157
+ public io .tiledb .java .api .Pair <ArrayList <ValueVector >, Integer > executeGenericArrow (GenericUDF genericUDF ){
158
+ genericUDF .setResultFormat (ResultFormat .ARROW );
159
+ try {
160
+ byte [] bytes = apiInstance .submitGenericUDFBytes (namespace , genericUDF , "none" );
161
+ return TileDBUtils .createValueVectors (bytes );
162
+ } catch (IOException | ApiException e ) {
163
+ e .printStackTrace ();
164
+ }
165
+ return null ;
166
+ }
167
+
168
+ @ Deprecated
106
169
/**
107
170
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
108
171
*
@@ -130,6 +193,40 @@ public String executeSingleArray(MultiArrayUDF multiArrayUDF, HashMap<String, Ob
130
193
return null ;
131
194
}
132
195
196
+ /**
197
+ * Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
198
+ *
199
+ * @param genericUDF The UDF to run
200
+ * @param array The Array to run the UDF on
201
+ * @param queryRanges the Query ranges
202
+ * @param xPayer Name of organization or user who should be charged for this request
203
+ * @return The results as a String
204
+ */
205
+ public String executeSingleArray (GenericUDF genericUDF , UDFArrayDetails array ,
206
+ QueryRanges queryRanges , String xPayer ){
207
+ List <UDFArrayDetails > arrays = new ArrayList <>();
208
+ arrays .add (array );
209
+
210
+ MultiArrayUDF multiArrayUDF = new MultiArrayUDF ();
211
+ multiArrayUDF .setArgument (genericUDF .getArgument ());
212
+ multiArrayUDF .setUdfInfoName (genericUDF .getUdfInfoName ());
213
+ multiArrayUDF .setRanges (queryRanges );
214
+ multiArrayUDF .setArrays (arrays );
215
+ String [] split = breakdownFullURI (array .getUri ());
216
+
217
+ try {
218
+ return apiInstance .submitUDFString (split [0 ], split [1 ], multiArrayUDF , xPayer , "none" , "" );
219
+ } catch (ApiException e ) {
220
+ System .err .println ("Exception when calling UdfApi#submitUDF" );
221
+ System .err .println ("Status code: " + e .getCode ());
222
+ System .err .println ("Reason: " + e .getResponseBody ());
223
+ System .err .println ("Response headers: " + e .getResponseHeaders ());
224
+ e .printStackTrace ();
225
+ }
226
+ return null ;
227
+ }
228
+
229
+ @ Deprecated
133
230
/**
134
231
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
135
232
*
@@ -145,6 +242,42 @@ public JSONObject executeSingleArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<St
145
242
return new JSONObject (jsonString );
146
243
}
147
244
245
+ /**
246
+ * Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
247
+ *
248
+ * @param genericUDF The UDF to run
249
+ * @param array The Array to run the UDF on
250
+ * @param queryRanges the Query ranges
251
+ * @param xPayer Name of organization or user who should be charged for this request
252
+ * @return The results as a JSON Object
253
+ */
254
+ public JSONObject executeSingleArrayJSON (GenericUDF genericUDF , UDFArrayDetails array ,
255
+ QueryRanges queryRanges , String xPayer ){
256
+
257
+ List <UDFArrayDetails > arrays = new ArrayList <>();
258
+ arrays .add (array );
259
+
260
+ MultiArrayUDF multiArrayUDF = new MultiArrayUDF ();
261
+ multiArrayUDF .setArgument (genericUDF .getArgument ());
262
+ multiArrayUDF .setUdfInfoName (genericUDF .getUdfInfoName ());
263
+ multiArrayUDF .setRanges (queryRanges );
264
+ multiArrayUDF .setArrays (arrays );
265
+ String [] split = breakdownFullURI (array .getUri ());
266
+ multiArrayUDF .setResultFormat (ResultFormat .JSON );
267
+ String result = "" ;
268
+ try {
269
+ result = apiInstance .submitUDFString (split [0 ], split [1 ], multiArrayUDF , xPayer , "none" , "" );
270
+ } catch (ApiException e ) {
271
+ System .err .println ("Exception when calling UdfApi#submitUDF" );
272
+ System .err .println ("Status code: " + e .getCode ());
273
+ System .err .println ("Reason: " + e .getResponseBody ());
274
+ System .err .println ("Response headers: " + e .getResponseHeaders ());
275
+ e .printStackTrace ();
276
+ }
277
+ return new JSONObject (result );
278
+ }
279
+
280
+ @ Deprecated
148
281
/**
149
282
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
150
283
*
@@ -154,12 +287,48 @@ public JSONObject executeSingleArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<St
154
287
* @param xPayer Name of organization or user who should be charged for this request
155
288
* @return The results as a JSON array
156
289
*/
157
- public JSONArray executeSingleArrayJSONArray (MultiArrayUDF multiArrayUDF , HashMap <String , Object > arguments , String arrayURI , String xPayer ){
290
+ public JSONArray executeSingleArrayJSONArray (MultiArrayUDF multiArrayUDF , HashMap <String ,
291
+ Object > arguments , String arrayURI , String xPayer ){
158
292
multiArrayUDF .setResultFormat (ResultFormat .JSON );
159
293
String jsonString = this .executeSingleArray (multiArrayUDF , arguments , arrayURI , xPayer );
160
294
return new JSONArray (jsonString );
161
295
}
162
296
297
+ /**
298
+ * Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
299
+ *
300
+ * @param genericUDF The UDF to run
301
+ * @param array The Array to run the UDF on
302
+ * @param queryRanges the Query ranges
303
+ * @param xPayer Name of organization or user who should be charged for this request
304
+ * @return The results as a JSON Object
305
+ */
306
+ public JSONArray executeSingleArrayJSONArray (GenericUDF genericUDF , UDFArrayDetails array ,
307
+ QueryRanges queryRanges , String xPayer ){
308
+ List <UDFArrayDetails > arrays = new ArrayList <>();
309
+ arrays .add (array );
310
+
311
+ MultiArrayUDF multiArrayUDF = new MultiArrayUDF ();
312
+ multiArrayUDF .setArgument (genericUDF .getArgument ());
313
+ multiArrayUDF .setUdfInfoName (genericUDF .getUdfInfoName ());
314
+ multiArrayUDF .setRanges (queryRanges );
315
+ multiArrayUDF .setArrays (arrays );
316
+ String [] split = breakdownFullURI (array .getUri ());
317
+ multiArrayUDF .setResultFormat (ResultFormat .JSON );
318
+ String result = "" ;
319
+ try {
320
+ result = apiInstance .submitUDFString (split [0 ], split [1 ], multiArrayUDF , xPayer , "none" , "" );
321
+ } catch (ApiException e ) {
322
+ System .err .println ("Exception when calling UdfApi#submitUDF" );
323
+ System .err .println ("Status code: " + e .getCode ());
324
+ System .err .println ("Reason: " + e .getResponseBody ());
325
+ System .err .println ("Response headers: " + e .getResponseHeaders ());
326
+ e .printStackTrace ();
327
+ }
328
+ return new JSONArray (result );
329
+ }
330
+
331
+ @ Deprecated
163
332
/**
164
333
* Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
165
334
*
@@ -185,6 +354,37 @@ public Pair<ArrayList<ValueVector>, Integer> executeSingleArrayArrow(MultiArrayU
185
354
return null ;
186
355
}
187
356
357
+ /**
358
+ * Executes an array-UDF. An array-UDF is a UDF applied to a TileDB array
359
+ *
360
+ * @param genericUDF The UDF to run
361
+ * @param array The Array to run the UDF on
362
+ * @param queryRanges the Query ranges
363
+ * @param xPayer Name of organization or user who should be charged for this request
364
+ * @return The results in arrow format
365
+ */
366
+ public Pair <ArrayList <ValueVector >, Integer > executeSingleArrayArrow (GenericUDF genericUDF , UDFArrayDetails array ,
367
+ QueryRanges queryRanges , String xPayer ){
368
+ List <UDFArrayDetails > arrays = new ArrayList <>();
369
+ arrays .add (array );
370
+
371
+ MultiArrayUDF multiArrayUDF = new MultiArrayUDF ();
372
+ multiArrayUDF .setArgument (genericUDF .getArgument ());
373
+ multiArrayUDF .setResultFormat (ResultFormat .ARROW );
374
+ multiArrayUDF .setUdfInfoName (genericUDF .getUdfInfoName ());
375
+ multiArrayUDF .setRanges (queryRanges );
376
+ multiArrayUDF .setArrays (arrays );
377
+ String [] split = breakdownFullURI (array .getUri ());
378
+
379
+ try {
380
+ byte [] bytes = apiInstance .submitUDFBytes (split [0 ], split [1 ], multiArrayUDF , xPayer , "none" , "" );
381
+ return TileDBUtils .createValueVectors (bytes );
382
+ } catch (IOException | ApiException e ) {
383
+ e .printStackTrace ();
384
+ }
385
+ return null ;
386
+ }
387
+
188
388
/**
189
389
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
190
390
*
@@ -207,6 +407,25 @@ public String executeMultiArray(MultiArrayUDF multiArrayUDF, HashMap<String, Obj
207
407
return null ;
208
408
}
209
409
410
+ /**
411
+ * Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
412
+ *
413
+ * @param multiArrayUDF The multiArrayUDF input object
414
+ * @return The results as a String
415
+ */
416
+ public String executeMultiArray (MultiArrayUDF multiArrayUDF ){
417
+ try {
418
+ return apiInstance .submitMultiArrayUDFString (this .namespace , multiArrayUDF , "none" );
419
+ } catch (ApiException e ) {
420
+ System .err .println ("Exception when calling UdfApi#submitMultiArrayUDFString" );
421
+ System .err .println ("Status code: " + e .getCode ());
422
+ System .err .println ("Reason: " + e .getResponseBody ());
423
+ System .err .println ("Response headers: " + e .getResponseHeaders ());
424
+ e .printStackTrace ();
425
+ }
426
+ return null ;
427
+ }
428
+
210
429
/**
211
430
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
212
431
*
@@ -220,6 +439,18 @@ public JSONObject executeMultiArrayJSON(MultiArrayUDF multiArrayUDF, HashMap<Str
220
439
return new JSONObject (jsonString );
221
440
}
222
441
442
+ /**
443
+ * Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
444
+ *
445
+ * @param multiArrayUDF The multiArrayUDF input object
446
+ * @return The results as JSON object
447
+ */
448
+ public JSONObject executeMultiArrayJSON (MultiArrayUDF multiArrayUDF ){
449
+ multiArrayUDF .setResultFormat (ResultFormat .JSON );
450
+ String jsonString = this .executeMultiArray (multiArrayUDF );
451
+ return new JSONObject (jsonString );
452
+ }
453
+
223
454
/**
224
455
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
225
456
*
@@ -233,6 +464,18 @@ public JSONArray executeMultiArrayJSONArray(MultiArrayUDF multiArrayUDF, HashMap
233
464
return new JSONArray (jsonString );
234
465
}
235
466
467
+ /**
468
+ * Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
469
+ *
470
+ * @param multiArrayUDF The multiArrayUDF input object
471
+ * @return The results as JSON Array
472
+ */
473
+ public JSONArray executeMultiArrayJSONArray (MultiArrayUDF multiArrayUDF ){
474
+ multiArrayUDF .setResultFormat (ResultFormat .JSON );
475
+ String jsonString = this .executeMultiArray (multiArrayUDF );
476
+ return new JSONArray (jsonString );
477
+ }
478
+
236
479
/**
237
480
* Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
238
481
*
@@ -255,15 +498,21 @@ public Pair<ArrayList<ValueVector>, Integer> executeMultiArrayArrow(MultiArrayUD
255
498
}
256
499
257
500
/**
258
- * Serializes the arguments to a String
501
+ * Executes a multi-array-UDF. A multi- array-UDF is a UDF applied to multiple TileDB arrays
259
502
*
260
- * @param arguments The input arguments in a HashMap
261
- * @return The arguments in a String
503
+ * @param multiArrayUDF The multiArrayUDF input object
504
+ * @return The results in arrow format
262
505
*/
263
- private String serializeArgs (HashMap <String , Object > arguments ) {
264
- if (arguments == null || arguments .isEmpty ()) return "" ;
265
- Gson gson = new Gson ();
266
- return gson .toJson (arguments );
506
+ public Pair <ArrayList <ValueVector >, Integer > executeMultiArrayArrow (MultiArrayUDF multiArrayUDF ){
507
+ multiArrayUDF .setResultFormat (ResultFormat .ARROW );
508
+
509
+ try {
510
+ byte [] bytes = apiInstance .submitMultiArrayUDFBytes (this .namespace , multiArrayUDF , "none" );
511
+ return TileDBUtils .createValueVectors (bytes );
512
+ } catch (IOException | ApiException e ) {
513
+ e .printStackTrace ();
514
+ }
515
+ return null ;
267
516
}
268
517
269
518
/**
0 commit comments