MySQL增量数据的投递核心目的是构建检索服务中的增量索引
增量数据的投递其实就是去实现 ISender 这个接口,ISender 接口去接收一个参数,是 MySqlRowData 的类型。构建索引的过程是使用我们之前完成的 AdLevelDataHandler,里面分为好几个层级,对于每一个层级都是将表转化为 table 对象,最终我们在增量数据投递的过程中,去实现增量索引的加载。
1.新建包,是为了构建增量索引而实现的index
2.编写类IndexSender并填充之前的IncrementListener
第二、第三、第四层级增量数据的投递
package com.imooc.ad.sender.index;
import com.alibaba.fastjson.JSON;
import com.imooc.ad.dump.table.AdCreativeTable;
import com.imooc.ad.dump.table.AdCreativeUnitTable;
import com.imooc.ad.dump.table.AdPlanTable;
import com.imooc.ad.dump.table.AdUnitDistrictTable;
import com.imooc.ad.dump.table.AdUnitItTable;
import com.imooc.ad.dump.table.AdUnitKeywordTable;
import com.imooc.ad.dump.table.AdUnitTable;
import com.imooc.ad.handler.AdLevelDataHandler;
import com.imooc.ad.index.DataLevel;
import com.imooc.ad.mysql.constant.Constant;
import com.imooc.ad.mysql.dto.MySqlRowData;
import com.imooc.ad.sender.ISender;
import com.imooc.ad.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Qinyi.
*/
@Slf4j
@Component("indexSender")
public class IndexSender implements ISender {
@Override
public void sender(MySqlRowData rowData) {
String level = rowData.getLevel();//获取数据层级
if (DataLevel.LEVEL2.getLevel().equals(level)) {
Level2RowData(rowData);
} else if (DataLevel.LEVEL3.getLevel().equals(level)) {
Level3RowData(rowData);
} else if (DataLevel.LEVEL4.getLevel().equals(level)) {
Level4RowData(rowData);
} else {
log.error("MysqlRowData ERROR: {}", JSON.toJSONString(rowData));
}
}
private void Level2RowData(MySqlRowData rowData) {
if (rowData.getTableName().equals(
Constant.AD_PLAN_TABLE_INFO.TABLE_NAME)) {
List<AdPlanTable> planTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdPlanTable planTable = new AdPlanTable();
//对每个列进行switch
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_PLAN_TABLE_INFO.COLUMN_ID:
planTable.setId(Long.valueOf(v));
break;
case Constant.AD_PLAN_TABLE_INFO.COLUMN_USER_ID:
planTable.setUserId(Long.valueOf(v));
break;
case Constant.AD_PLAN_TABLE_INFO.COLUMN_PLAN_STATUS:
planTable.setPlanStatus(Integer.valueOf(v));
break;
case Constant.AD_PLAN_TABLE_INFO.COLUMN_START_DATE:
planTable.setStartDate(
CommonUtils.parseStringDate(v)
);
break;
case Constant.AD_PLAN_TABLE_INFO.COLUMN_END_DATE:
planTable.setEndDate(
CommonUtils.parseStringDate(v)
);
break;
}
});
planTables.add(planTable);
}
planTables.forEach(p ->
AdLevelDataHandler.handleLevel2(p, rowData.getOpType()));
} else if (rowData.getTableName().equals(
Constant.AD_CREATIVE_TABLE_INFO.TABLE_NAME
)) {
List<AdCreativeTable> creativeTables = new ArrayList<>();
for (Map<String, String> fieldValeMap :
rowData.getFiledValueMap()) {
AdCreativeTable creativeTable = new AdCreativeTable();
fieldValeMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_ID:
creativeTable.setAdId(Long.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_TYPE:
creativeTable.setType(Integer.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_MATERIAL_TYPE:
creativeTable.setMaterialType(Integer.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_HEIGHT:
creativeTable.setHeight(Integer.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_WIDTH:
creativeTable.setWidth(Integer.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_AUDIT_STATUS:
creativeTable.setAuditStatus(Integer.valueOf(v));
break;
case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_URL:
creativeTable.setAdUrl(v);
break;
}
});
creativeTables.add(creativeTable);
}
creativeTables.forEach(c ->
AdLevelDataHandler.handleLevel2(c, rowData.getOpType()));
}
}
private void Level3RowData(MySqlRowData rowData) {
if (rowData.getTableName().equals(
Constant.AD_UNIT_TABLE_INFO.TABLE_NAME)) {
List<AdUnitTable> unitTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdUnitTable unitTable = new AdUnitTable();
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_UNIT_TABLE_INFO.COLUMN_ID:
unitTable.setUnitId(Long.valueOf(v));
break;
case Constant.AD_UNIT_TABLE_INFO.COLUMN_UNIT_STATUS:
unitTable.setUnitStatus(Integer.valueOf(v));
break;
case Constant.AD_UNIT_TABLE_INFO.COLUMN_POSITION_TYPE:
unitTable.setPositionType(Integer.valueOf(v));
break;
case Constant.AD_UNIT_TABLE_INFO.COLUMN_PLAN_ID:
unitTable.setPlanId(Long.valueOf(v));
break;
}
});
unitTables.add(unitTable);
}
unitTables.forEach(u ->
AdLevelDataHandler.handleLevel3(u, rowData.getOpType()));
} else if (rowData.getTableName().equals(
Constant.AD_CREATIVE_UNIT_TABLE_INFO.TABLE_NAME
)) {
List<AdCreativeUnitTable> creativeUnitTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdCreativeUnitTable creativeUnitTable = new AdCreativeUnitTable();
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_CREATIVE_ID:
creativeUnitTable.setAdId(Long.valueOf(v));
break;
case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_UNIT_ID:
creativeUnitTable.setUnitId(Long.valueOf(v));
break;
}
});
creativeUnitTables.add(creativeUnitTable);
}
creativeUnitTables.forEach(
u -> AdLevelDataHandler.handleLevel3(u, rowData.getOpType())
);
}
}
private void Level4RowData(MySqlRowData rowData) {
switch (rowData.getTableName()) {
case Constant.AD_UNIT_DISTRICT_TABLE_INFO.TABLE_NAME:
List<AdUnitDistrictTable> districtTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdUnitDistrictTable districtTable = new AdUnitDistrictTable();
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_UNIT_ID:
districtTable.setUnitId(Long.valueOf(v));
break;
case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_PROVINCE:
districtTable.setProvince(v);
break;
case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_CITY:
districtTable.setCity(v);
break;
}
});
districtTables.add(districtTable);
}
districtTables.forEach(
d -> AdLevelDataHandler.handleLevel4(d, rowData.getOpType())
);
break;
case Constant.AD_UNIT_IT_TABLE_INFO.TABLE_NAME:
List<AdUnitItTable> itTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdUnitItTable itTable = new AdUnitItTable();
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_UNIT_ID:
itTable.setUnitId(Long.valueOf(v));
break;
case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_IT_TAG:
itTable.setItTag(v);
break;
}
});
itTables.add(itTable);
}
itTables.forEach(
i -> AdLevelDataHandler.handleLevel4(i, rowData.getOpType())
);
break;
case Constant.AD_UNIT_KEYWORD_TABLE_INFO.TABLE_NAME:
List<AdUnitKeywordTable> keywordTables = new ArrayList<>();
for (Map<String, String> fieldValueMap :
rowData.getFiledValueMap()) {
AdUnitKeywordTable keywordTable = new AdUnitKeywordTable();
fieldValueMap.forEach((k, v) -> {
switch (k) {
case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_UNIT_ID:
keywordTable.setUnitId(Long.valueOf(v));
break;
case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_KEYWORD:
keywordTable.setKeyword(v);
break;
}
});
keywordTables.add(keywordTable);
}
keywordTables.forEach(
k -> AdLevelDataHandler.handleLevel4(k, rowData.getOpType())
);
break;
}
}
}