3.3.4.1 包: com.atguigu.dataanalasis.bean
1. 类: CommonDimension
Mapper 向 Reducer 传递的数据的封装
package com.atguigu.dataanalasis.bean;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class CommonDimension implements IDimension {
private ContactDimension contactDimension;
private DateDimension dateDimension;
public CommonDimension() {
this.contactDimension = new ContactDimension();
this.dateDimension = new DateDimension();
}
public ContactDimension getContactDimension() {
return contactDimension;
}
public void setContactDimension(ContactDimension contactDimension) {
this.contactDimension = contactDimension;
}
public DateDimension getDateDimension() {
return dateDimension;
}
public void setDateDimension(DateDimension dateDimension) {
this.dateDimension = dateDimension;
}
@Override
public String toString() {
return this.contactDimension + "," + this.dateDimension;
}
@Override
public int compareTo(IDimension other) {
CommonDimension o = (CommonDimension) other;
int result = this.contactDimension.compareTo(o.contactDimension);
if(result == 0){
result = this.dateDimension.compareTo(o.dateDimension);
}
return result;
}
@Override
public void write(DataOutput out) throws IOException {
this.contactDimension.write(out);
this.dateDimension.write(out);
}
@Override
public void readFields(DataInput in) throws IOException {
this.contactDimension.readFields(in);
this.dateDimension.readFields(in);
}
}
2. 类: ContactDimension
联系人维度bean封装
package com.atguigu.dataanalasis.bean;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* 这个类将来要用在Mapper中key的位置, 所以需要实现WritableComparable
*/
public class ContactDimension implements IDimension {
private String name;
private String telephone;
public ContactDimension() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "name='" + name + ", telephone='" + telephone;
}
/**
* 只需要安装电话号码来比较就可以了
* @param other
* @return
*/
@Override
public int compareTo(IDimension other) {
ContactDimension o = (ContactDimension) other;
return this.telephone.compareTo(o.telephone);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(this.name);
out.writeUTF(this.telephone);
}
@Override
public void readFields(DataInput in) throws IOException {
this.name = in.readUTF();
this.telephone = in.readUTF();
}
}
3. 类: DateDimension
日期维度的bean的封装
package com.atguigu.dataanalasis.bean;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class DateDimension implements IDimension {
private String year;
private String month;
private String day;
public DateDimension() {
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
@Override
public int compareTo(IDimension other) {
DateDimension o = (DateDimension) other;
int result = this.year.compareTo(o.year);
if (result == 0) {
result = this.month.compareTo(o.month);
if (result == 0) {
result = this.day.compareTo(o.day);
}
}
return result;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(year);
out.writeUTF(month);
out.writeUTF(day);
}
@Override
public void readFields(DataInput in) throws IOException {
this.year = in.readUTF();
this.month = in.readUTF();
this.day = in.readUTF();
}
}
4. 类: CountDurationValue
producer 向外写的数据的值类型的封装.
包括通话次数和通话时长
package com.atguigu.dataanalasis.bean;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class CountDurationValue implements IValue {
// 通话次数的和
private int countSum;
// 通话时长的和
private int durationSum;
public CountDurationValue() {
}
public int getCountSum() {
return countSum;
}
public void setCountSum(int countSum) {
this.countSum = countSum;
}
public int getDurationSum() {
return durationSum;
}
public void setDurationSum(int durationSum) {
this.durationSum = durationSum;
}
@Override
public String toString() {
return +countSum + "," + durationSum;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(countSum);
out.writeInt(durationSum);
}
@Override
public void readFields(DataInput in) throws IOException {
this.countSum = in.readInt();
this.durationSum = in.readInt();
}
}
5. 类: IDimension
和 IValue
两个接口, 为了通知维度bean的类型, 方便后期代码的精简
package com.atguigu.dataanalasis.bean;
import org.apache.hadoop.io.Writable;
public interface IValue extends Writable {
}
package com.atguigu.dataanalasis.bean;
import org.apache.hadoop.io.WritableComparable;
public abstract interface IDimension extends WritableComparable<IDimension> {
}