java—意外地多次执行Map程序,以运行一次

im9ewurl  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(445)

我试图写一个非常简单的工作,只有一个Map器,没有减速机写一些数据到hbase。在mapper中,我尝试简单地打开与hbase的连接,将几行数据写入一个表,然后关闭连接。在job driver中,我使用jobconf.setnummaptasks(1);和jobconf.setnumreducetasks(0);指定只执行1个Map器而不执行任何缩减器。我还在jobconf中将reducer类设置为identityreducer。我观察到的奇怪行为是,作业成功地将数据写入hbase表,但在日志中看到之后,它不断尝试打开与hbase的连接,然后关闭连接,连接持续20-30分钟,并且在作业被声明为100%成功完成之后。最后,当我检查由放入outputcollector.collect(…)中的伪数据创建的\u success文件时,我看到了数百行伪数据,而实际上只有1行。以下是作业驱动程序的代码

public int run(String[] arg0) throws Exception {
        Configuration config = HBaseConfiguration.create(getConf());
        ensureRequiredParametersExist(config);
        ensureOptionalParametersExist(config);

        JobConf jobConf = new JobConf(config, getClass());
        jobConf.setJobName(config.get(ETLJobConstants.ETL_JOB_NAME));
        //set map specific configuration
        jobConf.setNumMapTasks(1);
        jobConf.setMaxMapAttempts(1);
        jobConf.setInputFormat(TextInputFormat.class);
        jobConf.setMapperClass(SingletonMapper.class);
        jobConf.setMapOutputKeyClass(LongWritable.class);
        jobConf.setMapOutputValueClass(Text.class);

        //set reducer specific configuration
        jobConf.setReducerClass(IdentityReducer.class);
        jobConf.setOutputKeyClass(LongWritable.class);
        jobConf.setOutputValueClass(Text.class);
        jobConf.setOutputFormat(TextOutputFormat.class);
        jobConf.setNumReduceTasks(0);

        //set job specific configuration details like input file name etc
        FileInputFormat.setInputPaths(jobConf, jobConf.get(ETLJobConstants.ETL_JOB_FILE_INPUT_PATH));
        System.out.println("setting output path to : " + jobConf.get(ETLJobConstants.ETL_JOB_FILE_OUTPUT_PATH));
        FileOutputFormat.setOutputPath(jobConf,
                new Path(jobConf.get(ETLJobConstants.ETL_JOB_FILE_OUTPUT_PATH)));
        JobClient.runJob(jobConf);
        return 0;
    }

driver类扩展了configured和implements工具(我使用了权威指南中的示例),下面是mapper类中的代码。
下面是mapper的map方法中的代码,我只需打开与hbase的连接,进行一些初步检查以确保表存在,然后写入行并关闭表。

public void map(LongWritable arg0, Text arg1,
        OutputCollector<LongWritable, Text> arg2, Reporter arg3)
        throws IOException {

    HTable aTable = null;
    HBaseAdmin admin = null;

    try {

        arg3.setStatus("started");

        /*
         * set-up hbase config
         */
        admin = new HBaseAdmin(conf);

        /*
         * open connection to table
         */
        String tableName = conf.get(ETLJobConstants.ETL_JOB_TABLE_NAME);

        HTableDescriptor htd = new HTableDescriptor(toBytes(tableName));
        String colFamilyName = conf.get(ETLJobConstants.ETL_JOB_TABLE_COLUMN_FAMILY_NAME);

        byte[] tablename = htd.getName();
        /* call function to ensure table with 'tablename' exists */

        /*
         * loop and put the file data into the table
         */
        aTable = new HTable(conf, tableName);

        DataRow row = /* logic to generate data */
        while (row != null) {
            byte[] rowKey = toBytes(row.getRowKey());
            Put put = new Put(rowKey);
            for (DataNode node : row.getRowData()) {
                put.add(toBytes(colFamilyName), toBytes(node.getNodeName()),
                        toBytes(node.getNodeValue()));
            }
            aTable.put(put);
            arg3.setStatus("xoxoxoxoxoxoxoxoxoxoxoxo added another data row to hbase");
            row = fileParser.getNextRow();
        }
        aTable.flushCommits();
        arg3.setStatus("xoxoxoxoxoxoxoxoxoxoxoxo Finished adding data to hbase");

    } finally {
        if (aTable != null) {
            aTable.close();
        }

        if (admin != null) {
            admin.close();
        }
    }

    arg2.collect(new LongWritable(10), new Text("something"));
    arg3.setStatus("xoxoxoxoxoxoxoxoxoxoxoxoadded some dummy data to the collector");
}

正如您在结尾看到的,我在结尾(10,'something')向collection写入了一些伪数据,并且在作业终止后,我在\u success文件中看到了数百行这些数据。我不明白为什么Map程序代码被多次重新启动,而不是只运行一次。任何帮助都将不胜感激。

cwxwcias

cwxwcias1#

使用 JobConf.setNumMapTasks(1) 只是对hadoop说,如果可能的话,您希望使用1个Map器,而不是 setNumReduceTasks ,它实际上定义了您指定的数字。
这就是为什么会有更多的Map绘制者在运行,而你会观察到所有这些数字。
欲知详情,请阅读此帖。

相关问题