计数器
计数器是手机作业统计信息的有效手段之一,用于质量控制或应用级统计,计数器还可以辅助诊断系统故障
内置计数器
Hadoop为每个作业维护若干内置计数器,如处理的字节数,和记录数
计数器分组
MapReduce任务计数器 | TaskCount |
文件系统计数器 | FileSystemCounter |
FileInputFormat | FileInputFormatCounter |
FileOutPutFormat | FileOutPutFormatCounter |
作业计数器 | JobCounter |
任务计数器
任务计数器由其关联任务维护,并定期发送给application master
作业计数器
作业计数器由application master维护,无需再网络间传输数据,这些计数器都是做业级别的统计,值不会随着任务运行而改变。如:启动的map数
用户定义的Java计数器
计数器的值可在 mapper 或 reducer 中增加,计数器由一个Java枚举类型来定义,以便对有关计数器分组,
枚举类型的名称即为组的名称,枚举类型的字段就是计数器的名称
public class MaxTemperatureWithCounters extends Configured implements Tool {
enum Temperature {
MISSING,
MALFORMED
}
static class MaxTemperatureMapperWithCounters
extends Mapper<LongWritable, Text, Text, IntWritable> {
private NcdcRecordParser parser = new NcdcRecordParser();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
parser.parse(value);
if (parser.isValidTemperature()) {
int airTemperature = parser.getAirTemperature();
context.write(new Text(parser.getYear()),
new IntWritable(airTemperature));
} else if (parser.isMalformedTemperature()) {
System.err.println("Ignoring possibly corrupt input: " + value);
context.getCounter(Temperature.MALFORMED).increment(1);
} else if (parser.isMissingTemperature()) {
context.getCounter(Temperature.MISSING).increment(1);
}
// dynamic counter
context.getCounter("TemperatureQuality", parser.getQuality()).increment(1);
}
}
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(MaxTemperatureMapperWithCounters.class);
job.setCombinerClass(MaxTemperatureReducer.class);
job.setReducerClass(MaxTemperatureReducer.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MaxTemperatureWithCounters(), args);
System.exit(exitCode);
}
}
动态计数器
Java枚举类型字段在编译阶段必须指定,因而无法使用枚举类型动态新建计数器 context.getCounter("TemperatureQuality", parser.getQuality()).increment(1);
获取计数器
Java API 还支持在作业运行期间就能够获取计数器的值
public class MissingTemperatureFields extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
/*if (args.length != 1) {
JobBuilder.printUsage(this, "<job ID>");
return -1;
}*/
String jobID = "attempt_local1491922281_0001_m_000001_0";
Cluster cluster = new Cluster(getConf());
Job job = cluster.getJob(JobID.forName(jobID));
if (job == null) {
System.err.printf("No job with ID %s found.\n", jobID);
return -1;
}
if (!job.isComplete()) {
System.err.printf("Job %s is not complete.\n", jobID);
return -1;
}
Counters counters = job.getCounters();
long missing = counters.findCounter(
MaxTemperatureWithCounters.Temperature.MISSING).getValue();
long total = counters.findCounter(TaskCounter.MAP_INPUT_RECORDS).getValue();
System.out.printf("Records with missing temperature fields: %.2f%%\n",
100.0 * missing / total);
return 0;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MissingTemperatureFields(), args);
System.exit(exitCode);
}
}
排序
针对气温排序
有一个常用方法,消除所有负数,在数字前面增加0,使所有数字长度相等
反之,使用顺序文件存储数据集,其IntWritable键代表气温(并且正确排序),Text代表数据行,各个map创建并输出一个块压缩的顺序文件
public class SortDataPreprocessor extends Configured implements Tool {
static class CleanerMapper
extends Mapper<LongWritable, Text, IntWritable, Text> {
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 IntWritable(parser.getAirTemperature()), value);
}
}
}
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setMapperClass(CleanerMapper.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(0);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
/*SequenceFileOutputFormat.setCompressOutput(job, true);
SequenceFileOutputFormat.setOutputCompressorClass(job, DefaultCodec.class);*/
SequenceFileOutputFormat.setOutputCompressionType(job,
CompressionType.NONE);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new SortDataPreprocessor(), args);
System.exit(exitCode);
}
}
部分排序
默认情况下,MapReduce根据输入记录的键对数据进行排序,下面是一个变种,利用IntWritable键对顺序文件排序
public class SortByTemperatureUsingHashPartitioner extends Configured
implements Tool {
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
SequenceFileOutputFormat.setCompressOutput(job, true);
SequenceFileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
SequenceFileOutputFormat.setOutputCompressionType(job,
CompressionType.BLOCK);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new SortByTemperatureUsingHashPartitioner(),
args);
System.exit(exitCode);
}
键的排列顺序有RawComparator控制的
假设采用30个reduce运行,则会产生30个已排序的输出文件
按键进行排序
public class LookupRecordsByTemperature extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
/* if (args.length != 2) {
JobBuilder.printUsage(this, "<path> <key>");
return -1;
}*/
Path path = new Path("hdfs://hadoop:9000/input/data");
IntWritable key = new IntWritable(Integer.parseInt("2012"));
Reader[] readers = MapFileOutputFormat.getReaders(path, getConf());
Partitioner<IntWritable, Text> partitioner =
new HashPartitioner<IntWritable, Text>();
Text val = new Text();
Reader reader = readers[partitioner.getPartition(key, val, readers.length)];
Writable entry = reader.get(key, val);
if (entry == null) {
System.err.println("Key not found: " + key);
return -1;
}
NcdcRecordParser parser = new NcdcRecordParser();
IntWritable nextKey = new IntWritable();
do {
parser.parse(val.toString());
System.out.printf("%s\t%s\n", parser.getStationId(), parser.getYear());
} while(reader.next(nextKey, val) && key.equals(nextKey));
return 0;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new LookupRecordsByTemperature(), args);
System.exit(exitCode);
}
}
全排序
最简单方法是使用一个分区,但是在处理大型问件事效率极低,因为每一台机器必须处理所有输入文件,丧失了MapReduce提供的并行架构优势
替代方案:创建一系列拍还需的文件,串联这些文件,生成一个全局排序文件,使用一个Partitioner描述输出的。
全局排序:如:创建不同分区,第一个分区记录小于-10C,第二个-10和0之间,关键点在于如何划分分区,
可以
写一个MapReduce作业来计算落入各个气温桶的记录数,但是要操作整个数据集并不实用,可以使用Hadoop
内置的采样器
// vv SortByTemperatureUsingTotalOrderPartitioner
public class SortByTemperatureUsingTotalOrderPartitioner extends Configured
implements Tool {
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
SequenceFileOutputFormat.setCompressOutput(job, true);
SequenceFileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
SequenceFileOutputFormat.setOutputCompressionType(job,
CompressionType.BLOCK);
job.setPartitionerClass(TotalOrderPartitioner.class);
/**
* 0.1 采样率
* 10000 最大样本数
* 10 最大分区数
* 只要任意一个条件满足,停止采样
*/
InputSampler.Sampler<IntWritable, Text> sampler =
new InputSampler.RandomSampler<IntWritable, Text>(0.1, 10000, 10);
InputSampler.writePartitionFile(job, sampler);
// Add to DistributedCache
Configuration conf = job.getConfiguration();
String partitionFile = TotalOrderPartitioner.getPartitionFile(conf);
URI partitionUri = new URI(partitionFile);
job.addCacheFile(partitionUri);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(
new SortByTemperatureUsingTotalOrderPartitioner(), args);
System.exit(exitCode);
}
}
InputSampler.SplitSampler<K, V> 只采样一个分片中的n条记录,不适合已经排序好的数据
InputSampler.IntervalSampler<K, V> 以一定的间隔定期从分片中选择键,对于已排序好的数据来说是一个
更好的选择
辅助排序
MapReduce在记录到达Reducer之前按键对记录排序,但键所对应的值没有排序
场景:计算每年最高气温,如果按照气温降序排序,则无需遍历整个数据集,获取各年份收条即可
仅仅使用组合键,会导致同一年的记录有不同的键,通过设置一个按照年份进行分区的patitioner,可确保同一年
记录发送到同一个reducer中,这还不够,patitioner只保证每一个reducer接受一个年份的所有记录,而在一个分区
之内,reducer扔是通过键进行分组的分区。
最终解决方案是进行分组设置。
- 定义包括自然键和自然值得组合键
- 根据组合键对记录进行排序,即同事用自然键和自然值进行排序
- 针对组合键进行分区和分组时均只考虑自然键
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());/*]*/
}
}
public static class FirstPartitioner
extends Partitioner<IntPair, NullWritable> {
@Override
public int getPartition(IntPair key, NullWritable value, int numPartitions) {
// multiply by 127 to perform some mixing
return Math.abs(key.getFirst() * 127) % numPartitions;
}
}
public static class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(IntPair.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
IntPair ip1 = (IntPair) w1;
IntPair ip2 = (IntPair) w2;
int cmp = IntPair.compare(ip1.getFirst(), ip2.getFirst());
if (cmp != 0) {
return cmp;
}
return -IntPair.compare(ip1.getSecond(), ip2.getSecond()); //reverse
}
}
public static class GroupComparator extends WritableComparator {
protected GroupComparator() {
super(IntPair.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
IntPair ip1 = (IntPair) w1;
IntPair ip2 = (IntPair) w2;
return IntPair.compare(ip1.getFirst(), ip2.getFirst());
}
}
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setMapperClass(MaxTemperatureMapper.class);
/*[*/job.setPartitionerClass(FirstPartitioner.class);/*]*/
/*[*/job.setSortComparatorClass(KeyComparator.class);/*]*/
/*[*/job.setGroupingComparatorClass(GroupComparator.class);/*]*/
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(IntPair.class);
job.setOutputValueClass(NullWritable.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new MaxTemperatureUsingSecondarySort(), args);
System.exit(exitCode);
}
}
连接
连接操作的具体实现技术取决于数据集的规模及分区方式map端连接
两个大规模数据之间的map端连接,会在数据到达map函数之前就执行连接操作。
为达到目的,map的输入数据必须先分区,并且以特定方式排序,各个输入数据集被划分成相同数量的分区,并且均按相同键(连接键)排序,同一键的所有记录均会放在同一分区中。
reduce端连接
reduce端连接并不要求输入数据集符合特定结构,因而更为常用,,但是两个数据集需要经过shuffle过程,所有效率更低,基本思路是mapper为各个记录标记源,并且使用连接键作为map输出键,使键相同的记录放在同一个
reducer中,以下记录可以帮助实现reduce端连接
1.多输入
数据集的输入源往往有多个,可以使用MultipleInputs类
2.辅助排序
为了更好的执行连接操作,一个源的数据排列在另一个源数据前是非场重要的,气象站的值必须是最先看到的,这样能够将气象站名称填到天气记录之中再马上输出
public class JoinStationMapper
extends Mapper<LongWritable, Text, TextPair, Text> {
private NcdcStationMetadataParser parser = new NcdcStationMetadataParser();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
if (parser.parse(value)) {
context.write(new TextPair(parser.getStationId(), "0"),
new Text(parser.getStationName()));
}
}
}
public class JoinRecordMapper
extends Mapper<LongWritable, Text, TextPair, Text> {
private NcdcRecordParser parser = new NcdcRecordParser();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
parser.parse(value);
context.write(new TextPair(parser.getStationId(), "1"), value);
}
}
reduce知道自己会先接收气象站数据,因此从中抽取值,并将其作为后续每条输出记录的一部分写到输出文件
public class JoinReducer extends Reducer<TextPair, Text, Text, Text> {
@Override
protected void reduce(TextPair key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
Iterator<Text> iter = values.iterator();
Text stationName = new Text(iter.next());
while (iter.hasNext()) {
Text record = iter.next();
Text outValue = new Text(stationName.toString() + "\t" + record.toString());
context.write(key.getFirst(), outValue);
}
}
}
关键点在于根据组合键的第一个字段(气象站id)进行分区和分组
public class JoinRecordWithStationName extends Configured implements Tool {
public static class KeyPartitioner extends Partitioner<TextPair, Text> {
@Override
public int getPartition(/*[*/TextPair key/*]*/, Text value, int numPartitions) {
return (/*[*/key.getFirst().hashCode()/*]*/ & Integer.MAX_VALUE) % numPartitions;
}
}
@Override
public int run(String[] args) throws Exception {
/*if (args.length != 3) {
JobBuilder.printUsage(this, "<ncdc input> <station input> <output>");
return -1;
}*/
Job job = new Job(getConf(), "Join weather records with station names");
job.setJarByClass(getClass());
Path ncdcInputPath = new Path("hdfs://centos1:9000/input/join/sample.txt");
Path stationInputPath = new Path("hdfs://centos1:9000/input/join/stations-fixed-width.txt");
Path outputPath = new Path("hdfs://centos1:9000/output");
MultipleInputs.addInputPath(job, ncdcInputPath,
TextInputFormat.class, JoinRecordMapper.class);
MultipleInputs.addInputPath(job, stationInputPath,
TextInputFormat.class, JoinStationMapper.class);
FileOutputFormat.setOutputPath(job, outputPath);
/*[*/job.setPartitionerClass(KeyPartitioner.class);
job.setGroupingComparatorClass(TextPair.FirstComparator.class);/*]*/
job.setMapOutputKeyClass(TextPair.class);
job.setReducerClass(JoinReducer.class);
job.setOutputKeyClass(Text.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new JoinRecordWithStationName(), args);
System.exit(exitCode);
}
}
边数据分布
利用JobConf配置作业
分布式缓存
在任务运行过程中及时的将文件和存档复制到任务节点以供节点使用
-files 选项指定分发的文件,文件内包含以逗号隔开的URI列表
-archives选项想自己的任务中复制存档文件(JAR,ZIP,tar等)
-libjars 选项会把JAR文件添加到mapper和reducer任务的类路径中
public class MaxTemperatureByStationNameUsingDistributedCacheFile
extends Configured implements Tool {
static class StationTemperatureMapper
extends Mapper<LongWritable, Text, Text, IntWritable> {
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 Text(parser.getStationId()),
new IntWritable(parser.getAirTemperature()));
}
}
}
static class MaxTemperatureReducerWithStationLookup
extends Reducer<Text, IntWritable, Text, IntWritable> {
/*[*/private NcdcStationMetadata metadata;/*]*/
/*[*/@Override
protected void setup(Context context)
throws IOException, InterruptedException {
metadata = new NcdcStationMetadata();
metadata.initialize(new File("stations-fixed-width.txt"));
}/*]*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
/*[*/String stationName = metadata.getStationName(key.toString());/*]*/
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(new Text(/*[*/stationName/*]*/), new IntWritable(maxValue));
}
}
@Override
public int run(String[] args) throws Exception {
Job job = JobBuilder.parseInputAndOutput(this, getConf(), args);
if (job == null) {
return -1;
}
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(StationTemperatureMapper.class);
job.setCombinerClass(MaxTemperatureReducer.class);
job.setReducerClass(MaxTemperatureReducerWithStationLookup.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(
new MaxTemperatureByStationNameUsingDistributedCacheFile(), args);
System.exit(exitCode);
}
}
通过reducer的setup()来获取缓存文件
分布式缓存API
可以通过GenericOptionsParser间接使用分部署缓存,大多数情况使用job中的相关方法:
job.addCacheFile(uri);
job.addCacheArchive(uri);
job.setCacheFiles(files);
job.setCacheArchives(archives);
job.addFileToClassPath(file);
job.addArchiveToClassPath(archive);
job.createSymlink();