Sharding

Background

Data sharding YAML configuration is highly readable. The dependencies between sharding rules can be quickly understood through the YAML format. ShardingSphere automatically creates the ShardingSphereDataSource object according to YAML configuration, which can reduce unnecessary coding for users.

Parameters

  1. rules:
  2. - !SHARDING
  3. tables: # Sharding table configuration
  4. <logic_table_name> (+): # Logic table name
  5. actualDataNodes (?): # Describe data source names and actual tables (refer to Inline syntax rules)
  6. databaseStrategy (?): # Databases sharding strategy, use default databases sharding strategy if absent. sharding strategy below can choose only one.
  7. standard: # For single sharding column scenario
  8. shardingColumn: # Sharding column name
  9. shardingAlgorithmName: # Sharding algorithm name
  10. complex: # For multiple sharding columns scenario
  11. shardingColumns: # Sharding column names, multiple columns separated with comma
  12. shardingAlgorithmName: # Sharding algorithm name
  13. hint: # Sharding by hint
  14. shardingAlgorithmName: # Sharding algorithm name
  15. none: # Do not sharding
  16. tableStrategy: # Tables sharding strategy, same as database sharding strategy
  17. keyGenerateStrategy: # Key generator strategy
  18. column: # Column name of key generator
  19. keyGeneratorName: # Key generator name
  20. auditStrategy: # Sharding audit strategy
  21. auditorNames: # Sharding auditor name
  22. - <auditor_name>
  23. - <auditor_name>
  24. allowHintDisable: true # Enable or disable sharding audit hint
  25. autoTables: # Auto Sharding table configuration
  26. t_order_auto: # Logic table name
  27. actualDataSources (?): # Data source names
  28. shardingStrategy: # Sharding strategy
  29. standard: # For single sharding column scenario
  30. shardingColumn: # Sharding column name
  31. shardingAlgorithmName: # Auto sharding algorithm name
  32. bindingTables (+): # Binding tables
  33. - <logic_table_name_1, logic_table_name_2, ...>
  34. - <logic_table_name_1, logic_table_name_2, ...>
  35. defaultDatabaseStrategy: # Default strategy for database sharding
  36. defaultTableStrategy: # Default strategy for table sharding
  37. defaultKeyGenerateStrategy: # Default Key generator strategy
  38. defaultShardingColumn: # Default sharding column name
  39. # Sharding algorithm configuration
  40. shardingAlgorithms:
  41. <sharding_algorithm_name> (+): # Sharding algorithm name
  42. type: # Sharding algorithm type
  43. props: # Sharding algorithm properties
  44. # ...
  45. # Key generate algorithm configuration
  46. keyGenerators:
  47. <key_generate_algorithm_name> (+): # Key generate algorithm name
  48. type: # Key generate algorithm type
  49. props: # Key generate algorithm properties
  50. # ...
  51. # Sharding audit algorithm configuration
  52. auditors:
  53. <sharding_audit_algorithm_name> (+): # Sharding audit algorithm name
  54. type: # Sharding audit algorithm type
  55. props: # Sharding audit algorithm properties
  56. # ...
  57. - !BROADCAST
  58. tables: # Broadcast tables
  59. - <table_name>
  60. - <table_name>

Procedure

  1. Configure data sharding rules in YAML files, including data source, sharding rules, and global attributes and other configuration items.
  2. Call createDataSource method of the object YamlShardingSphereDataSourceFactory. Create ShardingSphereDataSource according to the configuration information in YAML files.

Sample

The YAML configuration sample of data sharding is as follows:

  1. dataSources:
  2. ds_0:
  3. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  4. driverClassName: com.mysql.jdbc.Driver
  5. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  6. username: root
  7. password:
  8. ds_1:
  9. dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  10. driverClassName: com.mysql.jdbc.Driver
  11. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  12. username: root
  13. password:
  14. rules:
  15. - !SHARDING
  16. tables:
  17. t_order:
  18. actualDataNodes: ds_${0..1}.t_order_${0..1}
  19. tableStrategy:
  20. standard:
  21. shardingColumn: order_id
  22. shardingAlgorithmName: t_order_inline
  23. keyGenerateStrategy:
  24. column: order_id
  25. keyGeneratorName: snowflake
  26. auditStrategy:
  27. auditorNames:
  28. - sharding_key_required_auditor
  29. allowHintDisable: true
  30. t_order_item:
  31. actualDataNodes: ds_${0..1}.t_order_item_${0..1}
  32. tableStrategy:
  33. standard:
  34. shardingColumn: order_id
  35. shardingAlgorithmName: t_order_item_inline
  36. keyGenerateStrategy:
  37. column: order_item_id
  38. keyGeneratorName: snowflake
  39. t_account:
  40. actualDataNodes: ds_${0..1}.t_account_${0..1}
  41. tableStrategy:
  42. standard:
  43. shardingAlgorithmName: t_account_inline
  44. keyGenerateStrategy:
  45. column: account_id
  46. keyGeneratorName: snowflake
  47. defaultShardingColumn: account_id
  48. bindingTables:
  49. - t_order,t_order_item
  50. defaultDatabaseStrategy:
  51. standard:
  52. shardingColumn: user_id
  53. shardingAlgorithmName: database_inline
  54. defaultTableStrategy:
  55. none:
  56. shardingAlgorithms:
  57. database_inline:
  58. type: INLINE
  59. props:
  60. algorithm-expression: ds_${user_id % 2}
  61. t_order_inline:
  62. type: INLINE
  63. props:
  64. algorithm-expression: t_order_${order_id % 2}
  65. t_order_item_inline:
  66. type: INLINE
  67. props:
  68. algorithm-expression: t_order_item_${order_id % 2}
  69. t_account_inline:
  70. type: INLINE
  71. props:
  72. algorithm-expression: t_account_${account_id % 2}
  73. keyGenerators:
  74. snowflake:
  75. type: SNOWFLAKE
  76. auditors:
  77. sharding_key_required_auditor:
  78. type: DML_SHARDING_CONDITIONS
  79. - !BROADCAST
  80. tables: # Broadcast tables
  81. - t_address
  82. props:
  83. sql-show: false

Read the YAML configuration to create a data source according to the createDataSource method of YamlShardingSphereDataSourceFactory.

  1. YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml"));