热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

Springcloud查询返回广告创意实例代码

在本篇文章里小编给大家整理的是关于Springcloud查询返回广告创意实例代码,需要的朋友们可以跟着学习下。

根据三个维度继续过滤

在上一节中我们实现了根据流量信息过滤的代码,但是我们的条件有可能是多条件一起传给我们的检索服务的,本节我们继续实现根据推广单元的三个维度条件的过滤。

在SearchImpl类中添加过滤方法

public class SearchImpl implements ISearch {
  @Override
  public SearchResponse fetchAds(SearchRequest request) {
    ...
      // 根据三个维度过滤
      if (featureRelation == FeatureRelation.AND) {
        filterKeywordFeature(adUnitIdSet, keywordFeature);
        filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(adUnitIdSet, districtFeature);

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

定义三个方法实现过滤

/**
   * 获取三个维度各自满足时的广告id
   */
  private Set getOrRelationUnitIds(Set adUnitIdsSet,
                      KeywordFeature keywordFeature,
                      HobbyFeatrue hobbyFeatrue,
                      DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIdsSet)) return Collections.EMPTY_SET;

    // 我们在处理的时候,需要对副本进行处理,大家可以考虑一下为什么需要这么做?
    Set keywordUnitIdSet = new HashSet<>(adUnitIdsSet);
    Set hobbyUnitIdSet = new HashSet<>(adUnitIdsSet);
    Set districtUnitIdSet = new HashSet<>(adUnitIdsSet);

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

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

  /**
   * 根据传递的关键词过滤
   */
  private void filterKeywordFeature(Collection adUnitIds, KeywordFeature keywordFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    if (CollectionUtils.isNotEmpty(keywordFeature.getKeywords())) {
      // 如果存在需要过滤的关键词,查找索引实例对象进行过滤处理
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitKeywordIndexAwareImpl.class)
                          .match(adUnitId, keywordFeature.getKeywords())
      );
    }
  }

  /**
   * 根据传递的兴趣信息过滤
   */
  private void filterHobbyFeature(Collection adUnitIds, HobbyFeatrue hobbyFeatrue) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要过滤的兴趣,查找索引实例对象进行过滤处理
    if (CollectionUtils.isNotEmpty(hobbyFeatrue.getHobbys())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> IndexDataTableUtils.of(UnitHobbyIndexAwareImpl.class)
                          .match(adUnitId, hobbyFeatrue.getHobbys())
      );
    }
  }

  /**
   * 根据传递的地域信息过滤
   */
  private void filterDistrictFeature(Collection adUnitIds, DistrictFeature districtFeature) {
    if (CollectionUtils.isEmpty(adUnitIds)) return;
    // 如果存在需要过滤的地域信息,查找索引实例对象进行过滤处理
    if (CollectionUtils.isNotEmpty(districtFeature.getProvinceAndCities())) {
      CollectionUtils.filter(
          adUnitIds,
          adUnitId -> {
            return IndexDataTableUtils.of(UnitDistrictIndexAwareImpl.class)
                         .match(adUnitId, districtFeature.getProvinceAndCities());
          }
      );
    }
  }

根据推广单元id获取推广创意

我们知道,推广单元和推广创意的关系是多对多,从上文我们查询到了推广单元ids,接下来我们实现根据推广单元id获取推广创意的代码,let's code.

首先,我们需要在com.sxzhongf.ad.index.creative_relation_unit.CreativeRelationUnitIndexAwareImpl 关联索引中查到推广创意的ids

/**
   * 通过推广单元id获取推广创意id
   */
  public List selectAdCreativeIds(List unitIndexObjects) {
    if (CollectionUtils.isEmpty(unitIndexObjects)) return Collections.emptyList();

    //获取要返回的广告创意ids
    List result = new ArrayList<>();
    for (AdUnitIndexObject unitIndexObject : unitIndexObjects) {
      //根据推广单元id获取推广创意
      Set adCreativeIds = unitRelationCreativeMap.get(unitIndexObject.getUnitId());
      if (CollectionUtils.isNotEmpty(adCreativeIds)) result.addAll(adCreativeIds);
    }

    return result;
  }

然后得到了推广创意的id list后,我们在创意索引实现类com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl中定义根据ids查询创意的方法。

/**
 * 根据ids获取创意list
 */
public List findAllByIds(Collection ids) {
  if (CollectionUtils.isEmpty(ids)) return Collections.emptyList();
  List result = new ArrayList<>();

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

  return result;
}

自此,我们已经得到了想要的推广单元和推广创意,因为推广单元包含了推广计划,所以我们想要的数据已经全部可以获取到了,接下来,我们还得过滤一次当前我们查询到的数据的状态,因为有的数据,我们可能已经进行过逻辑删除了,因此还需要判断获取的数据是否有效。在SearchImpl类中实现。

 /**
  * 根据状态信息过滤数据
  */
 private void filterAdUnitAndPlanStatus(List unitIndexObjects, CommonStatus status) {
   if (CollectionUtils.isEmpty(unitIndexObjects)) return;

   //同时判断推广单元和推广计划的状态
   CollectionUtils.filter(
       unitIndexObjects,
       unitIndexObject -> unitIndexObject.getUnitStatus().equals(status.getStatus()) &&
           unitIndexObject.getAdPlanIndexObject().getPlanStatus().equals(status.getStatus())
   );
 }

在SearchImpl中我们实现广告创意的查询.

...

//获取 推广计划 对象list
List unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class).fetch(adUnitIdSet);
//根据状态过滤数据
filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);
//获取 推广创意 id list
List creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                      .selectAdCreativeIds(unitIndexObjects);
//根据 推广创意ids获取推广创意
List creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
...

根据广告位adslot 实现对创意数据的过滤

因为我们的广告位是有不同的大小,不同的类型,因此,我们在获取到所有符合我们查询维度以及流量类型的条件后,还需要针对不同的广告位来展示不同的广告创意信息。

/**
* 根据广告位类型以及参数获取展示的合适广告信息
*
* @param creativeIndexObjects 所有广告创意
* @param width        广告位width
* @param height        广告位height
*/
private void filterCreativeByAdSlot(List creativeIndexObjects,
                 Integer width,
                 Integer height,
                 List type) {
 if (CollectionUtils.isEmpty(creativeIndexObjects)) return;

 CollectionUtils.filter(
     creativeIndexObjects,
     creative -> {
       //审核状态必须是通过
       return creative.getAuditStatus().equals(CommonStatus.VALID.getStatus())
           && creative.getWidth().equals(width)
           && creative.getHeight().equals(height)
           && type.contains(creative.getType());
     }
 );
}

组建搜索返回对象

正常业务场景中,同一个广告位可以展示多个广告信息,也可以只展示一个广告信息,这个需要根据具体的业务场景来做不同的处理,本次为了演示方便,会从返回的创意列表中随机选择一个创意广告信息进行展示,当然大家也可以根据业务类型,设置不同的优先级或者权重值来进行广告选择。

/**
 * 从创意列表中随机获取一条创意广告返回出去
 *
 * @param creativeIndexObjects 创意广告list
 */
private List buildCreativeResponse(List creativeIndexObjects) {
  if (CollectionUtils.isEmpty(creativeIndexObjects)) return Collections.EMPTY_LIST;

  //随机获取一个广告创意,也可以实现优先级排序,也可以根据权重值等等,具体根据业务
  CreativeIndexObject randomObject = creativeIndexObjects.get(
      Math.abs(new Random().nextInt()) % creativeIndexObjects.size()
  );
  //List result = new ArrayList<>();
  //result.add(SearchResponse.convert(randomObject));

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

完整的请求过滤实现方法:

@Service
@Slf4j
public class SearchImpl implements ISearch {
  @Override
  public SearchResponse fetchAds(SearchRequest request) {

    //获取请求广告位信息
    List 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> adSlotRelatiOnAds= response.getAdSlotRelationAds();

    for (AdSlot adSlot : adSlotList) {
      Set targetUnitIdSet;
      //根据流量类型从缓存中获取 初始 广告信息
      Set adUnitIdSet = IndexDataTableUtils.of(
          AdUnitIndexAwareImpl.class
      ).match(adSlot.getPositionType());

      // 根据三个维度过滤
      if (featureRelation == FeatureRelation.AND) {
        filterKeywordFeature(adUnitIdSet, keywordFeature);
        filterHobbyFeature(adUnitIdSet, hobbyFeatrue);
        filterDistrictFeature(adUnitIdSet, districtFeature);

        targetUnitIdSet = adUnitIdSet;
      } else {
        targetUnitIdSet = getOrRelationUnitIds(adUnitIdSet, keywordFeature, hobbyFeatrue, districtFeature);
      }
      //获取 推广计划 对象list
      List unitIndexObjects = IndexDataTableUtils.of(AdUnitIndexAwareImpl.class)
                                     .fetch(targetUnitIdSet);
      //根据状态过滤数据
      filterAdUnitAndPlanStatus(unitIndexObjects, CommonStatus.VALID);

      //获取 推广创意 id list
      List creativeIds = IndexDataTableUtils.of(CreativeRelationUnitIndexAwareImpl.class)
                            .selectAdCreativeIds(unitIndexObjects);
      //根据 推广创意ids获取推广创意
      List creativeIndexObjects = IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
                                        .fetch(creativeIds);

      //根据 广告位adslot 实现对创意数据的过滤
      filterCreativeByAdSlot(creativeIndexObjects, adSlot.getWidth(), adSlot.getHeight(), adSlot.getType());

      //一个广告位可以展示多个广告,也可以仅展示一个广告,具体根据业务来定
      adSlotRelationAds.put(
          adSlot.getAdSlotCode(),
          buildCreativeResponse(creativeIndexObjects)
      );
    }

    return response;
  }
  ...

检索服务对外提供

暴露API接口
上文中,我们实现了检索服务的核心逻辑,接下来,我们需要对外暴露我们的广告检索服务接口,在SearchController中提供:

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

实现API网关配置

zuul:
routes:
  sponsor: #在路由中自定义服务路由名称
  path: /ad-sponsor/**
  serviceId: mscx-ad-sponsor #微服务name
  strip-prefix: false
  search: #在路由中自定义服务路由名称
  path: /ad-search/**
  serviceId: mscx-ad-search #微服务name
  strip-prefix: false
prefix: /gateway/api
strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀

以上就是本次分享的全部知识点内容,感谢大家对的支持


推荐阅读
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 深入解析:阿里实战 SpringCloud 微服务架构与应用
    本文将详细介绍 SpringCloud 在微服务架构中的应用,涵盖入门、实战和案例分析。通过丰富的代码示例和实际项目经验,帮助读者全面掌握 SpringCloud 的核心技术和最佳实践。 ... [详细]
  • 本文详细介绍了在企业级项目中如何优化 Webpack 配置,特别是在 React 移动端项目中的最佳实践。涵盖资源压缩、代码分割、构建范围缩小、缓存机制以及性能优化等多个方面。 ... [详细]
  • FinOps 与 Serverless 的结合:破解云成本难题
    本文探讨了如何通过 FinOps 实践优化 Serverless 应用的成本管理,提出了首个 Serverless 函数总成本估计模型,并分享了多种有效的成本优化策略。 ... [详细]
  • 探讨了如何解决Ajax请求响应时间过长的问题。本文分析了一个从服务器获取少量数据的Ajax请求,尽管服务器已经对JSON响应进行了缓存,但实际响应时间仍然不稳定。 ... [详细]
  • 本文介绍如何在Grafana配置面板时,使用JSONNet获取数组中特定元素的位置,并将其应用于动态服务查询。 ... [详细]
  • 深入解析Spring Cloud微服务架构与分布式系统实战
    本文详细介绍了Spring Cloud在微服务架构和分布式系统中的应用,结合实际案例和最新技术,帮助读者全面掌握微服务的实现与优化。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 谷粒商城统一返回对象R的泛型data获取异常及解决方案
    在实现过程中遇到空指针错误,经过定位发现远程调用后使用泛型的统一返回对象R无法正确获取data值。本文详细分析问题原因并提供解决方案。 ... [详细]
  • 本文详细解析了如何使用Python的urllib模块发起POST请求,并通过实例展示如何爬取百度翻译的翻译结果。 ... [详细]
  • 本文探讨了在UC浏览器中调用分享面板后,图片无法正常显示的问题,并提供了详细的解决方法和代码示例。 ... [详细]
  • 本文介绍如何在PostgreSQL数据库中正确插入和处理JSON数据类型,确保数据完整性和避免常见错误。 ... [详细]
  • 本文探讨了现代分布式架构的多样性,包括高并发、多活数据中心、容器化、微服务、高可用性和弹性架构等,并介绍了与这些架构相关的重要管理技术,如DevOps、应用监控和自动化运维。文章还深入分析了分布式系统的核心概念、主要用途及类型,同时对比了单体应用与分布式服务化的优缺点。 ... [详细]
  • 最新计算机专业原创毕业设计参考选题都有源码+数据库是近期作品ling取参考你的选题刚好在下面有,有时间看到机会给您发1ssm资源循环利用2springboot校园考勤系统3ssm防 ... [详细]
  • 本文详细探讨了在微服务架构中,使用Feign进行远程调用时出现的请求头丢失问题,并提供了具体的解决方案。重点讨论了单线程和异步调用两种场景下的处理方法。 ... [详细]
author-avatar
mobiledu2502887531
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有