RaigorJiang commented on issue #15712:
URL: 
https://github.com/apache/shardingsphere/issues/15712#issuecomment-1073984494


   Hi @Swastyy 
   Here are some instructions and sample snippets:
   1. Startup class
      Reference to: 
`org.apache.shardingsphere.example.sharding.raw.jdbc.ShardingRawJavaConfigurationExample`
   2. DataSourceFactory
      Reference to: 
`org.apache.shardingsphere.example.sharding.raw.jdbc.factory.DataSourceFactory`
   3. DataSourceConfiguration
      Reference to: 
`org.apache.shardingsphere.example.sharding.raw.jdbc.config.ShardingDatabasesConfigurationPrecise`
   
   Application:
   - In ShardingDatabasesConfigurationPrecise:
   ```java
   private ShardingRuleConfiguration createShardingRuleConfiguration() {
           ShardingRuleConfiguration result = new ShardingRuleConfiguration();
           ...
           result.setDefaultDatabaseShardingStrategy(new 
StandardShardingStrategyConfiguration("user_id", "inline"));
           Properties props = new Properties();
           props.setProperty("algorithm-expression", "demo_ds_${user_id % 2}");
           result.getShardingAlgorithms().put("inline", new 
ShardingSphereAlgorithmConfiguration("INLINE", props));
           ...
           return result;
       }
   ```
   The `new ShardingSphereAlgorithmConfiguration` specifies the use of the 
`INLINE` algorithm, and passes in a property containing "algorithm-expression" 
as a parameter. In the algorithm map, the key is also `inline`.
   At the same time, a `StandardShardingStrategyConfiguration` is created in 
`defaultDatabaseShardingStrategy`, and `inline` (here is the key of map) is 
applied.
   
   
   - Custom Algorithm:
   
   Take 
`org.apache.shardingsphere.example.extension.classbased.sharding.raw.jdbc.fixture.ClassBasedStandardShardingAlgorithmFixture`
 as an example, it accepts a parameter "sharding-count" to calculate sharding 
count:
   ```java
   @Override
       public String doSharding(final Collection<String> availableTargetNames, 
final PreciseShardingValue<Integer> shardingValue) {
           for (String each : availableTargetNames) {
               if (each.endsWith(String.valueOf(shardingValue.getValue() % 
shardingCount))) {
                   return each;
               }
           }
           return null;
       }
   ```
   
   In the new `ShardingDatabasesConfigurationPrecise`, we can define:
   ````java
   private ShardingRuleConfiguration createShardingRuleConfiguration() {
           ShardingRuleConfiguration result = new ShardingRuleConfiguration();
           ...
           result.setDefaultDatabaseShardingStrategy(new 
StandardShardingStrategyConfiguration("user_id", "custom"));
           Properties props = new Properties();
           props.setProperty("strategy", "standard");
           props.setProperty("algorithmClassName", 
"org.apache.shardingsphere.example.extension.classbased.sharding.raw.jdbc.fixture.ClassBasedStandardShardingAlgorithmFixture");
           props.setProperty("sharding-count", "4");
           result.getShardingAlgorithms().put("custom", new 
ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
           ...
           return result;
       }
   
   ````
   
   illustrate:
   - Because `ClassBasedStandardShardingAlgorithmFixture` implements 
`StandardShardingAlgorithm`, the strategy in props is `standard`;
   - `algorithmClassName` is used to specify the custom algorithm to use;
   - `sharding-count` is a parameter required by the algorithm.
   
   The other parts are basically the same as the `sharding-raw-jdbc-example`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to