javascript - Pentaho Data Integration: How to load and call method of external library JS at Pentaho(using in Modified Java Script Value Step)? -


currently, have js library splits user agent string in request heads components browser, browserversion, operatingsystem, device, os, , on. have library (.js), can me how add , use in modified java script value? plz. want see simple example of or similar. enter image description here

this file js: https://drive.google.com/file/d/0b5fsakwz2dztcjrjcg15utfjyue/view?usp=sharing

you can use loadscriptfile(jsfilepath) function in order load execute js script file.

you can call function on start script tab avoid executing entire script every input row, call uaparser constructor on transform script tab.

example:

start script (executed once, after first input row arrives. create start script, right-click on "script1" tab header, select "add new", select new tab, right-click on it, select "set transform script")

// assuming, ua-parser.js in transformation directory: var transformationpath = getvariable("internal.transformation.filename.directory", ""); var jsscriptpath = transformationpath + "/" + "ua-parser.js" loadscriptfile(jsscriptpath); 

transform script (executed every row)

// use field user-agent data instead of hard-coded "mozilla..." string: var parsingresult = uaparser(         "mozilla/5.0 (windows nt 6.1; wow64; rv:52.0) gecko/20100101 firefox/52.0", "") // converting parsingresult json string demonstration purpose var stepresult = json.stringify(parsingresult) 

output:

the stepresult column have following value:

{"ua":"mozilla/5.0 (windows nt 6.1; wow64; rv:52.0) gecko/20100101 firefox/52.0","browser":{"name":"firefox","version":"52.0","major":"52"},"engine":{"version":"52.0","name":"gecko"},"os":{"name":"windows","version":"7"},"device":{},"cpu":{}}

transformation steps xml (for pdi 7.0): here xml, shows how integrate script input steps (using data grid input step). can copy following xml, paste on transformation canvas, save in same folder ua-parser.js file located, transformation should execute.

<?xml version="1.0" encoding="utf-8"?> <transformation-steps> <steps>   <step>     <name>data grid</name>     <type>datagrid</type>     <description/>     <distribute>y</distribute>     <custom_distribution/>     <copies>1</copies>     <partitioning>       <method>none</method>       <schema_name/>     </partitioning>     <fields>       <field>         <name>useragent</name>         <type>string</type>         <format/>         <currency/>         <decimal/>         <group/>         <length>-1</length>         <precision>-1</precision>         <set_empty_string>n</set_empty_string>       </field>     </fields>     <data>       <line> <item>mozilla&#x2f;5.0 &#x28;windows nt 6.1&#x3b; wow64&#x3b; rv&#x3a;52.0&#x29; gecko&#x2f;20100101 firefox&#x2f;52.0</item> </line>     </data>     <cluster_schema/>     <remotesteps>       <input>       </input>       <output>       </output>     </remotesteps>     <gui>       <xloc>144</xloc>       <yloc>128</yloc>       <draw>y</draw>     </gui>     </step>    <step>     <name>modified java script value</name>     <type>scriptvaluemod</type>     <description/>     <distribute>y</distribute>     <custom_distribution/>     <copies>1</copies>     <partitioning>       <method>none</method>       <schema_name/>     </partitioning>     <compatible>n</compatible>     <optimizationlevel>9</optimizationlevel>     <jsscripts>      <jsscript>        <jsscript_type>0</jsscript_type>         <jsscript_name>script 1</jsscript_name>         <jsscript_script>&#x2f;&#x2f;script here&#xd;&#xa;&#xd;&#xa;&#x2f;&#x2f; var parsingresult &#x3d; uaparser&#x28;&#x22;mozilla&#x2f;5.0 &#x28;windows nt 6.1&#x3b; wow64&#x3b; rv&#x3a;52.0&#x29; gecko&#x2f;20100101 firefox&#x2f;52.0&#x22;, &#x22;&#x22;&#x29;&#xd;&#xa;var parsingresult &#x3d; uaparser&#x28;useragent, &#x22;&#x22;&#x29;&#xd;&#xa;var stepresult &#x3d; json.stringify&#x28;parsingresult&#x29;</jsscript_script>       </jsscript>      <jsscript>        <jsscript_type>1</jsscript_type>         <jsscript_name>item_0</jsscript_name>         <jsscript_script>&#x2f;&#x2f;script here&#xd;&#xa;&#xd;&#xa;var transformationpath &#x3d; getvariable&#x28;&#x22;internal.transformation.filename.directory&#x22;, &#x22;&#x22;&#x29;&#x3b;&#xd;&#xa;var jsscriptpath &#x3d; transformationpath &#x2b; &#x22;&#x2f;&#x22; &#x2b; &#x22;ua-parser.js&#x22;&#xd;&#xa;loadscriptfile&#x28;jsscriptpath&#x29;&#x3b;</jsscript_script>       </jsscript>    </jsscripts>    <fields>      <field>        <name>stepresult</name>         <rename>stepresult</rename>         <type>string</type>         <length>-1</length>         <precision>-1</precision>         <replace>n</replace>       </field>    </fields>    <cluster_schema/>     <remotesteps>       <input>       </input>       <output>       </output>     </remotesteps>     <gui>       <xloc>320</xloc>       <yloc>128</yloc>       <draw>y</draw>     </gui>     </step>  </steps> <order>     <hop>       <from>data grid</from>       <to>modified java script value</to>       <enabled>y</enabled>     </hop>  </order> <notepads> </notepads> <step_error_handling> </step_error_handling> </transformation-steps> 

note: library seems work pdi. in general libraries not work pdi, since pdi javascript quite different js in browsers/node.js: pdi js misses browser-specific objects, "window" or "document", , objects in pdi have unexpected behavior, since may unwrapped java objects, not javascript objects.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -