All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wicp.tams.common.redis.plugin.DumperRedis Maven / Gradle / Ivy

/*
 * **********************************************************************
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称:common
 * 项目描述:公共的工具集
 * 版权说明:本软件属andy.zhou([email protected])所有。
 * ***********************************************************************
 */
package net.wicp.tams.common.redis.plugin;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.tuple.Pair;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.binlog.alone.DuckulaAssit;
import net.wicp.tams.common.binlog.alone.binlog.bean.Rule;
import net.wicp.tams.common.binlog.alone.binlog.bean.RuleItem;
import net.wicp.tams.common.binlog.alone.constant.ExportColType;
import net.wicp.tams.common.binlog.alone.dump.bean.Dump;
import net.wicp.tams.common.binlog.alone.dump.bean.DumpEvent;
import net.wicp.tams.common.binlog.alone.dump.listener.IBusiSender;
import net.wicp.tams.common.constant.DrdsPattern;
import net.wicp.tams.common.constant.FieldFormart;
import net.wicp.tams.common.constant.ods.AddColName;
import net.wicp.tams.common.redis.RedisAssit;
import net.wicp.tams.common.redis.pool.AbsPool;
import redis.clients.jedis.Jedis;

@Slf4j
public class DumperRedis implements IBusiSender {
//	private final AbsPool absPool;
	private final Map keyFormateMap = new HashMap();
	private final Map poolNameMap = new HashMap();// redis的池名称
	private final FieldFormart fieldFormart;
	private final ExportColType exportColType;

	public DumperRedis() {
//		this.absPool = RedisAssit.getPool("default");
		this.fieldFormart = Conf.getEnum(FieldFormart.class, "common.binlog.alone.global.fieldFormart");
		this.exportColType = Conf.getEnum(ExportColType.class, "common.binlog.alone.filter.exportColType");// 先写死这个
	}

	@Override
	public void init(Dump dump) {
		String keyformate = PluginCommon.keyformate(
				StringUtil.trimSpace(dump.getRule().getItems().get(RuleItem.keyformart)), dump.getRule().getDrds());
		this.keyFormateMap.put(dump.getRule(), keyformate);
		//
		String initPoolConf = PluginCommon.initPoolConf(dump.getRule());
		this.poolNameMap.put(dump.getRule(), initPoolConf);	
	}

	@Override
	public void doSend(DumpEvent dumpEvent, Map addValues, String newDb, String newTb,
			boolean isSplit) {
		AbsPool pool = RedisAssit.getPool(this.poolNameMap.get(dumpEvent.getDump().getRule()));
		Jedis jedis = pool.getResource();
		try {
			Pair controlKey = PluginCommon.getControl(newDb, newTb,
					dumpEvent.getDump().getRule().getDrds(), keyFormateMap.get(dumpEvent.getDump().getRule()));
			for (Map data : dumpEvent.getDatas()) {
				if (MapUtils.isNotEmpty(addValues)) {
					Map addColValuesStr = DuckulaAssit.getAddColValuesStr(addValues, this.fieldFormart);
					data.putAll(addColValuesStr);
				}
				String keyTableJoin = dumpEvent.findKey(data);
				// Pair oriDbtb = dumpEvent.getOriDbtb();
				String keyRedis;
				if (dumpEvent.getDump().getRule().getDrds() == DrdsPattern.no) {
					keyRedis = String.format(keyFormateMap.get(dumpEvent.getDump().getRule()), newDb, newTb,
							keyTableJoin);
				} else {
					String routeValue = data.get(dumpEvent.getDump().getRule().getRuleItem(RuleItem.routeColName));
					if (StringUtil.isNull(routeValue)) {
						log.error("路由字段【{}】为空值,请检查routeColName是否正确,规则,db:【{}】,tb:【{}】",
								dumpEvent.getDump().getRule().getRuleItem(RuleItem.routeColName),
								dumpEvent.getDump().getRule().getDbPattern(),
								dumpEvent.getDump().getRule().getTbPattern());
					}
					keyRedis = String.format(keyFormateMap.get(dumpEvent.getDump().getRule()), newDb, newTb, routeValue,
							keyTableJoin);
				}
				// 过滤主键和路由键,节省空间
				exportColType.filter(data, dumpEvent.getDump().getRule().getRuleItem(RuleItem.routeColName),
						dumpEvent.getDump().getPrimarys());
				jedis.hmset(keyRedis, data);
				jedis.sadd(controlKey.getLeft(), keyRedis);// 添加元素
//				Map msg = PluginCommon.packChangeEvent(oriDbtb.getLeft(), oriDbtb.getRight(),
//						keyTableJoin, keyformart, net.wicp.tams.common.constant.OptType.insert);
				// 全量不需要推到流中,否则会重复计算
//				this.absPool.putStream(jedis, controlKey.getRight(), msg);
			}
		} finally {
			pool.returnResource(jedis);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy