Skip to content
rohitjoshi edited this page Sep 9, 2012 · 7 revisions

Here is an example of how word count is performed using lua-mapreduce.

example/task-file.lua is a sample task-file for work count which implements following functions.

Server required functions:

  1. taskfn: It reads the source and creates the map of the tasks. E.g For word count, it reads the all file with .lua from current directory and creates a map with key as a file name and content as a value.

    mr.taskfn = function() --logger:debug("Getting map task") local tasks = read_source() -- read source is utility function defined to read data source for key, value in pairs(tasks) do coroutine.yield(key, value) end end Here read_source() is a local function defined as below. NOTE: it uses luafilesystem (lfs) module to read the files

    local function read_source() --local file_path = system.pathForFile( "*.lua", lfs.currentdir() ) local file_path = lfs.currentdir() --logger:debug("Current directory path:" .. file_path) local source_table = {} for file in lfs.dir(file_path) do if(string.find(file, ".lua") ~= nil) then -- logger:debug("File name:" .. file_path .. "/" .. file) local c = read_file(file_path .. "/" .. file) -- logger:debug("file:" .. file .. ", length:" .. #c)

     	if( c ~= nil) then
     		source_table[file]=c
     	end
     end
    

    end return source_table end

  2. finalfn : How to output final result. Here it prints on the console. mr.finalfn = function (results) print("Final results of the task:") for key, value in pairs(results) do print( key .. ":" .. value) coroutine.yield() end end

Clone this wiki locally