L Robin,

I have an example of using LuaJ in ExecuteScript on my blog [1], but I
believe you have found that and are looking for an example to also
read in an incoming flow file and manipulate the contents.  Often the
scripting examples use Apache Commons' IOUtils class to read the
entire flow file content in as a string, then manipulate it after the
fact. However LuaJ has a bug (discussed on this list a while ago [2])
where it only uses the system classloader and thus won't have access
to the additional classes provided to the scripting NAR.

I will write this up as a follow-on blog post shortly, but in the
meantime, here is a working example of a Lua script that reverses each
line in an incoming flow file (and adds an attribute afterwards):

flowFile = session:get()
if flowFile == nil then
  return
end

local writecb =
luajava.createProxy("org.apache.nifi.processor.io.StreamCallback", {
    process = function(inputStream, outputStream)
      local isr = luajava.newInstance('java.io.InputStreamReader', inputStream)
      local br = luajava.newInstance('java.io.BufferedReader', isr)
      local line = br:readLine()
      while line ~= nil do
         -- Do stuff to each line here
         outputStream:write(line:reverse())
         line = br:readLine()
         if line ~= nil then
           outputStream:write('\n')
         end
      end
    end
})

flowFile = session:putAttribute(flowFile, "lua.attrib", "my attribute value")
flowFile = session:write(flowFile, writecb)
session:transfer(flowFile, REL_SUCCESS)


Regards,
Matt

[1] 
https://funnifi.blogspot.com/2016/04/using-lua-with-executescript-in-nifi.html
[2] 
http://apache-nifi.1125220.n5.nabble.com/Lua-usage-in-ExecuteScript-Processor-td6577.html

On Tue, Mar 14, 2017 at 5:36 AM, L Robin <robinl8...@gmail.com> wrote:
> Hi,
> I am using lua with ExecuteScript,and i am must get the flowFile from 
> upstream for use session:get() and then i must deal with the flowFile,in the 
> end write the flowFile to the downstream,can you help me and give me a simple 
> example.Thank You!!

Reply via email to