Search code examples
u-sql

U-SQL: How to pass parameter to User Defined Reducer?


I would like to pass a parameter to my User Defined Reducer (apart from input & output). The documentation (https://learn.microsoft.com/en-us/u-sql/statements-and-expressions/reduce-expression) mentions I should be able to do that by:

USING new MyNameSpace.MyReducer(parameter:"value")

When I try to do that, the compiler tells me that my reducer doesn't have a constructor that takes only one argument. It only has a contructor that takes 2 arguments, yet calling it with no parameters (USING new MyNameSpace.MyReducer()) works.

I still need the IRowset input and output parameters, so creating a constructor with just one parameter will not work. If I create one with 3 parameters (input, output, param) then I don't know how to call it from U-SQL (how to pass input & output?)


Solution

  • You are passing input and output in the method Reduce, not in the constructor. So my advice is to just create a constructor that takes 1 parameter in your UDO.

    public class MyReducer: IReducer
    {
        string myParam;
        public MyReducer(string _myParam){
        myParam=_myParam;
    }
        public override IEnumerable<IRow> Reduce(IRowset input, IUpdatableRow output)
        {
    

    and then when you call your reducer you just add a value that you want to pass

    USING new MyNameSpace.MyReducer("value")
    

    Try something like this.