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.
rules:
- !SHARDING
tables: # Sharding table configuration
<logic_table_name> (+): # Logic table name
actualDataNodes (?): # Describe data source names and actual tables (refer to Inline syntax rules)
databaseStrategy (?): # Databases sharding strategy, use default databases sharding strategy if absent. sharding strategy below can choose only one.
standard: # For single sharding column scenario
shardingColumn: # Sharding column name
shardingAlgorithmName: # Sharding algorithm name
complex: # For multiple sharding columns scenario
shardingColumns: # Sharding column names, multiple columns separated with comma
shardingAlgorithmName: # Sharding algorithm name
hint: # Sharding by hint
shardingAlgorithmName: # Sharding algorithm name
none: # Do not sharding
tableStrategy: # Tables sharding strategy, same as database sharding strategy
auditStrategy: # Sharding audit strategy
auditorNames: # Sharding auditor name
- <auditor_name>
- <auditor_name>
allowHintDisable: true # Enable or disable sharding audit hint
autoTables: # Auto Sharding table configuration
t_order_auto: # Logic table name
actualDataSources (?): # Data source names
shardingStrategy: # Sharding strategy
standard: # For single sharding column scenario
shardingColumn: # Sharding column name
shardingAlgorithmName: # Auto sharding algorithm name
bindingTables (+): # Binding tables
- <logic_table_name_1, logic_table_name_2, ...>
- <logic_table_name_1, logic_table_name_2, ...>
defaultDatabaseStrategy: # Default strategy for database sharding
defaultTableStrategy: # Default strategy for table sharding
defaultKeyGenerateStrategy: # Default Key generator strategy
defaultShardingColumn: # Default sharding column name
keyGenerateStrategies:
<key_generate_strategy_name> (+): # Sharding key generate strategy name
keyGenerateType: # Sharding key generate strategy type
keyGeneratorName: # Key generate algorithm name
logicTable: # Logic table name, required when keyGenerateType is column
keyGenerateColumn: # Column name of key generate, required when keyGenerateType is column
keyGenerateSequence: # Sequence name, required when keyGenerateType is sequence
# Sharding algorithm configuration
shardingAlgorithms:
<sharding_algorithm_name> (+): # Sharding algorithm name
type: # Sharding algorithm type
props: # Sharding algorithm properties
# ...
# Key generate algorithm configuration
keyGenerators:
<key_generate_algorithm_name> (+): # Key generate algorithm name
type: # Key generate algorithm type
props: # Key generate algorithm properties
# ...
# Sharding audit algorithm configuration
auditors:
<sharding_audit_algorithm_name> (+): # Sharding audit algorithm name
type: # Sharding audit algorithm type
props: # Sharding audit algorithm properties
# ...
- !BROADCAST
tables: # Broadcast tables
- <table_name>
- <table_name>
The YAML configuration sample of data sharding is as follows:
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
standardJdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
standardJdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_inline
auditStrategy:
auditorNames:
- sharding_key_required_auditor
allowHintDisable: true
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_inline
t_account:
actualDataNodes: ds_${0..1}.t_account_${0..1}
tableStrategy:
standard:
shardingAlgorithmName: t_account_inline
defaultShardingColumn: account_id
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:
keyGenerateStrategies:
t_order_order_id:
keyGenerateType: column
keyGeneratorName: snowflake
logicTable: t_order
keyGenerateColumn: order_id
t_order_item_order_item_id:
keyGenerateType: column
keyGeneratorName: snowflake
logicTable: t_order_item
keyGenerateColumn: order_item_id
t_account_account_id:
keyGenerateType: column
keyGeneratorName: snowflake
logicTable: t_account
keyGenerateColumn: account_id
shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
t_order_inline:
type: INLINE
props:
algorithm-expression: t_order_${order_id % 2}
t_order_item_inline:
type: INLINE
props:
algorithm-expression: t_order_item_${order_id % 2}
t_account_inline:
type: INLINE
props:
algorithm-expression: t_account_${account_id % 2}
keyGenerators:
snowflake:
type: SNOWFLAKE
auditors:
sharding_key_required_auditor:
type: DML_SHARDING_CONDITIONS
- !BROADCAST
tables: # Broadcast tables
- t_address
props:
sql-show: false
Read the YAML configuration to create a data source according to the createDataSource method of YamlShardingSphereDataSourceFactory.
YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml"));
