sharding-proxy利用字符串hash取模的方式分表

sharding-proxy利用字符串hash取模的方式分表

如果是数值类型的字段,可以直接取模来分表,如果是字符串,往往需要先求出hash值再取模

修改 config-sharding.yaml

schemaName: activity

dataSources:
  ds_1:
    url: jdbc:mysql://192.168.0.1:3306/activity?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
  tables:
    plat_activity_product_detail:
      actualDataNodes: ds_${1}.plat_activity_product_detail_${0..199}
      tableStrategy:
        standard:
          shardingColumn: activity_id
          shardingAlgorithmName: plat_activity_product_mod
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake

  shardingAlgorithms:
    plat_activity_product_mod:
      type: HASH_MOD
      props:
        sharding-count: '200'
  
  keyGenerators:
    snowflake:
      type: SNOWFLAKE
      props:
        worker-id: 123

配置一个模式命名为:plat_activity_product_mod,类型指定为hash取模:HASH_MOD,props:sharding-count: '200’意为分200张表。

tableStrategy:shardingAlgorithmName下配置自定义的模式名plat_activity_product_mod

此时sharding会按照activity_id字段hash取模,分配到分表中

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值