MySql 分表帮助类代码实现

一、场景分析

       在我们使用过程中遇到一张表无法承载数据时,如容量大于2000w-3000w时,就需要用到数据表拆分技术 。下面是实现分表的一种帮助实现类,用于数据表水平拆分。

二、代码示例

package com.juanpi.mkt.utils;

import java.util.zip.CRC32;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 分表帮助类
 * @author: 杨杰
 */
public class TableModelHelper {
	/**
	 * 定义记录日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(TableModelHelper.class);

	/** mod分表 **/
	public static String TABLE_SPIT_MOD = "mod";
	/** hash分表 **/
	public static String TABLE_SPIT_HASH = "hash";
	/** crc32分表 **/
	public static String TABLE_SPIT_CRC32 = "crc32";
	/** mobile分表 **/
	public static String TABLE_SPIT_MOBILE = "mobile";

	public static int TABLE_NUM = 10;

	/**
	 * 获取表名
	 * 
	 * @param value
	 *            值
	 * @param tableName
	 *            表名
	 * @param tableRule
	 *            分表规则
	 * @param tablenum
	 *            表个数
	 * @return String 分表名
	 */
	public static String getTableName(String value, String tableName, String tableRule, int tablenum) {
		long suffix = 0L;
		if (tableRule.equals(TABLE_SPIT_MOD)) {
			suffix = Integer.parseInt(value) % 10 + 1;
		}
		
		if (tableRule.equals(TABLE_SPIT_HASH)) {
			/**Hash算法估计不是这样的**/
			suffix = value.hashCode() % (tablenum == 0 ? 10 : tablenum) + 1; 
		}
		
		if (tableRule.equals(TABLE_SPIT_CRC32)) {
			CRC32 crc = new CRC32();
			/**将字符串转大写**/
			String upperValue = value.toUpperCase();
			crc.update(upperValue.getBytes());
			suffix = crc.getValue() % (tablenum == 0 ? 10 : tablenum) + 1;
		}
		
		if (tableRule.equals(TABLE_SPIT_MOBILE)) {
			CRC32 crc = new CRC32();
			/**将字符串转大写**/
			String upperValue = value.toUpperCase();
			crc.update(upperValue.getBytes());			
			long crcValue = crc.getValue();
			long v1 = crcValue % 10;
			long v2 = (crcValue % 100)/10;
			long v3 = (crcValue % 1000)/100;
			long v4 = (crcValue % 10000)/1000;
			suffix = (v1 + v2 + v3 + v4)% 10 + 1;
		}
		
		logger.info("getTableName "+tableName + "_" + suffix);
		return tableName + "_" + suffix;
	}
	
	public static String getCouponUserTableName(String tableName,long uid){
		long suffix = (uid % 10) + 1;
		return tableName + "_" + suffix;
	}
	
	public static String getCouponCodeTableName(String value){
		long suffix = 0L;
		CRC32 crc = new CRC32();
		/**将字符串转大写**/
		String upperValue = value.toUpperCase();
		crc.update(upperValue.getBytes());
		long crcValue = crc.getValue();
		long v1 = crcValue % 10;
		long v2 = (crcValue % 100)/10;
		long v3 = (crcValue % 1000)/100;
		long v4 = (crcValue % 10000)/1000;
		
		suffix = (v1 + v2 + v3 + v4)% 10 + 1;
		return String.valueOf(suffix);
	}
	
	/**
	 * 取得用户分表
	 */
	public static String getCouponUserTableName(String field) {
		String tableName = "js_coupon_user";
		tableName = TableModelHelper.getTableName(field, tableName, TableModelHelper.TABLE_SPIT_MOD, TableModelHelper.TABLE_NUM);
		return tableName;
	}
}

 

转载于:https://my.oschina.net/ukapollo/blog/849703

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值