最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring cloud 查詢返回廣告創(chuàng)意實(shí)例代碼

 更新時(shí)間:2019年08月14日 08:30:11   作者:zhangpan0614  
在本篇文章里小編給大家整理的是關(guān)于Spring cloud 查詢返回廣告創(chuàng)意實(shí)例代碼,需要的朋友們可以跟著學(xué)習(xí)下。

根據(jù)三個(gè)維度繼續(xù)過(guò)濾

在上一節(jié)中我們實(shí)現(xiàn)了根據(jù)流量信息過(guò)濾的代碼,但是我們的條件有可能是多條件一起傳給我們的檢索服務(wù)的,本節(jié)我們繼續(xù)實(shí)現(xiàn)根據(jù)推廣單元的三個(gè)維度條件的過(guò)濾。

在SearchImpl類(lèi)中添加過(guò)濾方法

public class SearchImpl implements ISearch {
  @Override
  public SearchResponse fetchAds(SearchRequest request) {
    ...
      // 根據(jù)三個(gè)維度過(guò)濾
      if (featureRelation == FeatureRelation.AND) {
        filterKeywordFeature(adUnitIdSet, keywordFeature);
        filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(adUnitIdSet, districtFeature);

        targetUnitIdSet = adUnitIdSet;
      } else {
        getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
      }
    }
    return null;
  }

定義三個(gè)方法實(shí)現(xiàn)過(guò)濾

/**
   * 獲取三個(gè)維度各自滿足時(shí)的廣告id
   */
  private Set<Long> getOrRelationUnitIds(Set<Long> adUnitIdsSet,
                      KeywordFeature keywordFeature,
                      HobbyFeatrue hobbyFeatrue,
                      DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET;

    // 我們?cè)谔幚淼臅r(shí)候,需要對(duì)副本進(jìn)行處理,大家可以考慮一下為什么需要這么做?
    Set<Long> keywordUnitIdSet = new HashSet<>(adUnitIdsSet);
    Set<Long> hobbyUnitIdSet = new HashSet<>(adUnitIdsSet);
    Set<Long> districtUnitIdSet = new HashSet<>(adUnitIdsSet);

    filterKeywordFeature(keywordUnitIdSet, keywordFeature);
    filterHobbyFeature(hobbyUnitIdSet, hobbyFeatrue);
    filterDistrictFeature(districtUnitIdSet, districtFeature);

    // 返回它們的并集
    return new HashSet<>(
        CollectionUtils.union(
            CollectionUtils.union(keywordUnitIdSet, hobbyUnitIdSet),
            districtUnitIdSet
        )
    );
  }

  /**
   * 根據(jù)傳遞的關(guān)鍵詞過(guò)濾
   */
  private void filterKeywordFeature(Collection<Long> adUnitIds, KeywordFeature keywordFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {
      // 如果存在需要過(guò)濾的關(guān)鍵詞,查找索引實(shí)例對(duì)象進(jìn)行過(guò)濾處理
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class)
                          .match(adUnitId, keywordFeature.getKeywords())
      );
    }
  }

  /**
   * 根據(jù)傳遞的興趣信息過(guò)濾
   */
  private void filterHobbyFeature(Collection<Long> adUnitIds, HobbyFeatrue hobbyFeatrue) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要過(guò)濾的興趣,查找索引實(shí)例對(duì)象進(jìn)行過(guò)濾處理
    if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class)
                          .match(adUnitId, hobbyFeatrue.getHobbys())
      );
    }
  }

  /**
   * 根據(jù)傳遞的地域信息過(guò)濾
   */
  private void filterDistrictFeature(Collection<Long> adUnitIds, DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要過(guò)濾的地域信息,查找索引實(shí)例對(duì)象進(jìn)行過(guò)濾處理
    if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> {
            return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
                         .match(adUnitId, districtFeature.getProvinceAndCities());
          }
      );
    }
  }

根據(jù)推廣單元id獲取推廣創(chuàng)意

我們知道,推廣單元和推廣創(chuàng)意的關(guān)系是多對(duì)多,從上文我們查詢到了推廣單元ids,接下來(lái)我們實(shí)現(xiàn)根據(jù)推廣單元id獲取推廣創(chuàng)意的代碼,let's code.

首先,我們需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 關(guān)聯(lián)索引中查到推廣創(chuàng)意的ids

/**
   * 通過(guò)推廣單元id獲取推廣創(chuàng)意id
   */
  public List<Long> selectAdCreativeIds(List<AdUnitIndexObject> unitIndexObjects) {
    if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList();

    //獲取要返回的廣告創(chuàng)意ids
    List<Long> result = new ArrayList<>();
    for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {
      //根據(jù)推廣單元id獲取推廣創(chuàng)意
      Set<Long> adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());
      if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds);
    }

    return result;
  }

然后得到了推廣創(chuàng)意的id list后,我們?cè)趧?chuàng)意索引實(shí)現(xiàn)類(lèi)com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl中定義根據(jù)ids查詢創(chuàng)意的方法。

/**
 * 根據(jù)ids獲取創(chuàng)意list
 */
public List<CreativeIndexObject> findAllByIds(Collection<Long> ids) {
  if (CollectionUtils.isEmpty(ids)) return Collections.emptyList();
  List<CreativeIndexObject> result = new ArrayList<>();

  for (Long id : ids) {
    CreativeIndexObject object = get(id);
    if (null != object)
      result.add(object);
  }

  return result;
}

自此,我們已經(jīng)得到了想要的推廣單元和推廣創(chuàng)意,因?yàn)橥茝V單元包含了推廣計(jì)劃,所以我們想要的數(shù)據(jù)已經(jīng)全部可以獲取到了,接下來(lái),我們還得過(guò)濾一次當(dāng)前我們查詢到的數(shù)據(jù)的狀態(tài),因?yàn)橛械臄?shù)據(jù),我們可能已經(jīng)進(jìn)行過(guò)邏輯刪除了,因此還需要判斷獲取的數(shù)據(jù)是否有效。在SearchImpl類(lèi)中實(shí)現(xiàn)。

 /**
  * 根據(jù)狀態(tài)信息過(guò)濾數(shù)據(jù)
  */
 private void filterAdUnitAndPlanStatus(List<AdUnitIndexObject> unitIndexObjects, CommonStatus status) {
   if (CollectionUtils.isEmpty(unitIndexObjects)) return;

   //同時(shí)判斷推廣單元和推廣計(jì)劃的狀態(tài)
   CollectionUtils.filter(
       unitIndexObjects,
       unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) &&
           unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())
   );
 }

在SearchImpl中我們實(shí)現(xiàn)廣告創(chuàng)意的查詢.

...

//獲取 推廣計(jì)劃 對(duì)象list
List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);
//根據(jù)狀態(tài)過(guò)濾數(shù)據(jù)
filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
//獲取 推廣創(chuàng)意 id list
List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                      .selectAdCreativeIds(unitIndexObjects);
//根據(jù) 推廣創(chuàng)意ids獲取推廣創(chuàng)意
List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
...

根據(jù)廣告位adslot 實(shí)現(xiàn)對(duì)創(chuàng)意數(shù)據(jù)的過(guò)濾

因?yàn)槲覀兊膹V告位是有不同的大小,不同的類(lèi)型,因此,我們?cè)讷@取到所有符合我們查詢維度以及流量類(lèi)型的條件后,還需要針對(duì)不同的廣告位來(lái)展示不同的廣告創(chuàng)意信息。

/**
* 根據(jù)廣告位類(lèi)型以及參數(shù)獲取展示的合適廣告信息
*
* @param creativeIndexObjects 所有廣告創(chuàng)意
* @param width        廣告位width
* @param height        廣告位height
*/
private void filterCreativeByAdSlot(List<CreativeIndexObject> creativeIndexObjects,
                 Integer width,
                 Integer height,
                 List<Integer> type) {
 if (CollectionUtils.isEmpty(creativeIndexObjects)) return;

 CollectionUtils.filter(
     creativeIndexObjects,
     creative -> {
       //審核狀態(tài)必須是通過(guò)
       return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
           && creative.getWidth().equals(width)
           && creative.getHeight().equals(height)
           && type.contains(creative.getType());
     }
 );
}

組建搜索返回對(duì)象

正常業(yè)務(wù)場(chǎng)景中,同一個(gè)廣告位可以展示多個(gè)廣告信息,也可以只展示一個(gè)廣告信息,這個(gè)需要根據(jù)具體的業(yè)務(wù)場(chǎng)景來(lái)做不同的處理,本次為了演示方便,會(huì)從返回的創(chuàng)意列表中隨機(jī)選擇一個(gè)創(chuàng)意廣告信息進(jìn)行展示,當(dāng)然大家也可以根據(jù)業(yè)務(wù)類(lèi)型,設(shè)置不同的優(yōu)先級(jí)或者權(quán)重值來(lái)進(jìn)行廣告選擇。

/**
 * 從創(chuàng)意列表中隨機(jī)獲取一條創(chuàng)意廣告返回出去
 *
 * @param creativeIndexObjects 創(chuàng)意廣告list
 */
private List<SearchResponse.Creative> buildCreativeResponse(List<CreativeIndexObject> creativeIndexObjects) {
  if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST;

  //隨機(jī)獲取一個(gè)廣告創(chuàng)意,也可以實(shí)現(xiàn)優(yōu)先級(jí)排序,也可以根據(jù)權(quán)重值等等,具體根據(jù)業(yè)務(wù)
  CreativeIndexObject randomObject = creativeIndexObjects.get(
      Math.abs(new Random().nextInt()) % creativeIndexObjects.size()
  );
  //List<SearchResponse.Creative> result = new ArrayList<>();
  //result.add(SearchResponse.convert(randomObject));

  return Collections.singletonList(
      SearchResponse.convert(randomObject)
  );
}

完整的請(qǐng)求過(guò)濾實(shí)現(xiàn)方法:

@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());

      // 根據(jù)三個(gè)維度過(guò)濾
      if (featureRelation == FeatureRelation.AND) {
        filterKeywordFeature(adUnitIdSet, keywordFeature);
        filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(adUnitIdSet, districtFeature);

        targetUnitIdSet = adUnitIdSet;
      } else {
        targetUnitIdSet = getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
      }
      //獲取 推廣計(jì)劃 對(duì)象list
      List<AdUnitIndexObject> unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class)
                                     .fetch(targetUnitIdSet);
      //根據(jù)狀態(tài)過(guò)濾數(shù)據(jù)
      filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);

      //獲取 推廣創(chuàng)意 id list
      List<Long> creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                            .selectAdCreativeIds(unitIndexObjects);
      //根據(jù) 推廣創(chuàng)意ids獲取推廣創(chuàng)意
      List<CreativeIndexObject> creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
                                        .fetch(creativeIds);

      //根據(jù) 廣告位adslot 實(shí)現(xiàn)對(duì)創(chuàng)意數(shù)據(jù)的過(guò)濾
      filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType());

      //一個(gè)廣告位可以展示多個(gè)廣告,也可以僅展示一個(gè)廣告,具體根據(jù)業(yè)務(wù)來(lái)定
      adSlotRelationAds.put(
          adSlot.getAdSlotCode(),
          buildCreativeResponse(creativeIndexObjects)
      );
    }

    return response;
  }
  ...

檢索服務(wù)對(duì)外提供

暴露API接口
上文中,我們實(shí)現(xiàn)了檢索服務(wù)的核心邏輯,接下來(lái),我們需要對(duì)外暴露我們的廣告檢索服務(wù)接口,在SearchController中提供:

@PostMapping("/fetchAd")
  public SearchResponse fetchAdCreative(@RequestBody SearchRequest request) {
    log.info("ad-serach: fetchAd ->{}", JSON.toJSONString(request));
    return search.fetchAds(request);
  }

實(shí)現(xiàn)API網(wǎng)關(guān)配置

zuul:
routes:
  sponsor: #在路由中自定義服務(wù)路由名稱(chēng)
  path: /ad-sponsor/**
  serviceId: mscx-ad-sponsor #微服務(wù)name
  strip-prefix: false
  search: #在路由中自定義服務(wù)路由名稱(chēng)
  path: /ad-search/**
  serviceId: mscx-ad-search #微服務(wù)name
  strip-prefix: false
prefix: /gateway/api
strip-prefix: true #不對(duì) prefix: /gateway/api 設(shè)置的路徑進(jìn)行截取,默認(rèn)轉(zhuǎn)發(fā)會(huì)截取掉配置的前綴

以上就是本次分享的全部知識(shí)點(diǎn)內(nèi)容,感謝大家對(duì)腳本之家的支持

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)

    SpringBoot實(shí)現(xiàn)跨域的幾種常用方式總結(jié)

    跨域是指一個(gè)域下的文檔或腳本試圖去請(qǐng)求另一個(gè)域下的資源,或者涉及到兩個(gè)不同域名的資源之間的交互,由于同源策略(Same Origin Policy)的限制,瀏覽器不允許跨域請(qǐng)求,本文小編給大家分享了SpringBoot實(shí)現(xiàn)跨域的幾種常用方式,需要的朋友可以參考下
    2023-09-09
  • Spring解析配置類(lèi)和掃描包路徑的詳細(xì)過(guò)程

    Spring解析配置類(lèi)和掃描包路徑的詳細(xì)過(guò)程

    這篇文章主要介紹了Spring解析配置類(lèi)和掃描包路徑的詳細(xì)過(guò)程,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-12-12
  • java 數(shù)據(jù)結(jié)構(gòu)并查集詳解

    java 數(shù)據(jù)結(jié)構(gòu)并查集詳解

    并查集是一種用來(lái)管理元素分組情況的數(shù)據(jù)結(jié)構(gòu)。并查集可以高效地進(jìn)行如下操作。本文將通過(guò)Java實(shí)現(xiàn)并查集,感興趣的小伙伴可以了解一下
    2022-03-03
  • Day14基礎(chǔ)不牢地動(dòng)山搖-Java基礎(chǔ)

    Day14基礎(chǔ)不牢地動(dòng)山搖-Java基礎(chǔ)

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑

    Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑

    這篇文章主要介紹了Spring整合Mybatis使用<context:property-placeholder>時(shí)的坑 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Java反射技術(shù)詳解

    Java反射技術(shù)詳解

    這篇文章主要結(jié)合實(shí)例形式分析了介紹了Java反射技術(shù),小編覺(jué)得這篇文章寫(xiě)的還不錯(cuò),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • 詳解Java中日期工具類(lèi)的操作

    詳解Java中日期工具類(lèi)的操作

    這篇文章主要為大家詳細(xì)介紹了Java中日期工具類(lèi)的常見(jiàn)操作,如:字符串和Date互轉(zhuǎn)、字符串和LocalDate互轉(zhuǎn)等,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-11-11
  • Jenkins忘記密碼密碼重置操作步驟詳解

    Jenkins忘記密碼密碼重置操作步驟詳解

    這篇文章主要為大家介紹了Jenkins忘記密碼密碼重置操作步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • java將圖片分割為幾個(gè)部分示例

    java將圖片分割為幾個(gè)部分示例

    這篇文章主要介紹了java將圖片分割為幾個(gè)部分示例,需要的朋友可以參考下
    2014-04-04
  • 一文詳解Spring事務(wù)的實(shí)現(xiàn)與本質(zhì)

    一文詳解Spring事務(wù)的實(shí)現(xiàn)與本質(zhì)

    這篇文章主要介紹了Spring中事務(wù)的兩種實(shí)現(xiàn)方式:聲明式事務(wù)、編程式事務(wù)以及他們的本質(zhì)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-04-04

最新評(píng)論

沁源县| 永胜县| 黄冈市| 仙游县| 浪卡子县| 易门县| 修武县| 黄山市| 十堰市| 怀安县| 资兴市| 都匀市| 达孜县| 许昌市| 基隆市| 浙江省| 浦江县| 嵊泗县| 乃东县| 麦盖提县| 崇文区| 宜春市| 古田县| 鹤峰县| 渝中区| 永寿县| 衢州市| 杂多县| 大英县| 临桂县| 福鼎市| 永丰县| 彝良县| 民县| 方城县| 乃东县| 闻喜县| 收藏| 德钦县| 海南省| 柳河县|