自定義計數器java
計數器用來監控,hadoop中job的運行進度和狀態。apache
如源文件內容爲:網絡
a bide
c d e foop
g h ithis
如今須要找出字段數大於3和小於3的記錄條數,能夠使用計數器來實現,代碼以下:spa
public void map(LongWritable key, Text value,orm
OutputCollector<Text, Text> output, Reporter reporter)接口
throws IOException {hadoop
String[] split = value.toString().split("\t");
if(split.length>3){
org.apache.hadoop.mapred.Counters.Counter counter = reporter.getCounter("MyCounter", "isLong");
counter.increment(1);
}else if(split.length<3){
org.apache.hadoop.mapred.Counters.Counter counter = reporter.getCounter("MyCounter","isShort");
counter.increment(1);
}
2. hadoop中的自定義數據類型
hadoop中默認的數據類型有:
BooleanWritable:標準布爾型數值
ByteWritable:單字節數值
DoubleWritable:雙字節數值
FloatWritable:浮點數
IntWritable:整型數
LongWritable:長整型數
Text:使用UTF8格式存儲的文本
NullWritable:當<key, value>中的key或value爲空時使用
自定義數據類型的實現:
1.實現Writable接口,並重寫內部write()和readFields()方法,從而完成序列化以後的網絡傳輸和文件輸入或輸出。
2.若是該數據類型被做爲mapreduce中的key,則該key須要爲可比較的,須要實現WriableComparable接口,並重寫內部write()和readFields()、compare()方法。
代碼以下:
代碼一:
public class Person implements Writable{
long id;
String name;
long age;
@Override
public void readFields(DataInput in) throws IOException {
this.id = in.readLong();
this.name = in.readUTF();
this.age = in.readLong();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(id);
out.writeUTF(name);
out.writeLong(age);
}
@Override
public String toString() {
return "id:"+id+" name:"+name+" age:"+age;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public long getAge() {
return age;
}
}
代碼二:基於key的比較
package cn.com.bonc.hadoop;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
public class PersonSortByAge implements WritableComparable<PersonSortByAge>{
long id;
String name;
long age;
@Override
public void readFields(DataInput in) throws IOException {
in.readLong();
in.readUTF();
in.readLong();
}
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(id);
out.writeUTF(name);
out.writeLong(age);
}
@Override
public int compareTo(PersonSortByAge o) {
return (int) (this.id - o.id);
}
@Override
public String toString() {
return "id:"+id+" name:"+name+" age:"+age;
}
}