java-stream.ecl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import java;
  2. /*
  3. This example illustrates various calls to Java functions defined in the Java module JavaCat.
  4. The source of JavaCat can be found in the examples directory - it can be compiled to JavaCat.class
  5. using
  6. javac JavaCat
  7. and the resulting file JavaCat.class should be placed in /opt/HPCCSystems/classes (or somewhere else
  8. where it can be located via the standard Java CLASSPATH environment variable.
  9. */
  10. // Passing and returning records and datasets
  11. // When passing/returning a record, the corresponding Java function should take/return an object as a parameter whose fields
  12. // can be mapped by name to the ECL record fields
  13. nrec := record
  14. utf8 ufield;
  15. end;
  16. jret := RECORD
  17. boolean bfield;
  18. integer4 ifield;
  19. integer8 lfield;
  20. real8 dfield;
  21. real4 ffield;
  22. string1 cfield1;
  23. string1 cfield2;
  24. string sfield;
  25. nrec n;
  26. set of boolean bset;
  27. set of data dset;
  28. set of string sset;
  29. LINKCOUNTED DATASET(nrec) sub;
  30. end;
  31. jret jreturnrec(boolean b, integer i, real8 d) := IMPORT(java, 'JavaCat.returnrec:(ZID)LJavaCat;');
  32. STRING jpassrec(jret r) := IMPORT(java, 'JavaCat.passrec:(LJavaCat;)Ljava/lang/String;');
  33. ret := jreturnrec(false, 10, 2.345);
  34. OUTPUT(ret); // Calls a Java function that returns an ECL record
  35. OUTPUT(jpassrec(ret)); // Passes an ECL record to a Java function
  36. // When passing a dataset to a Java function, the Java function should take either an array or an iterator of objects,
  37. // where the fields of the object in question are mapped by name to the fields in the ECL record.
  38. // When passing an iterator, we use a modified form of the function signature in the IMPORT statement, using a < to indicate "Iterator of"
  39. // followed by the name of the class of the objects that the iterator is to return. This is the one case where the output of javap -s cannot be used
  40. // directly to provide the signature of the java function being called. We can also use the "extended" signature of the method that is output by
  41. // javap -s -v, of the form 'JavaCat.passDataset:(Ljava/util/Iterator<LJavaCat;>;)I'
  42. //
  43. // To return a dataset, an iterator must be returned.
  44. INTEGER passDataset(LINKCOUNTED DATASET(jret) d) :=
  45. IMPORT(java, 'JavaCat.passDataset:(<LJavaCat;)I'); // Calls int passDataset(Iterator<JavaCat> d)
  46. // Note we could also use 'Ljava/util/Iterator<LJavaCat;>;)I' as the signature, but not 'Ljava/util/Iterator;)I' which is the signature output by javap -s
  47. DATASET(jret) passDataset2(LINKCOUNTED DATASET(jret) d) :=
  48. IMPORT(java, 'JavaCat.passDataset2:([LJavaCat;)Ljava/util/Iterator;'); // Calls Iterator<JavaCat> passDataset2(JavaCat d[])
  49. ds := DATASET(
  50. [
  51. {true, 1,2,3,4,'a', 'b', 'cd', u'ef', [true,false], [], ['Hello from ECL'], [{'1'},{'2'},{'3'},{'4'},{'5'}]}
  52. ,{true, 2,4,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
  53. ,{true, 3,6,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
  54. ,{true, 8,8,3,4,'a', 'b', 'cd', u'ef', [true,false], [d'AA55'], [], []}
  55. ], jret);
  56. output(passDataset(ds)); // Using an iterator
  57. output(passDataset2(ds)); // using an array, and illustrating the return of a dataset
  58. // It is also possible to code a traonsform function in Java - both the parameter and the return type should be a
  59. // Java object type that maps the fields of the ECL record by name.
  60. transform(jret) testTransform(jret in, integer lim) := IMPORT(java, 'JavaCat.transform:(LJavaCat;I)LJavaCat;');
  61. output(passDataset2(ds));
  62. output(project(ds, testTransform(LEFT, COUNTER)));