Sharding

Background

The Java API rule configuration for data sharding, which allows users to create ShardingSphereDataSource objects directly by writing Java code, is flexible enough to integrate various types of business systems without relying on additional jar packages.

Parameters

Root Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
tables (+)Collection<ShardingTableRuleConfiguration>Sharding table rules-
autoTables (+)Collection<ShardingAutoTableRuleConfiguration>Sharding auto table rules-
bindingTableGroups (*)Collection<String>Binding table rulesEmpty
defaultDatabaseShardingStrategy (?)ShardingStrategyConfigurationDefault database sharding strategyNot sharding
defaultTableShardingStrategy (?)ShardingStrategyConfigurationDefault table sharding strategyNot sharding
defaultKeyGenerateStrategy (?)KeyGeneratorConfigurationDefault key generatorSnowflake
defaultAuditStrategy (?)ShardingAuditStrategyConfigurationDefault key auditorDML_SHARDING_CONDITIONS
defaultShardingColumn (?)StringDefault sharding column nameNone
shardingAlgorithms (+)Map<String, AlgorithmConfiguration>Sharding algorithm name and configurationsNone
keyGenerators (?)Map<String, AlgorithmConfiguration>Key generate algorithm name and configurationsNone
auditors (?)Map<String, AlgorithmConfiguration>Sharding audit algorithm name and configurationsNone

Sharding Table Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
logicTableStringName of sharding logic table-
actualDataNodes (?)StringDescribe data source names and actual tables, delimiter as point.
Multiple data nodes split by comma, support inline expression
Broadcast table or databases sharding only
databaseShardingStrategy (?)ShardingStrategyConfigurationDatabases sharding strategyUse default databases sharding strategy
tableShardingStrategy (?)ShardingStrategyConfigurationTables sharding strategyUse default tables sharding strategy
keyGenerateStrategy (?)KeyGeneratorConfigurationKey generator configurationUse default key generator
auditStrategy (?)ShardingAuditStrategyConfigurationSharding audit strategy configurationUse default auditor

Sharding Auto Table Configuration

Class name: org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration

Attributes:

NameDataTypeDescriptionDefault Value
logicTableStringName of sharding logic table-
actualDataSources (?)StringData source names.
Multiple data nodes split by comma
Use all configured data sources
shardingStrategy (?)ShardingStrategyConfigurationSharding strategyUse default sharding strategy
keyGenerateStrategy (?)KeyGeneratorConfigurationKey generator configurationUse default key generator

Sharding Strategy Configuration

Standard Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingColumnStringSharding column name
shardingAlgorithmNameStringSharding algorithm name

Complex Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingColumnsStringSharding column name, separated by commas
shardingAlgorithmNameStringSharding algorithm name

Hint Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration

Attributes:

NameDataTypeDescription
shardingAlgorithmNameStringSharding algorithm name

None Sharding Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration

Attributes: None

Please refer to Built-in Sharding Algorithm List for more details about type of algorithm.

Distributed Key Strategy Configuration

Class name: org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration

Attributes:

NameDataTypeDescription
columnStringColumn name of key generate
keyGeneratorNameStringkey generate algorithm name

Please refer to Built-in Key Generate Algorithm List for more details about type of algorithm.

Sharding audit Strategy Configuration

Class name:org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration

Attributes:

NameDataTypeDescription
auditorNamesCollection<String>Sharding audit algorithm name
allowHintDisableBooleanEnable or disable sharding audit hint

Please refer to Built-in Sharding Audit Algorithm List for more details about type of algorithm.

Procedure

  1. Create an authentic data source mapping relationship, with key as the logical name of the data source and value as the DataSource object.
  2. Create the sharding rule object ShardingRuleConfiguration, and initialize the sharding table objects—ShardingTableRuleConfiguration, the set of bound tables, the set of broadcast tables, and parameters like library sharding strategy and the database sharding strategy, on which the data sharding depends.
  3. Using the ShardingSphereDataSource method of calling the ShardingSphereDataSourceFactory subject to create the ShardingSphereDataSource.

Sample

  1. public final class ShardingDatabasesAndTablesConfigurationPrecise {
  2. @Override
  3. public DataSource getDataSource() throws SQLException {
  4. return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Arrays.asList(createShardingRuleConfiguration(), createBroadcastRuleConfiguration())), new Properties());
  5. }
  6. private ShardingRuleConfiguration createShardingRuleConfiguration() {
  7. ShardingRuleConfiguration result = new ShardingRuleConfiguration();
  8. result.getTables().add(getOrderTableRuleConfiguration());
  9. result.getTables().add(getOrderItemTableRuleConfiguration());
  10. result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item"));
  11. result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "inline"));
  12. result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "standard_test_tbl"));
  13. Properties props = new Properties();
  14. props.setProperty("algorithm-expression", "demo_ds_${user_id % 2}");
  15. result.getShardingAlgorithms().put("inline", new AlgorithmConfiguration("INLINE", props));
  16. result.getShardingAlgorithms().put("standard_test_tbl", new AlgorithmConfiguration("STANDARD_TEST_TBL", new Properties()));
  17. result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
  18. result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
  19. return result;
  20. }
  21. private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
  22. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
  23. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
  24. result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true));
  25. return result;
  26. }
  27. private ShardingTableRuleConfiguration getOrderItemTableRuleConfiguration() {
  28. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
  29. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_item_id", "snowflake"));
  30. return result;
  31. }
  32. private Map<String, DataSource> createDataSourceMap() {
  33. Map<String, DataSource> result = new HashMap<>();
  34. result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
  35. result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
  36. return result;
  37. }
  38. private BroadcastRuleConfiguration createBroadcastRuleConfiguration() {
  39. return new BroadcastRuleConfiguration(Collections.singletonList("t_address"));;
  40. }
  41. }