溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 17. 根據(jù)流量類(lèi)型查詢(xún)廣告

發(fā)布時(shí)間:2020-08-03 02:10:08 來(lái)源:網(wǎng)絡(luò) 閱讀:411 作者:zhangpan0614 欄目:編程語(yǔ)言
廣告檢索服務(wù)
功能介紹

[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 17. 根據(jù)流量類(lèi)型查詢(xún)廣告

媒體方(手機(jī)APP打開(kāi)的展示廣告,走在路上看到的大屏幕廣告等等)

請(qǐng)求數(shù)據(jù)對(duì)象實(shí)現(xiàn)

從上圖我們可以看出,在媒體方向我們的廣告檢索系統(tǒng)發(fā)起請(qǐng)求的時(shí)候,請(qǐng)求中會(huì)有很多的請(qǐng)求參數(shù)信息,他們分為了三個(gè)部分,我們來(lái)編碼封裝這幾個(gè)參數(shù)對(duì)象信息以及我們請(qǐng)求本身的信息。Let's code.

  • 創(chuàng)建廣告檢索請(qǐng)求接口

/**
 * ISearch for 請(qǐng)求接口,
 * 根據(jù)廣告請(qǐng)求對(duì)象,獲取廣告響應(yīng)信息
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@FunctionalInterface
public interface ISearch {

    /**
     * 根據(jù)請(qǐng)求返回廣告結(jié)果
     */
    SearchResponse fetchAds(SearchRequest request);
}
  • 創(chuàng)建SearchRequest,包含三部分:mediaId,RequestInfo,FeatureInfo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchRequest {

    //媒體方請(qǐng)求標(biāo)示
    private String mediaId;
    //請(qǐng)求基本信息
    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;
    }
}

其他的對(duì)象大家可以去github傳送門(mén) & gitee傳送門(mén) 下載源碼。

[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 17. 根據(jù)流量類(lèi)型查詢(xún)廣告

檢索響應(yīng)對(duì)象定義
/**
 * SearchResponse for 檢索API響應(yīng)對(duì)象
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchResponse {

    //一個(gè)廣告位,可以展示多個(gè)廣告
    //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;

        //展示監(jiān)控url
        private List<String> showMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
        //點(diǎn)擊監(jiān)控url
        private List<String> clickMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
    }

    /**
     * 我們的檢索服務(wù)針對(duì)的是內(nèi)存中的索引檢索,那么我們就需要一個(gè)轉(zhuǎn)換方法
     */
    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();
    }
}
根據(jù)流量類(lèi)型廣告過(guò)濾

流量類(lèi)型本身屬于推廣單元下的類(lèi)目,有很多種類(lèi)貼片廣告,開(kāi)屏廣告等等,這些類(lèi)型需要同步到媒體方,媒體方會(huì)根據(jù)不同的流量類(lèi)型發(fā)起不同的廣告請(qǐng)求,我們需要先定義一個(gè)流量類(lèi)型的信息類(lèi)。

public class AdUnitConstants {
    public static class PositionType{
        //App啟動(dòng)時(shí)展示的、展示時(shí)間短暫的全屏化廣告形式。
        private static final int KAIPING = 1;
        //電影開(kāi)始之前的廣告
        private static final int TIEPIAN = 2;
        //電影播放中途廣告
        private static final int TIEPIAN_MIDDLE = 4;
        //暫停視頻時(shí)候播放的廣告
        private static final int TIEPIAN_PAUSE = 8;
        //視頻播放完
        private static final int TIEPIAN_POST = 16;
    }
}

從上述類(lèi)型的數(shù)字,我們可以看出是2的倍數(shù),這是為了使用位運(yùn)算提升性能。

com.sxzhongf.ad.index.adunit.AdUnitIndexObject中,我們添加類(lèi)型校驗(yàn)方法:

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;
        }
    }

    /**
     * 與運(yùn)算,低位取等,高位補(bǔ)零。
     * 如果 > 0,則為開(kāi)屏
     */
    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;
    }

無(wú)所如何,我們都是需要根據(jù)positionType進(jìn)行數(shù)據(jù)查詢(xún)過(guò)濾,我們?cè)谥暗?code>com.sxzhongf.ad.index.adunit.AdUnitIndexAwareImpl中添加2個(gè)方法來(lái)實(shí)現(xiàn)過(guò)濾:

/**
     * 過(guò)濾當(dāng)前是否存在滿(mǎn)足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;
    }

    /**
     * 根據(jù)UnitIds查詢(xún)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;
    }
  • 實(shí)現(xiàn)Search服務(wù)接口

上述我們準(zhǔn)備了一系列的查詢(xún)方法,都是為了根據(jù)流量類(lèi)型查詢(xún)廣告單元信息,我們現(xiàn)在開(kāi)始實(shí)現(xiàn)我們的查詢(xún)接口,查詢(xún)接口中,我們可以獲取到媒體方的請(qǐng)求對(duì)象信息,它帶有一系列查詢(xún)所需要的過(guò)濾參數(shù):

/**
 * SearchImpl for 實(shí)現(xiàn)search 服務(wù)
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Service
@Slf4j
public class SearchImpl implements ISearch {
    @Override
    public SearchResponse fetchAds(SearchRequest request) {

        //獲取請(qǐng)求廣告位信息
        List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();

        //獲取三個(gè)Feature信息
        KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
        HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();
        DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
        //Feature關(guān)系
        FeatureRelation featureRelation = request.getFeatureInfo().getRelation();

        //構(gòu)造響應(yīng)對(duì)象
        SearchResponse response = new SearchResponse();
        Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();

        for (AdSlot adSlot : adSlotList) {
            Set<Long> targetUnitIdSet;
            //根據(jù)流量類(lèi)型從緩存中獲取 初始 廣告信息
            Set<Long> adUnitIdSet = IndexDataTableUtils.of(
                    AdUnitIndexAwareImpl.class
            ).match(adSlot.getPositionType());
        }
        return null;
    }
}
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI