-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MAPREDUCE-6827. Fixed bug: the second foreach-loop was not executed #177
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… reduce() method The following code is a reduce() method (of WordCount): public static class WcReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @OverRide protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // print some logs List<String> vals = new LinkedList<>(); for(IntWritable i : values) { vals.add(i.toString()); } System.out.println(String.format(">>>> reduce(%s, [%s])", key, String.join(", ", vals))); // sum of values int sum = 0; for(IntWritable i : values) { sum += i.get(); } System.out.println(String.format(">>>> reduced(%s, %s)", key, sum)); context.write(key, new IntWritable(sum)); } } After running it, we got the result that all sums were zero! After debugging, it was found that the second foreach-loop was not executed, and the root cause was the returned value of Iterable.iterator(), it returned the same instance in the two calls by foreach-loop. In general, Iterable.iterator() should return a new instance in each call, such as ArrayList.iterator(). This patch fixed the bug. Signed-off-by: Javeme <[email protected]>
NOTE: The following is a test about foreach with int[]/ArrayList, and the test results are expected(the second for-loop is also executed correctly):
|
javeme
changed the title
MAPREDUCE-6827. Failed to traverse Iterable values the second time in…
MAPREDUCE-6827. Fixed bug: the second foreach-loop was not executed
Jan 4, 2017
According to Daniel Templeton, we think it is expected. |
This was referenced Feb 27, 2022
This was referenced Mar 7, 2022
This was referenced Mar 15, 2022
This was referenced Apr 2, 2022
This was referenced Apr 11, 2022
This was referenced Apr 18, 2022
Open
This was referenced Jul 11, 2022
Closed
This was referenced Jul 18, 2022
This was referenced Jul 26, 2022
Closed
This was referenced Aug 2, 2022
This was referenced Aug 9, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Failed to traverse
Iterable values
withforeach
at the second time in reduce() method, because the second foreach-loop was not executed.JIRA MAPREDUCE-6827
The following code is a reduce() method (of WordCount):
After running it, we got the result that the value of the variable
sum
is always 0!After debugging, it was found that the second foreach-loop was not executed, and the root cause was the returned value of
Iterable.iterator()
, it returned the same instance in the two calls called by foreach-loop. In general,Iterable.iterator()
should return a new instance in each call, such asArrayList.iterator()
. This patch fixed the bug.Signed-off-by: Javeme [email protected]