123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- //class=embedded
- //class=3rdparty
- import java;
- /*
- Similar to java-stream example, but using inline Java
- */
- // Passing and returning records and datasets
- // When passing/returning a record, the corresponding Java function should take/return an object as a parameter whose fields
- // can be mapped by name to the ECL record fields
- nrec := record
- utf8 ufield;
- end;
- jret := RECORD
- boolean bfield;
- integer4 ifield;
- integer8 lfield;
- real8 dfield;
- real4 ffield;
- string1 cfield1;
- string1 cfield2;
- string sfield;
- nrec n;
- set of boolean bset;
- set of data dset;
- set of string sset;
- LINKCOUNTED DATASET(nrec) sub;
- end;
- jret returnrec(boolean b, integer i, real8 d) := EMBED(java)
- import java.util.*;
- public class Test1
- {
- public static class NestedClass
- {
- String ufield;
- public NestedClass(String s)
- {
- ufield = s;
- }
- public NestedClass()
- {
- }
- }
- boolean bfield;
- int ifield;
- long lfield;
- double dfield;
- float ffield;
- String sfield;
- char cfield1;
- String cfield2;
- NestedClass n;
- boolean bset[];
- byte [] dset[];
- String sset[];
- NestedClass sub[];
- public Test1(boolean b, int i, double d)
- {
- bfield = b;
- ifield = i;
- lfield = i * 100000000;
- dfield = d;
- ffield = (float) d;
- sfield = "Yoohoo";
- cfield1 = 'X';
- cfield2 = "Z";
- n = new NestedClass("nest");
- bset = new boolean [5];
- bset[3] = b;
- dset = new byte[1][];
- dset[0] = new byte[1];
- dset[0][0] = 14;
- sset = new String[1];
- sset[0] = "Hello";
- sub = new NestedClass[1];
- sub[0] = new NestedClass("subnest");
- }
- public Test1()
- {
- n = new NestedClass("nest2");
- }
- public static Test1 returnrec(boolean b, int i, double d)
- {
- return new Test1(b,i,d);
- }
- }
- ENDEMBED;
- STRING passrec(jret r) := EMBED(java)
- import java.util.*;
- public class Test2
- {
- public static class NestedClass
- {
- String ufield;
- public NestedClass(String s)
- {
- ufield = s;
- }
- public NestedClass()
- {
- }
- }
- boolean bfield;
- int ifield;
- long lfield;
- double dfield;
- float ffield;
- String sfield;
- char cfield1;
- String cfield2;
- NestedClass n;
- boolean bset[];
- byte [] dset[];
- String sset[];
- NestedClass sub[];
- public Test2()
- {
- n = new NestedClass("nest2");
- }
- public static String passrec(Test2 j)
- {
- return j.n.ufield;
- }
- }
- ENDEMBED;
- ret := returnrec(false, 10, 2.345);
- OUTPUT(ret); // Calls a Java function that returns an ECL record
- OUTPUT(passrec(ret)); // Passes an ECL record to a Java function
- // When passing a dataset to a Java function, the Java function should take either an array or an iterator of objects,
- // where the fields of the object in question are mapped by name to the fields in the ECL record.
- //
- // To return a dataset, an iterator must be returned.
- INTEGER passDataset(LINKCOUNTED DATASET(jret) d) := EMBED(Java)
- import java.util.*;
- public class Test3
- {
- public static class NestedClass
- {
- String ufield;
- public NestedClass(String s)
- {
- ufield = s;
- }
- public NestedClass()
- {
- }
- }
- boolean bfield;
- int ifield;
- long lfield;
- double dfield;
- float ffield;
- String sfield;
- char cfield1;
- String cfield2;
- NestedClass n;
- boolean bset[];
- byte [] dset[];
- String sset[];
- NestedClass sub[];
- public Test3()
- {
- n = new NestedClass("nest2");
- }
- public static int passDataset(Iterator<Test3> d)
- {
- int sum = 0;
- while (d.hasNext())
- {
- Test3 r = d.next();
- sum += r.lfield;
- }
- return sum;
- }
- }
- ENDEMBED;
- DATASET(jret) passDataset2(LINKCOUNTED DATASET(jret) d) := EMBED(Java)
- import java.util.*;
- public class Test4
- {
- public static class NestedClass
- {
- String ufield;
- public NestedClass(String s)
- {
- ufield = s;
- }
- public NestedClass()
- {
- }
- }
- boolean bfield;
- int ifield;
- long lfield;
- double dfield;
- float ffield;
- String sfield;
- char cfield1;
- String cfield2;
- NestedClass n;
- boolean bset[];
- byte [] dset[];
- String sset[];
- NestedClass sub[];
- public Test4()
- {
- n = new NestedClass("nest2");
- }
- public static Iterator<Test4> passDataset2(Test4 d[])
- {
- return Arrays.asList(d).iterator();
- }
- }
- ENDEMBED;
- ds := DATASET(
- [
- {true, 1,2,3,4,'a', 'b', 'cd', u'ef', [true,false], [], ['Hello from ECL'], [{'1'},{'2'},{'3'},{'4'},{'5'}]}
- ,{true, 2,4,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
- ,{true, 3,6,3,4,'a', 'b', 'cd', u'ef', [true,false], [], [], []}
- ,{true, 8,8,3,4,'a', 'b', 'cd', u'ef', [true,false], [d'AA55'], [], []}
- ], jret);
- output(passDataset(ds)); // Using an iterator
- output(passDataset2(ds)); // using an array, and illustrating the return of a dataset
- // It is also possible to code a traonsform function in Java - both the parameter and the return type should be a
- // Java object type that maps the fields of the ECL record by name.
- transform(jret) testTransform(jret in, integer lim) := EMBED(java)
- import java.util.*;
- public class Test5
- {
- public static class NestedClass
- {
- String ufield;
- public NestedClass(String s)
- {
- ufield = s;
- }
- public NestedClass()
- {
- }
- }
- boolean bfield;
- int ifield;
- long lfield;
- double dfield;
- float ffield;
- String sfield;
- char cfield1;
- String cfield2;
- NestedClass n;
- boolean bset[];
- byte [] dset[];
- String sset[];
- NestedClass sub[];
- public Test5(boolean b, int i, double d)
- {
- bfield = b;
- ifield = i;
- lfield = i * 100000000;
- dfield = d;
- ffield = (float) d;
- sfield = "Yoohoo";
- cfield1 = 'X';
- cfield2 = "Z";
- n = new NestedClass("nest");
- bset = new boolean [5];
- bset[3] = b;
- dset = new byte[1][];
- dset[0] = new byte[1];
- dset[0][0] = 14;
- sset = new String[1];
- sset[0] = "Hello";
- sub = new NestedClass[1];
- sub[0] = new NestedClass("subnest");
- }
- public Test5()
- {
- n = new NestedClass("nest2");
- }
- public static Test5 testTransform(Test5 in, int lim)
- {
- return new Test5(in.bfield, lim, in.dfield);
- }
- }
- ENDEMBED;
- output(project(ds, testTransform(LEFT, COUNTER)));
|