javaembed-stream.ecl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. //class=embedded
  14. //class=3rdparty
  15. import java;
  16. /*
  17. Similar to java-stream example, but using inline Java
  18. */
  19. // Passing and returning records and datasets
  20. // When passing/returning a record, the corresponding Java function should take/return an object as a parameter whose fields
  21. // can be mapped by name to the ECL record fields
  22. nrec := record
  23. utf8 ufield;
  24. end;
  25. jret := RECORD
  26. boolean bfield;
  27. integer4 ifield;
  28. integer8 lfield;
  29. real8 dfield;
  30. real4 ffield;
  31. string1 cfield1;
  32. string1 cfield2;
  33. string sfield;
  34. nrec n;
  35. set of boolean bset;
  36. set of data dset;
  37. set of string sset;
  38. LINKCOUNTED DATASET(nrec) sub;
  39. end;
  40. jret returnrec(boolean b, integer i, real8 d) := EMBED(java)
  41. import java.util.*;
  42. public class Test1
  43. {
  44. public static class NestedClass
  45. {
  46. String ufield;
  47. public NestedClass(String s)
  48. {
  49. ufield = s;
  50. }
  51. public NestedClass()
  52. {
  53. }
  54. }
  55. boolean bfield;
  56. int ifield;
  57. long lfield;
  58. double dfield;
  59. float ffield;
  60. String sfield;
  61. char cfield1;
  62. String cfield2;
  63. NestedClass n;
  64. boolean bset[];
  65. byte [] dset[];
  66. String sset[];
  67. NestedClass sub[];
  68. public Test1(boolean b, int i, double d)
  69. {
  70. bfield = b;
  71. ifield = i;
  72. lfield = i * 100000000;
  73. dfield = d;
  74. ffield = (float) d;
  75. sfield = "Yoohoo";
  76. cfield1 = 'X';
  77. cfield2 = "Z";
  78. n = new NestedClass("nest");
  79. bset = new boolean [5];
  80. bset[3] = b;
  81. dset = new byte[1][];
  82. dset[0] = new byte[1];
  83. dset[0][0] = 14;
  84. sset = new String[1];
  85. sset[0] = "Hello";
  86. sub = new NestedClass[1];
  87. sub[0] = new NestedClass("subnest");
  88. }
  89. public Test1()
  90. {
  91. n = new NestedClass("nest2");
  92. }
  93. public static Test1 returnrec(boolean b, int i, double d)
  94. {
  95. return new Test1(b,i,d);
  96. }
  97. }
  98. ENDEMBED;
  99. STRING passrec(jret r) := EMBED(java)
  100. import java.util.*;
  101. public class Test2
  102. {
  103. public static class NestedClass
  104. {
  105. String ufield;
  106. public NestedClass(String s)
  107. {
  108. ufield = s;
  109. }
  110. public NestedClass()
  111. {
  112. }
  113. }
  114. boolean bfield;
  115. int ifield;
  116. long lfield;
  117. double dfield;
  118. float ffield;
  119. String sfield;
  120. char cfield1;
  121. String cfield2;
  122. NestedClass n;
  123. boolean bset[];
  124. byte [] dset[];
  125. String sset[];
  126. NestedClass sub[];
  127. public Test2()
  128. {
  129. n = new NestedClass("nest2");
  130. }
  131. public static String passrec(Test2 j)
  132. {
  133. return j.n.ufield;
  134. }
  135. }
  136. ENDEMBED;
  137. ret := returnrec(false, 10, 2.345);
  138. OUTPUT(ret); // Calls a Java function that returns an ECL record
  139. OUTPUT(passrec(ret)); // Passes an ECL record to a Java function
  140. // When passing a dataset to a Java function, the Java function should take either an array or an iterator of objects,
  141. // where the fields of the object in question are mapped by name to the fields in the ECL record.
  142. //
  143. // To return a dataset, an iterator must be returned.
  144. INTEGER passDataset(LINKCOUNTED DATASET(jret) d) := EMBED(Java)
  145. import java.util.*;
  146. public class Test3
  147. {
  148. public static class NestedClass
  149. {
  150. String ufield;
  151. public NestedClass(String s)
  152. {
  153. ufield = s;
  154. }
  155. public NestedClass()
  156. {
  157. }
  158. }
  159. boolean bfield;
  160. int ifield;
  161. long lfield;
  162. double dfield;
  163. float ffield;
  164. String sfield;
  165. char cfield1;
  166. String cfield2;
  167. NestedClass n;
  168. boolean bset[];
  169. byte [] dset[];
  170. String sset[];
  171. NestedClass sub[];
  172. public Test3()
  173. {
  174. n = new NestedClass("nest2");
  175. }
  176. public static int passDataset(Iterator<Test3> d)
  177. {
  178. int sum = 0;
  179. while (d.hasNext())
  180. {
  181. Test3 r = d.next();
  182. sum += r.lfield;
  183. }
  184. return sum;
  185. }
  186. }
  187. ENDEMBED;
  188. DATASET(jret) passDataset2(LINKCOUNTED DATASET(jret) d) := EMBED(Java)
  189. import java.util.*;
  190. public class Test4
  191. {
  192. public static class NestedClass
  193. {
  194. String ufield;
  195. public NestedClass(String s)
  196. {
  197. ufield = s;
  198. }
  199. public NestedClass()
  200. {
  201. }
  202. }
  203. boolean bfield;
  204. int ifield;
  205. long lfield;
  206. double dfield;
  207. float ffield;
  208. String sfield;
  209. char cfield1;
  210. String cfield2;
  211. NestedClass n;
  212. boolean bset[];
  213. byte [] dset[];
  214. String sset[];
  215. NestedClass sub[];
  216. public Test4()
  217. {
  218. n = new NestedClass("nest2");
  219. }
  220. public static Iterator<Test4> passDataset2(Test4 d[])
  221. {
  222. return Arrays.asList(d).iterator();
  223. }
  224. }
  225. ENDEMBED;
  226. ds := DATASET(
  227. [
  228. {true, 1,2,3,4,'a', 'b', 'cd', u'ef', [true,false], [], ['Hello from ECL'], [{'1'},{'2'},{'3'},{'4'},{'5'}]}
  229. ,{true, 2,4,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
  230. ,{true, 3,6,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
  231. ,{true, 8,8,3,4,'a', 'b', 'cd', u'ef', [true,false], [d'AA55'], [], []}
  232. ], jret);
  233. output(passDataset(ds)); // Using an iterator
  234. output(passDataset2(ds)); // using an array, and illustrating the return of a dataset
  235. // It is also possible to code a traonsform function in Java - both the parameter and the return type should be a
  236. // Java object type that maps the fields of the ECL record by name.
  237. transform(jret) testTransform(jret in, integer lim) := EMBED(java)
  238. import java.util.*;
  239. public class Test5
  240. {
  241. public static class NestedClass
  242. {
  243. String ufield;
  244. public NestedClass(String s)
  245. {
  246. ufield = s;
  247. }
  248. public NestedClass()
  249. {
  250. }
  251. }
  252. boolean bfield;
  253. int ifield;
  254. long lfield;
  255. double dfield;
  256. float ffield;
  257. String sfield;
  258. char cfield1;
  259. String cfield2;
  260. NestedClass n;
  261. boolean bset[];
  262. byte [] dset[];
  263. String sset[];
  264. NestedClass sub[];
  265. public Test5(boolean b, int i, double d)
  266. {
  267. bfield = b;
  268. ifield = i;
  269. lfield = i * 100000000;
  270. dfield = d;
  271. ffield = (float) d;
  272. sfield = "Yoohoo";
  273. cfield1 = 'X';
  274. cfield2 = "Z";
  275. n = new NestedClass("nest");
  276. bset = new boolean [5];
  277. bset[3] = b;
  278. dset = new byte[1][];
  279. dset[0] = new byte[1];
  280. dset[0][0] = 14;
  281. sset = new String[1];
  282. sset[0] = "Hello";
  283. sub = new NestedClass[1];
  284. sub[0] = new NestedClass("subnest");
  285. }
  286. public Test5()
  287. {
  288. n = new NestedClass("nest2");
  289. }
  290. public static Test5 testTransform(Test5 in, int lim)
  291. {
  292. return new Test5(in.bfield, lim, in.dfield);
  293. }
  294. }
  295. ENDEMBED;
  296. output(project(ds, testTransform(LEFT, COUNTER)));