• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
野猪佩奇ii
帮助1

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取模,分配到分表中

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgahffe
系列文章
更多 icon
同类精品
更多 icon
继续加载