您好,登錄后才能下訂單哦!
媒體方(手機APP打開的展示廣告,走在路上看到的大屏幕廣告等等)
從上圖我們可以看出,在媒體方向我們的廣告檢索系統發起請求的時候,請求中會有很多的請求參數信息,他們分為了三個部分,我們來編碼封裝這幾個參數對象信息以及我們請求本身的信息。Let's code.
/**
* ISearch for 請求接口,
* 根據廣告請求對象,獲取廣告響應信息
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@FunctionalInterface
public interface ISearch {
/**
* 根據請求返回廣告結果
*/
SearchResponse fetchAds(SearchRequest request);
}
mediaId
,RequestInfo
,FeatureInfo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchRequest {
//媒體方請求標示
private String mediaId;
//請求基本信息
private RequestInfo requestInfo;
//匹配信息
private FeatureInfo featureInfo;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class RequestInfo {
private String requestId;
private List<AdSlot> adSlots;
private App app;
private Geo geo;
private Device device;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class FeatureInfo {
private KeywordFeature keywordFeature;
private DistrictFeature districtFeature;
private HobbyFeatrue hobbyFeatrue;
private FeatureRelation relation = FeatureRelation.AND;
}
}
其他的對象大家可以去github傳送門 & gitee傳送門 下載源碼。
/**
* SearchResponse for 檢索API響應對象
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchResponse {
//一個廣告位,可以展示多個廣告
//Map key為廣告位 AdSlot#adSlotCode
public Map<String, List<Creative>> adSlotRelationAds = new HashMap<>();
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Creative {
private Long adId;
private String adUrl;
private Integer width;
private Integer height;
private Integer type;
private Integer materialType;
//展示監控url
private List<String> showMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
//點擊監控url
private List<String> clickMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
}
/**
* 我們的檢索服務針對的是內存中的索引檢索,那么我們就需要一個轉換方法
*/
public static Creative convert(CreativeIndexObject object) {
return Creative.builder()
.adId(object.getAdId())
.adUrl(object.getAdUrl())
.width(object.getWidth())
.height(object.getHeight())
.type(object.getType())
.materialType(object.getMaterialType())
.build();
}
}
流量類型本身屬于推廣單元下的類目,有很多種類貼片廣告
,開屏廣告
等等,這些類型需要同步到媒體方,媒體方會根據不同的流量類型發起不同的廣告請求,我們需要先定義一個流量類型的信息類。
public class AdUnitConstants {
public static class PositionType{
//App啟動時展示的、展示時間短暫的全屏化廣告形式。
private static final int KAIPING = 1;
//電影開始之前的廣告
private static final int TIEPIAN = 2;
//電影播放中途廣告
private static final int TIEPIAN_MIDDLE = 4;
//暫停視頻時候播放的廣告
private static final int TIEPIAN_PAUSE = 8;
//視頻播放完
private static final int TIEPIAN_POST = 16;
}
}
從上述類型的數字,我們可以看出是2的倍數,這是為了使用位運算提升性能。
在com.sxzhongf.ad.index.adunit.AdUnitIndexObject
中,我們添加類型校驗方法:
public static boolean isAdSlotType(int adSlotType, int positionType) {
switch (adSlotType) {
case AdUnitConstants.PositionType.KAIPING:
return isKaiPing(positionType);
case AdUnitConstants.PositionType.TIEPIAN:
return isTiePian(positionType);
case AdUnitConstants.PositionType.TIEPIAN_MIDDLE:
return isTiePianMiddle(positionType);
case AdUnitConstants.PositionType.TIEPIAN_PAUSE:
return isTiePianPause(positionType);
case AdUnitConstants.PositionType.TIEPIAN_POST:
return isTiePianPost(positionType);
default:
return false;
}
}
/**
* 與運算,低位取等,高位補零。
* 如果 > 0,則為開屏
*/
private static boolean isKaiPing(int positionType) {
return (positionType & AdUnitConstants.PositionType.KAIPING) > 0;
}
private static boolean isTiePianMiddle(int positionType) {
return (positionType & AdUnitConstants.PositionType.TIEPIAN_MIDDLE) > 0;
}
private static boolean isTiePianPause(int positionType) {
return (positionType & AdUnitConstants.PositionType.TIEPIAN_PAUSE) > 0;
}
private static boolean isTiePianPost(int positionType) {
return (positionType & AdUnitConstants.PositionType.TIEPIAN_POST) > 0;
}
private static boolean isTiePian(int positionType) {
return (positionType & AdUnitConstants.PositionType.TIEPIAN) > 0;
}
無所如何,我們都是需要根據positionType進行數據查詢過濾,我們在之前的com.sxzhongf.ad.index.adunit.AdUnitIndexAwareImpl
中添加2個方法來實現過濾:
/**
* 過濾當前是否存在滿足positionType的UnitIds
*/
public Set<Long> match(Integer positionType) {
Set<Long> adUnitIds = new HashSet<>();
objectMap.forEach((k, v) -> {
if (AdUnitIndexObject.isAdSlotType(positionType, v.getPositionType())) {
adUnitIds.add(k);
}
});
return adUnitIds;
}
/**
* 根據UnitIds查詢AdUnit list
*/
public List<AdUnitIndexObject> fetch(Collection<Long> adUnitIds) {
if (CollectionUtils.isEmpty(adUnitIds)) {
return Collections.EMPTY_LIST;
}
List<AdUnitIndexObject> result = new ArrayList<>();
adUnitIds.forEach(id -> {
AdUnitIndexObject object = get(id);
if (null == object) {
log.error("AdUnitIndexObject does not found:{}", id);
return;
}
result.add(object);
});
return result;
}
上述我們準備了一系列的查詢方法,都是為了根據流量類型查詢廣告單元信息,我們現在開始實現我們的查詢接口,查詢接口中,我們可以獲取到媒體方的請求對象信息,它帶有一系列查詢所需要的過濾參數:
/**
* SearchImpl for 實現search 服務
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@Service
@Slf4j
public class SearchImpl implements ISearch {
@Override
public SearchResponse fetchAds(SearchRequest request) {
//獲取請求廣告位信息
List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();
//獲取三個Feature信息
KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();
DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
//Feature關系
FeatureRelation featureRelation = request.getFeatureInfo().getRelation();
//構造響應對象
SearchResponse response = new SearchResponse();
Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();
for (AdSlot adSlot : adSlotList) {
Set<Long> targetUnitIdSet;
//根據流量類型從緩存中獲取 初始 廣告信息
Set<Long> adUnitIdSet = IndexDataTableUtils.of(
AdUnitIndexAwareImpl.class
).match(adSlot.getPositionType());
}
return null;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。