Database Reference
In-Depth Information
To summarize, there is a recipe here to get the effect of sorting by value:
▪ Make the key a composite of the natural key and the natural value.
▪ The sort comparator should order by the composite key (i.e., the natural key and
natural value).
▪ The partitioner and grouping comparator for the composite key should consider
only the natural key for partitioning and grouping.
Java code
Putting this all together results in the code in
Example 9-6
.
This program uses the plain-
text input again.
Example 9-6. Application to find the maximum temperature by sorting temperatures in the
key
public class
MaxTemperatureUsingSecondarySort
extends
Configured
implements
Tool
{
static class
MaxTemperatureMapper
extends
Mapper
<
LongWritable
,
Text
,
IntPair
,
NullWritable
> {
private
NcdcRecordParser parser
=
new
NcdcRecordParser
();
@Override
protected
void
map
(
LongWritable key
,
Text value
,
Context context
)
throws
IOException
,
InterruptedException
{
parser
.
parse
(
value
);
if
(
parser
.
isValidTemperature
()) {
context
.
write
(
new
IntPair
(
parser
.
getYearInt
(),
parser
.
getAirTemperature
()),
NullWritable
.
get
());
}
}
}
static class
MaxTemperatureReducer
extends
Reducer
<
IntPair
,
NullWritable
,
IntPair
,
NullWritable
> {
@Override
protected
void
reduce
(
IntPair key
,
Iterable
<
NullWritable
>
values
,
Context context
)
throws
IOException
,
InterruptedException
{
context
.
write
(
key
,
NullWritable
.
get
());
}
}