当reducer的输出VALUE为null时,如何使用组合器?

时间:2015-12-27 13:01:13

标签: hadoop mapreduce

当我尝试在我的MR工作中使用合并器时,我得到以下异常

  

显示java.lang.NullPointerException
  在   org.apache.hadoop.mapred.IFile $ Writer.append(IFile.java:193)
  在   org.apache.hadoop.mapred.Task $ CombineOutputCollector.collect(Task.java:1315)

     

在   org.apache.hadoop.mapred.Task $ $ NewCombinerRunner OutputConverter.write(Task.java:1632)

原因是,我在reducer类中使用 null 作为输出VALUE。 减速机代码:

public  static class reducer extends Reducer<Text,IntWritable,Text,IntWritable>{
            public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
                context.write(key, null);
            }
    }

当我删除组合器类 job.setCombinerClass(reducer.class); 作业成功时。

如何实现组合器,我需要相同的减速器输出,即只有KEY作为输出?

1 个答案:

答案 0 :(得分:1)

这是不可能实现的。问题是IFile.java中的以下代码:

public void append(K key, V value) throws IOException {
    .....

    if (value.getClass() != valueClass)
        throw new IOException("wrong value class: "+ value.getClass()
                          +" is not "+ valueClass);

    .....

append()函数中,有一个检查:

if (value.getClass() != valueClass)

由于您将null作为值传递,因此NullPointerException会在getClass()值上尝试null时抛出value.getClass()

NullWritable

因此,即使您使用null(又是一个类)并通过NullPointerException,您仍然可以获得null

不应传递 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="../js/jquery.js"></script> <!--<link rel="stylesheet" type="text/css" href="node/css/bootstrap.min.css"> <script type="text/javascript" src="node/js/bootstrap.min.js"></script>--> </head> <body> <div class="row-fluid"> <div> <label>Hi Welcome to Ajax Request</label> </div> <div> <input type="button" value="click" onclick="alert('hi')" /> <button onclick="callAjax()">Call Ajax</button> </div> </div> <script type="text/javascript"> function callAjax() { console.log('ajax Button clicked'); var url = 'http://localhost:3334/getLeaderboard'; /* var xhr = createCORSRequest('GET', url); if (!xhr) { throw new Error('CORS not supported'); } else { console.log(xhr); } xhr.onload = function () { var text = xhr.responseText; var title = getTitle(text); alert('Response from CORS request to ' + url + ': ' + title); }; xhr.onerror = function () { alert('Woops, there was an error making the request.'); }; xhr.send();*/ var request = new XMLHttpRequest(); var params = "t=test&userid=123&accessToken=123&origin=http://test.local"; request.open('POST', url, true); request.withCredentials = true; request.onreadystatechange = function () { if (request.readyState == 4) { alert("It worked!"); console.log(request); } }; request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.setRequestHeader("Content-length", params.length); /*request.setRequestHeader("Connection", "close");*/ request.send(params); } function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { xhr.withCredentials = true; // Check if the XMLHttpRequest object has a "withCredentials" property. // "withCredentials" only exists on XMLHTTPRequest2 objects. xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { // Otherwise, check if XDomainRequest. // XDomainRequest only exists in IE, and is IE's way of making CORS requests. xhr = new XDomainRequest(); xhr.open(method, url); } else { // Otherwise, CORS is not supported by the browser. xhr = null; } return xhr; } </script> </body> </html> ,而应通过传递0(零)进行管理。