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/5.0 (windows nt 6.1; wow64; rv:52.0) gecko/20100101 firefox/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>//script here

// var parsingresult = uaparser("mozilla/5.0 (windows nt 6.1; wow64; rv:52.0) gecko/20100101 firefox/52.0", "")
var parsingresult = uaparser(useragent, "")
var stepresult = json.stringify(parsingresult)</jsscript_script> </jsscript> <jsscript> <jsscript_type>1</jsscript_type> <jsscript_name>item_0</jsscript_name> <jsscript_script>//script here

var transformationpath = getvariable("internal.transformation.filename.directory", "");
var jsscriptpath = transformationpath + "/" + "ua-parser.js"
loadscriptfile(jsscriptpath);</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
Post a Comment