Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6907701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:28:05+00:00 2026-05-27T08:28:05+00:00

Is there any way to give constructor args to a Mapper in Hadoop? Possibly

  • 0

Is there any way to give constructor args to a Mapper in Hadoop? Possibly through some library that wraps the Job creation?

Here’s my scenario:

public class HadoopTest {

    // Extractor turns a line into a "feature"
    public static interface Extractor {
        public String extract(String s);
    }

    // A concrete Extractor, configurable with a constructor parameter
    public static class PrefixExtractor implements Extractor {
        private int endIndex;

        public PrefixExtractor(int endIndex) { this.endIndex = endIndex; }

        public String extract(String s) { return s.substring(0, this.endIndex); }
    }

    public static class Map extends Mapper<Object, Text, Text, Text> {
        private Extractor extractor;

        // Constructor configures the extractor
        public Map(Extractor extractor) { this.extractor = extractor; }

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String feature = extractor.extract(value.toString());
            context.write(new Text(feature), new Text(value.toString()));
        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {
        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text val : values) context.write(key, val);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "test");
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}

As should be clear, since the Mapper is only given to the Configuration as a class reference (Map.class), Hadoop has no way to pass a constructor argument and configure a specific Extractor.

There are Hadoop-wrapping frameworks out there like Scoobi, Crunch, Scrunch (and probably many more I don’t know about) that seem to have this capability, but I don’t know how they accomplish it. EDIT: After some more working with Scoobi, I discovered I was partially wrong about this. If you use an externally defined object in the “mapper”, Scoobi requires that it be serializable, and will complain at runtime if it isn’t. So maybe the right way is just to make my Extractor serializable and de-serialize it in the Mapper’s setup method…

Also, I actually work in Scala, so Scala-based solutions are definitely welcome (if not encouraged!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:28:05+00:00Added an answer on May 27, 2026 at 8:28 am

    The best solution I’ve come up with so far is to pass a serialized version of the object I want to the Mapper, and to use reflection to construct the object at runtime.

    So, the main method would say something like:

    conf.set("ExtractorConstructor", "dicta03.hw4.PrefixExtractor(3)");
    

    Then, in the Mapper we use a helper function construct (defined below) and can say:

    public void setup(Context context) {
        try {
            String constructor = context.getConfiguration().get("ExtractorConstructor");
            this.extractor = (Extractor) construct(constructor);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    

    Definition of construct that uses reflection to recursively construct an object at runtime from a String:

    public static Object construct(String s) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        if (s.matches("^[A-Za-z0-9.#]+\\(.*\\)$")) {
            Class cls = null;
            List<Object> argList = new ArrayList<Object>();
            int parenCount = 0;
            boolean quoted = false;
            boolean escaped = false;
            int argStart = -1;
            for (int i = 0; i < s.length(); i++) {
                if (escaped) {
                    escaped = false;
                } else if (s.charAt(i) == '\\') {
                    escaped = true;
                } else if (s.charAt(i) == '"') {
                    quoted = true;
                } else if (!quoted) {
                    if (s.charAt(i) == '(') {
                        if (cls == null)
                            cls = Class.forName(s.substring(0, i));
                        parenCount++;
                        argStart = i + 1;
                    } else if (s.charAt(i) == ')') {
                        if (parenCount == 1)
                            argList.add(construct(s.substring(argStart, i)));
                        parenCount--;
                    } else if (s.charAt(i) == ',') {
                        if (parenCount == 1) {
                            argList.add(construct(s.substring(argStart, i)));
                            argStart = i + 1;
                        }
                    }
                }
            }
    
            Object[] args = new Object[argList.size()];
            Class[] argTypes = new Class[argList.size()];
            for (int i = 0; i < argList.size(); i++) {
                argTypes[i] = argList.get(i).getClass();
                args[i] = argList.get(i);
            }
            Constructor constructor = cls.getConstructor(argTypes);
            return constructor.newInstance(args);
        } else if (s.matches("^\".*\"$")) {
            return s.substring(1, s.length() - 1);
        } else if (s.matches("^\\d+$")) {
            return Integer.parseInt(s);
        } else {
            throw new RuntimeException("Cannot construct " + s);
        }
    }
    

    (This may not be the most robust parser, but it could easily be extended to cover more types of objects.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to have something that looks just like a file on
Is there any way to give each record in a table its own css
Is there any way to give instructions directly to the parser and lexar from
I have a silly question Is there any way to give shadow effect to
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any way to apply an attribute to a model file in ASP.NET
Is there any way, in any language, to hook my program when a user
Is there any way to include the SVN repository revision number in the version
Is there any way to use inheritance in database (Specifically in SQL Server 2005)?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.