PrG_Automated_ECL.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <sect1 id="Automated_ECL">
  5. <title><emphasis role="bold">Automated ECL</emphasis></title>
  6. <para>Once you have established standard ECL processes that you know you
  7. need to perform regularly, you can begin to make those processes automated.
  8. Doing this eliminates the need to remember the order of processes, or their
  9. periodicity.</para>
  10. <para>One form of automation typically involves launching MACROs with the
  11. ECLPlus application. By using MACROs, you can have standard processes that
  12. operate on different input each time, but produce the same result. Since
  13. ECLPlus is a command-line application, its use can be automatically launched
  14. in many different ways — DOS Batch files, from within another application,
  15. or ...</para>
  16. <para>Here's an example. This MACRO (contained in DeclareData.ECL) takes two
  17. parameters: the name of a file, and the name of a field in that file to
  18. produce a count of the unique values in that field and a crosstab report of
  19. the number of instances of each value.</para>
  20. <programlisting>EXPORT MAC_CountFieldValues(infile,infield) := MACRO
  21. // Create the count of unique values in the infield
  22. COUNT(DEDUP(TABLE(infile,{infile.infield}),infield,ALL));
  23. // Create the crosstab report
  24. #UNIQUENAME(r_macro)
  25. %r_macro% := RECORD
  26. infile.infield;
  27. INTEGER cnt := COUNT(GROUP);
  28. END;
  29. #UNIQUENAME(y_macro)
  30. %y_macro% := TABLE(infile,%r_macro%,infield,FEW);
  31. OUTPUT(CHOOSEN(%y_macro%,50000));
  32. ENDMACRO;
  33. </programlisting>
  34. <para>By using #UNIQUENAME to generate all the attribute names, this MACRO
  35. can be used multiple times in the same workunit. You can test the MACRO
  36. through the ECL IDE program by executing a query like this in the ECL
  37. Builder window:</para>
  38. <programlisting>IMPORT ProgrammersGuide AS PG;
  39. PG.DeclareData.MAC_CountFieldValues(PG.DeclareData.Person.file,gender);
  40. </programlisting>
  41. <para>Once you've throughly tested the MACRO and are certain it works
  42. correctly, you can automate the process by using ECLplus.</para>
  43. <para>Install the ECLplus program in its own directory on the same PC that
  44. runs the ECL IDE, and create an ECLPLUS.INI file in the same folder with the
  45. correct settings to access your cluster (see the <emphasis>Command Line
  46. ECL</emphasis> section of the <emphasis>Client Tools </emphasis>PDF). Then
  47. you can open a Command Prompt window and run the same query from the command
  48. line like this:</para>
  49. <programlisting>C:\eclplus&gt;eclplus ecl=$ProgGuide.MAC_CountFieldValues(ProgrammersGuide.DeclareData.Person.File,gender)</programlisting>
  50. <para>Notice that you're using the <emphasis>ecl=</emphasis> command line
  51. option and not the <emphasis>$Module.Attribute</emphasis> option. This is
  52. the “proper” way to make a MACRO expand and execute through ECLplus. The
  53. <emphasis>$Module.Attribute</emphasis> option is only used to execute ECL
  54. Builder window queries that have been saved as attributes in the repository
  55. (Builder Window Runnable—BWR code) and won't work with MACROs.</para>
  56. <para>When the MACRO expands and executes, you get a result that looks like
  57. this in your Command Prompt window:</para>
  58. <programlisting>Workunit W20070118-145647 submitted
  59. [Result 1]
  60. Result_1
  61. 2
  62. [Result_2]
  63. gender cnt
  64. F 500000
  65. M 500000
  66. </programlisting>
  67. <para>You can re-direct this output to a file by using the
  68. <emphasis>output=”filename”</emphasis> option on the command line, like
  69. this:</para>
  70. <programlisting>C:\eclplus&gt;eclplus ecl=$ProgGuide.MAC_CountFieldValues( ProgrammersGuide.DeclareData.Person.File, gender)
  71. output="MyFile.txt"</programlisting>
  72. <para>This will send the output to the “MyFile.txt” file on your local PC.
  73. For larger output files, you'll want to have the OUTPUT action in your ECL
  74. code write the result set to disk in the supercomputer then de-spray it to
  75. your landing zone (you can use the Standard Library's File.Despray function
  76. to do this from within your ECL code).</para>
  77. <sect2 id="Using_Text_Files">
  78. <title>Using Text Files</title>
  79. <para>Another automation option is to generate a text file containing the
  80. ECL code to execute, then execute that code from the command line.</para>
  81. <para>For example, you could create a file containing this:</para>
  82. <programlisting>IMPORT ProgrammersGuide AS PG;
  83. PG.DeclareData.MAC_CountFieldValues(PG.DeclareData.Person.file,gender);
  84. PG.DeclareData.MAC_CountFieldValues(PG.DeclareData.person.File,state)</programlisting>
  85. <para>These two MACRO calls will generate the field ordinality count and
  86. crosstab report for two fields in the same file. You could then execute
  87. them like this (where “test.ECL” is the name of the file you
  88. created):</para>
  89. <programlisting>C:\eclplus&gt;eclplus @test.ecl</programlisting>
  90. <para>This will generate similar results to that above.</para>
  91. <para>The advantage this method has is the ability to include any
  92. necessary “setup” ECL code in the file before the MACRO calls, like this
  93. (contained in RunText.ECL):</para>
  94. <programlisting>IMPORT ProgrammersGuide AS PG;
  95. MyRec := RECORD
  96. STRING1 value1;
  97. STRING1 value2;
  98. END;
  99. D := DATASET([{'A','B'},
  100. {'B','C'},
  101. {'A','D'},
  102. {'B','B'},
  103. {'A','C'},
  104. {'B','D'},
  105. {'A','B'},
  106. {'C','C'},
  107. {'C','D'},
  108. {'A','A'}],MyRec);
  109. PG.DeclareData.MAC_CountFieldValues(D,Value1)
  110. PG.DeclareData.MAC_CountFieldValues(D,Value2)
  111. </programlisting>
  112. <para>So that you get a result like this:</para>
  113. <programlisting>C:\eclplus&gt;eclplus @test.ecl
  114. Workunit W20070118-145647 submitted
  115. [Result 1]
  116. result_1
  117. 3
  118. [Result 2]
  119. value1 cnt
  120. C 2
  121. A 5
  122. B 3
  123. [Result 3]
  124. result_3
  125. 4
  126. [Result 4]
  127. value2 cnt
  128. D 3
  129. C 3
  130. A 1
  131. B 3
  132. </programlisting>
  133. <para>How you create this text file is up to you. To fully automate the
  134. process you may want to write a daemon application that watches a
  135. directory (such as your HPCC environment's landing zone) to detect new
  136. files dropped in (by whatever means) and generate the appropriate ECL code
  137. file to process that new file in some standard fashion (typically using
  138. MACRO calls), then execute it from ECLplus command line as described
  139. above. The realm of possibilities is endless.</para>
  140. </sect2>
  141. </sect1>