0%

实现创意轮播

实现创意轮播

广告投放时有时候需要进行创意轮播,系统将平分各创意展现量,便于广告主比较各创意投放效果.

使用redis

可以使用redis来实现这个需求。

创意是与投放单元绑定的,我们可以使用uid:deal来作为redis的key,那存储的内容是什么呢?由于要进行创意轮播,那就是存储创意id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 添加曝光
private void addExpose(String uid, String deal,long creativeId){
String key = uid+":"+deal;
jedis.lpush(key, String.valueOf(creativeId));
}

// 获取最后一次播放的创意
private Long getLastCreative(String uid, String deal) {
String key = uid+":"+deal;

// 取最后一条
String creativeId = jedis.lindex(key, 0);

if (creativeId == null || creativeId.isEmpty()) {
return null;
} else {
return Long.parseLong(creativeId);
}
}

此时假设该投放单元有三条创意,id分别为100,101,102

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
RedisController redisController = new RedisController();
// 模拟三条创意
List<Long> creativeIds = new ArrayList<>();
creativeIds.add(100L);
creativeIds.add(101L);
creativeIds.add(102L);

Long lastCreative = redisController.getLastCreative("uid", "dealId");
System.out.println(lastCreative);
if(lastCreative == null){ // 第一次曝光
Long crativeId = creativeIds.get(0);
// 曝光操作 doSomething
redisController.addExpose("uid","dealId",crativeId);
} else { // 不是第一次曝光
int lastIndex = 0;
for(int i = 0;i<creativeIds.size();i++){
if(creativeIds.get(i).equals(lastCreative)){
lastIndex = i;
break;
}
}
int index = (lastIndex + 1) % creativeIds.size();
Long crativeId = creativeIds.get(index);
// 曝光操作 doSomething
redisController.addExpose("uid","dealId",crativeId);
}

使用hbase

由于广告投放中的数据量是很大的,存在redis中内存扛不住,所以后来使用了hbase来进行实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 从Hbase中获取到指定deal的最后一次创意
*
* @param uid
* @param deal
* @return
*/
public Long getLastCreative(String uid, String deal) {
if (StringUtils.isBlank(uid) || StringUtils.isBlank(deal)) {
return null;
}

Table htable = null;
try {
// 对应的表
htable = connection.getTable(TableName.valueOf("lastExpose"));
// 用户id作为rowkey
Get get = new Get(Bytes.toBytes(uid));
// 列族 列名
get.addColumn(Bytes.toBytes("f"),Bytes.toBytes(deal));

Result result = htable.get(get);
if (result != null && !result.isEmpty()) {
// 内容
byte[] value = result.getValue(Bytes.toBytes("f"),Bytes.toBytes(deal));

String s = new String(value);
Gson gson = new Gson();
// 最后一次曝光的创意
Long creative = gson.fromJson(s, Long.class);
return creative;
}
} catch (IOException e) {
LOGGER.error("get camp list failed: {}", uid);
}finally {
if (htable != null) {
try {
htable.close();
} catch (Exception closeExcption) {
LOGGER.error("close connection failed: ", closeExcption);
}
}
}
return null;
}

添加最后一次曝光创意

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 添加最后一次曝光创意到Hbase
*
* @param uid
* @param deal
* @param creative
*/
public void addExposeInHbase(String uid, String deal,long creative) {

Table htable = null;
try {
htable = connection.getTable(TableName.valueOf("lastExpose"));
Gson gson = new Gson();
Put put = new Put(Bytes.toBytes(uid));
put.addColumn(Bytes.toBytes("f"), Bytes.toBytes(deal), Bytes.toBytes(creative);
//增加过期时间 90天
put.setTTL(3600 * 24 * 90 * 1000L);
htable.put(put);
} catch (IOException e) {
LOGGER.error("write to hbase failed: {}", uid + ":" + e.getMessage());
} finally {
if (htable != null) {
try {
htable.close();
} catch (Exception closeExcption) {
LOGGER.error("close connection failed: ", closeExcption);
}
}
}
}

使用的时候与redis的操作一样

欢迎关注我的其它发布渠道