cancel
Showing results for 
Search instead for 
Did you mean: 

How to get meta data from a document set using script snap?

krish_pwc
New Contributor II

I want to get the meta data of a document using a script snap.i prepared the below javascript using script snap. Its working fine at validation time but failing at runtime.
please find my script below.

// Ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines
try { load("nashorn:mozilla_compat.js"); } catch(e) { }

// Import the interface required by the Script snap.
importPackage(com.snaplogic.scripting.language);

// Import the Java utility classes.
importPackage(java.util);

/**
 * Create an object that implements the methods defined by the "ScriptHook"
 * interface.  We'll be passing this object to the constructor for the
 * ScriptHook interface.
 */
var impl = {
    /*
     * These variables (input, output, error, log) are defined by the
     * ExecuteScript snap when evaluating this script.
     */
    input : input,
    output : output,
    error : error,
    log : log,

    /**
     * The "execute()" method is called once when the pipeline is started
     * and allowed to process its inputs or just send data to its outputs.
     *
     * Exceptions are automatically caught and sent to the error view.
     */
    execute : function () {
       this.log.info("Executing Transform Script");
      // var i=0;
        if (this.input.hasNext() ) {
            try{
                // Read the next document, wrap it in a map and write out the wrapper
                
                var doc = this.input.next();
                 var keyArray = doc.keySet().toArray();
                  var wrapperOut ={};
                 for( var index in keyArray) 
                        {
                             var new_data = {};
                            var key = keyArray[index];
                            var type=(!isNaN(parseInt(doc.get(key))) ? 'int' : 'binary');
                           //  wrapperOut.put("data_type",type);
                           //  wrapperOut.put("col_name",key);
                           wrapperOut.data_type=type;
                           wrapperOut.col_name=key;
                          output.write(wrapperOut);
                          //  wrapperOut.add(new_data);
                            
                        }
               
             //   wrapper.put("original", doc);
              //  
                this.log.info("Transform Script finished");
               
               
            }
            catch(err) {
                var wrapper = new java.util.HashMap();
                wrapper.put("errorMsg", err);
                this.log.error(err);
                this.error.write(wrapper);
            }
           // i++;
        }
      //  output.write(wrapperOut);
        this.log.info(" data"+wrapperOut.toString());
       // this.output.write(doc, wrapperOut);
    }
};

/**
 * The Script Snap will look for a ScriptHook object in the "hook"
 * variable.  The snap will then call the hook's "execute" method.
 */
var hook = new com.snaplogic.scripting.language.ScriptHook(impl);

Where is wrong with this script?Its failing at runtime giving below error:
java.io.IOException: Failed to close subscriber

If the document view is:
items color price

car red 1100
mango green 5

Then my desired output is:
column name type

items string
color string
price int

1 REPLY 1

krish_pwc
New Contributor II

The error in the script was due to partial iteration of the, input doc.
The input doc is to be fully iterated otherwise this error will come at runtime like the input is not fully consumed. if required we can break the script into two script.