Project: ShardingSphere (App Definition and Development / Database)
File: sharding.en.md

+++ title = "Sharding" weight = 1 +++ 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. ```yaml rules: !SHARDING tables: # Sharding table configuration <logictablename> (+): # 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 keyGenerateStrategy: # Key generator strategy column: # Column name of key generator keyGeneratorName: # Key generator name 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 torderauto: # 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 <logictablename1, logictablename2, ...> <logictablename1, logictablename2, ...> defaultDatabaseStrategy: # Default strategy for database sharding defaultTableStrategy: # Default strategy for table sharding defaultKeyGenerateStrategy: # Default Key generator strategy defaultShardingColumn: # Default sharding column name shardingAlgorithms: <shardingalgorithmname> (+): # Sharding algorithm name type: # Sharding algorithm type props: # Sharding algorithm properties keyGenerators: <keygeneratealgorithm_name> (+): # Key generate algorithm name type: # Key generate algorithm type props: # Key generate algorithm properties auditors: <shardingauditalgorithm_name> (+): # Sharding audit algorithm name type: # Sharding audit algorithm type props: # Sharding audit algorithm properties !BROADCAST tables: # Broadcast tables <table_name> <table_name> ``` Configure data sharding rules in YAML files, including data source, sharding rules, and global attributes and other configuration items. Call createDataSource method of the object YamlShardingSphereDataSourceFactory. Create ShardingSphereDataSource according to the configuration information in YAML files. The YAML configuration sample of data sharding is as follows: ```yaml dataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demods0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource driverClassName: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/demods1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: rules: !SHARDING tables: t_order: actualDataNodes: ds${0..1}.torder_${0..1} tableStrategy: standard: shardingColumn: order_id shardingAlgorithmName: torderinline keyGenerateStrategy: column: order_id keyGeneratorName: snowflake auditStrategy: auditorNames: shardingkeyrequired_auditor allowHintDisable: true torderitem: actualDataNodes: ds${0..1}.torderitem${0..1} tableStrategy: standard: shardingColumn: order_id shardingAlgorithmName: torderitem_inline keyGenerateStrategy: column: orderitemid keyGeneratorName: snowflake t_account: actualDataNodes: ds${0..1}.taccount_${0..1} tableStrategy: standard: shardingAlgorithmName: taccountinline keyGenerateStrategy: column: account_id keyGeneratorName: snowflake defaultShardingColumn: account_id bindingTables: torder,torder_item defaultDatabaseStrategy: standard: shardingColumn: user_id shardingAlgorithmName: database_inline defaultTableStrategy: none: shardingAlgorithms: database_inline: type: INLINE props: algorithm-expression: ds${userid % 2} torderinline: type: INLINE props: algorithm-expression: torder${order_id % 2} torderitem_inline: type: INLINE props: algorithm-expression: torderitem${orderid % 2} taccountinline: type: INLINE props: algorithm-expression: taccount${account_id % 2} keyGenerators: snowflake: type: SNOWFLAKE auditors: shardingkeyrequired_auditor: type: DMLSHARDINGCONDITIONS !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. ```java YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases-tables.yaml")); ```