广告检索服务

功能介绍

媒体方(手机APP打开的展示广告,走在路上看到的大屏幕广告等等)

请求数据对象实现

从上图我们可以看出,在媒体方向我们的广告检索系统发起请求的时候,请求中会有很多的请求参数信息,他们分为了三个部分,我们来编码封装这几个参数对象信息以及我们请求本身的信息。Let's code.

  • 创建广告检索请求接口

/**
* ISearch for 请求接口,
* 根据广告请求对象,获取广告响应信息
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@FunctionalInterface
public interface ISearch { /**
* 根据请求返回广告结果
*/
SearchResponse fetchAds(SearchRequest request);
}
  • 创建SearchRequest,包含三部分: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;
}
  • 实现Search服务接口

上述我们准备了一系列的查询方法,都是为了根据流量类型查询广告单元信息,我们现在开始实现我们的查询接口,查询接口中,我们可以获取到媒体方的请求对象信息,它带有一系列查询所需要的过滤参数:

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

[Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告的更多相关文章

  1. SpringCloud(9)使用Spring Cloud OAuth2保护微服务系统

    一.简介 OAth2是一个标准的授权协议. 在认证与授权的过程中,主要包含以下3种角色. 服务提供方 Authorization Server. 资源持有者 Resource Server. 客户端 ...

  2. [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard

    在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...

  3. [Spring cloud 一步步实现广告系统] 21. 系统错误汇总

    广告系统学习过程中问题答疑 博客园 Eureka集群启动报错 Answer 因为Eureka在集群启动过程中,会连接集群中其他的机器进行数据同步,在这个过程中,如果别的服务还没有启动完成,就会出现Co ...

  4. [Spring cloud 一步步实现广告系统] 2. 配置&Eureka服务

    父项目管理 首先,我们在创建投放系统之前,先看一下我们的工程结构: mscx-ad-sponsor就是我们的广告投放系统.如上结构,我们需要首先创建一个Parent Project mscx-ad 来 ...

  5. [Spring cloud 一步步实现广告系统] 22. 广告系统回顾总结

    到目前为止,我们整个初级广告检索系统就初步开发完成了,我们来整体回顾一下我们的广告系统. 整个广告系统编码结构如下: mscx-ad 父模块 主要是为了方便我们项目的统一管理 mscx-ad-db 这 ...

  6. [Spring cloud 一步步实现广告系统] 7. 中期总结回顾

    在前面的过程中,我们创建了4个project: 服务发现 我们使用Eureka 作为服务发现组件,学习了Eureka Server,Eureka Client的使用. Eureka Server 加依 ...

  7. [Spring cloud 一步步实现广告系统] 1. 业务架构分析

    什么是广告系统? 主要包含: 广告主投放广告的<广告投放系统> 媒体方(广告展示媒介-)检索广告用的<广告检索系统> 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用 ...

  8. [Spring cloud 一步步实现广告系统] 13. 索引服务编码实现

    上一节我们分析了广告索引的维护有2种,全量索引加载和增量索引维护.因为广告检索是广告系统中最为重要的环节,大家一定要认真理解我们索引设计的思路,接下来我们来编码实现索引维护功能. 我们来定义一个接口, ...

  9. [Spring cloud 一步步实现广告系统] 20. 系统运行测试

    系统运行 经过长时间的编码实现,我们的主体模块已经大致完成,因为之前我们都是零散的对各个微服务自行测试,接下来,我们需要将所有的服务模块进行联调测试,Let's do it. 清除测试数据&测 ...

随机推荐

  1. vue中局部组件的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. linux学习书籍推荐《鸟哥的Linux私房菜》下载

    下载地址:点我 <鸟哥的Linux私房菜:基础学习篇>是具有知名度的Linux入门书<鸟哥的Linux私房菜基础学习篇>的最新版,全面而详细地介绍了Linux操作系统.< ...

  3. 谷歌浏览器 Google Chrome 70.0.3538.102 便携版

    oogle Chrome 是由Google开发的一款设计简单.运行高效.支持扩展的浏览器,它基于高速WebKit/Blink内核和高性能JavaScript V8引擎,在支持多标签浏览的基础上,提供顺 ...

  4. 关于爬虫平台的架构实现和框架的选型(二)--scrapy的内部实现以及实时爬虫的实现

    我们接着关于爬虫平台的架构实现和框架的选型(一)继续来讲爬虫框架的架构实现和狂阶的选型. 前面介绍了scrapy的基本操作,下面介绍下scrapy爬虫的内部实现架构如下图 1.Spiders(爬虫): ...

  5. PCB SQL SERVER 数据库阻塞进程关系以思维导图方式呈现的实现方法

    最近公司服务数据库同步机制常发生阻塞,时不时的导致PCB工程系统卡死现象,只有找到阻塞源头并处理掉,才以消除阻塞,但数据库中查看会话阻塞是通过二维表方式展示的父子会话进程ID的,是很难清楚的展示各会话 ...

  6. 【dfs基础讲解及例题】

    定义 DFS(Depth-First-Search)深度优先搜索算法,是搜索算法的一种. 接下来因为懒得去找大段大段深奥的材料 所以就是一些个人的理解. 所谓深搜,是相对于广搜(只是第一篇)来说的.深 ...

  7. VS2012 BIDS之Reporting Service/SSRS 项目2--开发过程问题总结(全)

    由刚开始的接触到现在做出来一个基本完整的SSRS的项目,学到了比较多的知识,和大家共享. 上一篇学习总结可能有些问题,一起修正和总结. ================================ ...

  8. ISTQB TA - 边界值分析中三值测试法的注意事项

    三值测试法的定义(中文版20150601大纲): 取一个不超过边界.一个在边界上.一个超过边界的值. 这三个值其实还有另外一种叫法,分别是内点.上点和离点. 内点:不超过边界的点 上点:在边界上的点 ...

  9. 算法学习笔记,几个简单的Demo

    算法初学的一些心得 前言:现在工作也快一年多了,有时间下班回家会学学算法,陆陆续续也接触了一些 貌似我知道的就冒泡排序其他的都不是很了解 最近买了一本书,边学边记录吧! 一些常用的方法 暴力破解 下面 ...

  10. AndroidStudio使用genymotion模拟器

    安装Genymotion之前首先要安装好virtualbox这个软件 virtual官方网站:https://www.virtualbox.org/ genymotion的官方网站: https:// ...