[Spring cloud 一步步实现广告系统] 13. 索引服务编码实现
上一节我们分析了广告索引的维护有2种,全量索引加载
和增量索引维护
。因为广告检索是广告系统中最为重要的环节,大家一定要认真理解我们索引设计的思路,接下来我们来编码实现索引维护功能。
我们来定义一个接口,来接收所有index的增删改查操作,接口定义一个范型,来接收2个参数,K
代表我们索引的健值,V
代表返回值。
/**
* IIndexAware for 实现广告索引的增删改查
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
public interface IIndexAware<K, V> {
/**
* 通过key 获取索引
*/
V get(K key);
/**
* 添加索引
* @param key
* @param value
*/
void add(K key, V value);
/**
* 更新索引
*/
void update(K key, V value);
/**
* 删除索引
*/
void delete(K key, V value);
}
我们一定要知道,并不是所有的数据库表都需要创建索引,比如User
表我们在数据检索的时候其实是不需要的,当然也就没必要创建索引,并且,也不是表中的所有字段都需要索引,这个也是根据具体的业务来确定字段信息,比如我们接下来要编写的推广计划
索引中,推广计划名称就可以不需要。下面,我们来实现我们的第一个正向索引
。
- 首先创建操作推广计划的实体对象
/**
* AdPlanIndexObject for 推广计划索引对象
* 这个索引对象我们没有添加 推广计划名称
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanIndexObject {
private Long planId;
private Long userId;
private Integer planStatus;
private Date startDate;
private Date endDate;
/**
* 根据实际字段来更新索引
*/
public void update(AdPlanIndexObject newObject) {
if (null != newObject.getPlanId()) {
this.planId = newObject.getPlanId();
}
if (null != newObject.getUserId()) {
this.userId = newObject.getUserId();
}
if (null != newObject.getPlanStatus()) {
this.planStatus = newObject.getPlanStatus();
}
if (null != newObject.getStartDate()) {
this.startDate = newObject.getStartDate();
}
if (null != newObject.getEndDate()) {
this.endDate = newObject.getEndDate();
}
}
}
- 然后创建推广计划索引实现类,并实现
IIndexAware
接口。
/**
* AdPlanIndexAwareImpl for 推广计划索引实现类
*
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@Slf4j
@Component
public class AdPlanIndexAwareImpl implements IIndexAware<Long, AdPlanIndexObject> {
private static Map<Long, AdPlanIndexObject> planIndexObjectMap;
/**
* 因为操作索引的过程中有可能对索引进行更新,为了防止多线程造成的线程不安全问题,我们不能使用hashmap,需要实现ConcurrentHashMap
*/
static {
planIndexObjectMap = new ConcurrentHashMap<>();
}
@Override
public AdPlanIndexObject get(Long key) {
return planIndexObjectMap.get(key);
}
@Override
public void add(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before add::{}", planIndexObjectMap);
planIndexObjectMap.put(key, value);
log.info("AdPlanIndexAwareImpl after add::{}", planIndexObjectMap);
}
@Override
public void update(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before update::{}", planIndexObjectMap);
//查询当前的索引信息,如果不存在,直接新增索引信息
AdPlanIndexObject oldObj = planIndexObjectMap.get(key);
if (null == oldObj) {
planIndexObjectMap.put(key, value);
} else {
oldObj.update(value);
}
log.info("AdPlanIndexAwareImpl after update::{}", planIndexObjectMap);
}
@Override
public void delete(Long key, AdPlanIndexObject value) {
log.info("AdPlanIndexAwareImpl before delete::{}", planIndexObjectMap);
planIndexObjectMap.remove(key);
log.info("AdPlanIndexAwareImpl after delete::{}", planIndexObjectMap);
}
}
至此,我们已经完成了推广计划的索引对象和索引操作的代码编写,大家可以参考上面的示例,依次完成推广单元
、推广创意
、地域
、兴趣
、关键词
以及推广创意和推广单元的关联索引
,或者可直接从 Github传送门 / Gitee传送门 下载源码。
按照上述代码展示,我们已经实现了所有的索引操作的定义,但是实际情况中,我们需要使用这些服务的时候,需要在每一个Service中
@Autowired
注入,我们那么多的索引操作类,还不包含后续还有可能需要新增的索引维度,工作量实在是太大,而且不方便维护,作为一个合格的程序员来说,这是非常不友好的,也许会让后续的开发人员骂娘。
为了防止后续被骂,我们来编写一个索引缓存工具类com.sxzhongf.ad.index.IndexDataTableUtils
,通过这个索引缓存工具类来实现一次注入,解决后顾之忧。要实现这个工具类,我们需要实现2个接口:org.springframework.context.ApplicationContextAware
和org.springframework.core.PriorityOrdered
org.springframework.context.ApplicationContextAware
, 统一通过实现该接口的类,来操作Spring容器以及其中的Bean实例。
在Spring中,以Aware
为后缀结束的类,大家可以简单的理解为应用程序想要XXX,比如ApplicationContextAware
代表应用程序想要ApplicationContext
,BeanFactoryAware
表示应用程序想要BeanFactory
...等等org.springframework.core.PriorityOrdered
组件加载顺序,也可以使用org.springframework.core.Ordered
以下代码为我们的工具类:
package com.sxzhongf.ad.index;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* IndexDataTableUtils for 所有索引服务需要缓存的Java Bean
*
* 使用方式:
* 获取{@link com.sxzhongf.ad.index.creative.CreativeIndexAwareImpl}索引服务类
* 如下:
* {@code
* IndexDataTableUtils.of(CreativeIndexAwareImpl.class)
* }
* @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
*/
@Component
public class IndexDataTableUtils implements ApplicationContextAware, PriorityOrdered {
//注入ApplicationContext
private static ApplicationContext applicationContext;
/**
* 定义用于保存所有Index的Map
* Class标示我们的索引类
*/
private static final Map<Class, Object> dataTableMap = new ConcurrentHashMap<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
IndexDataTableUtils.applicationContext = applicationContext;
}
/**
* 获取索引服务缓存
*/
public static <T> T of(Class<T> klass) {
T instance = (T) dataTableMap.get(klass);
//如果获取到索引bean,直接返回当前bean
if (null != instance) {
return instance;
}
//首次获取索引bean为空,写入Map
dataTableMap.put(klass, bean(klass));
return (T) dataTableMap.get(klass);
}
/**
* 获取Spring 容器中的Bean对象
*/
private static <T> T bean(String beanName) {
return (T) applicationContext.getBean(beanName);
}
/**
* 获取Spring 容器中的Bean对象
*/
private static <T> T bean(Class klass) {
return (T) applicationContext.getBean(klass);
}
@Override
public int getOrder() {
return PriorityOrdered.HIGHEST_PRECEDENCE;
}
}
[Spring cloud 一步步实现广告系统] 13. 索引服务编码实现的更多相关文章
- [Spring cloud 一步步实现广告系统] 16. 增量索引实现以及投送数据到MQ(kafka)
实现增量数据索引 上一节中,我们为实现增量索引的加载做了充足的准备,使用到mysql-binlog-connector-java 开源组件来实现MySQL 的binlog监听,关于binlog的相关知 ...
- [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard
在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...
- [Spring cloud 一步步实现广告系统] 21. 系统错误汇总
广告系统学习过程中问题答疑 博客园 Eureka集群启动报错 Answer 因为Eureka在集群启动过程中,会连接集群中其他的机器进行数据同步,在这个过程中,如果别的服务还没有启动完成,就会出现Co ...
- [Spring cloud 一步步实现广告系统] 2. 配置&Eureka服务
父项目管理 首先,我们在创建投放系统之前,先看一下我们的工程结构: mscx-ad-sponsor就是我们的广告投放系统.如上结构,我们需要首先创建一个Parent Project mscx-ad 来 ...
- [Spring cloud 一步步实现广告系统] 22. 广告系统回顾总结
到目前为止,我们整个初级广告检索系统就初步开发完成了,我们来整体回顾一下我们的广告系统. 整个广告系统编码结构如下: mscx-ad 父模块 主要是为了方便我们项目的统一管理 mscx-ad-db 这 ...
- [Spring cloud 一步步实现广告系统] 7. 中期总结回顾
在前面的过程中,我们创建了4个project: 服务发现 我们使用Eureka 作为服务发现组件,学习了Eureka Server,Eureka Client的使用. Eureka Server 加依 ...
- [Spring cloud 一步步实现广告系统] 1. 业务架构分析
什么是广告系统? 主要包含: 广告主投放广告的<广告投放系统> 媒体方(广告展示媒介-)检索广告用的<广告检索系统> 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用 ...
- [Spring cloud 一步步实现广告系统] 12. 广告索引介绍
索引设计介绍 在我们广告系统中,为了我们能更快的拿到我们想要的广告数据,我们需要对广告数据添加类似于数据库index一样的索引结构,分两大类:正向索引和倒排索引. 正向索引 通过唯一键/主键生成与对象 ...
- [Spring cloud 一步步实现广告系统] 14. 全量索引代码实现
上一节我们实现了索引基本操作的类以及索引缓存工具类,本小节我们开始实现加载全量索引数据,在加载全量索引数据之前,我们需要先将数据库中的表数据导出到一份文件中.Let's code. 1.首先定义一个常 ...
随机推荐
- 两个div,都设置未inline-block,可是在IE出现错位问题
[实现要求] 红色的和黄色的内容撑开,蓝色包住红黄,不定框居中显示 [遇到问题] chrome显示正常,但是在IE上红黄框会出现错位的问题 [如何解决] 给红色框添加一个overflow:hidde ...
- SSM整合框架(基于IDEA的配置)
Pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...
- C# 控制台输入和输出
目录 从控制台获取输入 将输出写入控制台 Console.Write() Console.WriteLine() 格式字符串 多重标记和值 格式化字符串 索引 对齐说明符 格式字段 标准数字格式说明符 ...
- JS---案例:协议按钮禁用(倒计时)
案例:协议按钮倒计时和禁用 <textarea name="texta" id="" cols="30" rows="10& ...
- angular8 导出excel文件
angular package 1.xlsx npm install xlsx --save 2.file-saver npm install file-saver --save npm instal ...
- 腾讯云推出一站式 DevOps 解决方案 —— CODING DevOps
在产业互联网的大背景下,如何将人工智能.大数据等前沿技术与实体产业相结合,推动传统企业转型升级,已经成为每一个企业不得不思考的问题.落后的软件研发能力已经拖慢了中国大量企业的数字化转型进程. 为了满足 ...
- PCA主成分分析(最大投影方差)
PCA简介: 从n维数据中提取最能代表这组数据的m个向量,也就是对数据进行降维(n->m),提取特征. 目标: 找到一个向量\(\mu\),使n个点在其上的投影的方差最大(投影后的数据越不集中, ...
- WPF使用FlowDocument实现图文混排
代码: <RichTextBox CaretBrush="#fff" Background="Transparent" BorderThickness=& ...
- Unity 声音处理 之 语音识别
音量检测 检测当前麦克风的输入音量 using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...
- variable '' of type '' referenced from scope '', but it is not defined 异常解决方法
最近在做一个功能,通过拼接lamdba表达试来实现的功能,但测试时总是出现一个错误,如下图所示,网上也找不到答案,差点都放弃了.. 如上图图所示,我是想通过一个lamdba表达式(上图的IdField ...