hadoop工作流引擎解压jar文件,并运行出现类型不匹配的情况

来源:互联网 发布:360借壳昊华能源大数据 编辑:程序博客网 时间:2024/05/16 14:17

在做hadoop工作流引擎项目开发的时候,编写某些特定的Mapreduce程序,虽然该程序在hadoop平台型运行正常,但在工作流引擎中运行却出现错误,主要的原因是该工作流引擎是提取jar中的Mapper和Reducer以及主类,运行该jar时是要重新设置输入输出类型,原先设定的是输入时Text,输出是IntWritable,所以即使jar程序没有错,但放在工作流引擎中运行的时候就会报出异常……所以,需要重新调整程序提取的变量,获取配置信息。在这里可以使用java的reflect的Method或者Field来解决,具体的代码如下:

//从键值对中获取当前结点的Mapper和Reducerclss = fileScan.getCls();String m = clss.get("mapper");String r = clss.get("reducer");String ml=clss.get("maincls");logger.info(jobName + "'s Map : " + m);logger.info(jobName + "'s Reduce : " + r);Class mapper = loader.loadClass(m);Class reducer = loader.loadClass(r);Class maincls=loader.loadClass(ml);    Object main=maincls.newInstance();    Field combinerField=maincls.getDeclaredField("combinerCls");    Field outputKeyField=maincls.getDeclaredField("outputKeyCls");    Field outputValueField=maincls.getDeclaredField("outputValueCls");    boolean combiner=combinerField.getBoolean(main);    Class outputKeyClass=(Class)outputKeyField.get(main);    Class outputValueClass=(Class)outputValueField.get(main);    logger.info("combinerCls"+combiner);    logger.info("outputKeyCls"+outputKeyClass);    logger.info("outputValueCls"+outputValueClass);Job job = new Job(conf, jobName);job.setJarByClass(maincls);job.setMapperClass(mapper);if(combiner)job.setCombinerClass(reducer);job.setReducerClass(reducer);job.setOutputKeyClass(outputKeyClass);job.setOutputValueClass(outputValueClass);if (input.indexOf(";") == -1) {FileInputFormat.addInputPath(job, new Path(input));} else {st = new StringTokenizer(input, ";");while (st.hasMoreTokens()) {FileInputFormat.addInputPath(job, new Path(st.nextToken().trim()));}}output ="/"+username+"/output/" + jobName;FileOutputFormat.setOutputPath(job, new Path(output));/** * 此部分用来处理job,将所有的job,根据其流id存储到Map中,供用户选择性终止 * *///将当前job加入到job的ArrayList jobs中jobs.add(job);//如果全局FlowId_Job_List中没有该flowId,则将flowId作为key,jobs作为值,存入FlowId_Job_Listif(!GlobalVar.getGlobalVar().getFlowId_Job_List().containsKey(flowId))GlobalVar.getGlobalVar().getFlowId_Job_List().put(flowId, jobs);else{//全局FlowId_Job_List中有flowId,更新该key对应的valuejobs=GlobalVar.getGlobalVar().getFlowId_Job_List().get(flowId);    jobs.add(job);    GlobalVar.getGlobalVar().getFlowId_Job_List().remove(flowId);    GlobalVar.getGlobalVar().getFlowId_Job_List().put(flowId, jobs);}                   //提交作业job.submit();