keliubo commented on issue #21205:
URL: 
https://github.com/apache/shardingsphere/issues/21205#issuecomment-1261613985

       rules:
         sharding:
           tables:
             #逻辑表
             course:
               #m1表示数据源 course表示表名
               actual-data-nodes: m$->{1..2}.course_$->{1..2}
               #库的分片策略
               database-strategy:
                 hint:
                   sharding-algorithm-name: t-db-hint
                     # 
com.example.shardingspheredemo.algorithm.db.hint.MyHintDbShardingAlgorithm
               #表分片策略
               table-strategy:
                 hint:
                   sharding-algorithm-name: t-table-hint
                   
#com.example.shardingspheredemo.algorithm.table.hint.MyHintTableShardingAlgorithm
           key-generators:
             snowflake:
               type: SNOWFLAKE
               props:
                 worker-id: 1
           sharding-algorithms:
             t-db-hint:
               type: HINT_DB_TEST
             t-table-hint:
               type: HINT_TABLE_TEST
   
   @Getter
   public class MyHintDbShardingAlgorithm implements 
HintShardingAlgorithm<Long> {
   
       private Properties props;
   
       @Override
       public Collection<String> doSharding(Collection<String> 
availableTargetNames, HintShardingValue<Long> shardingValue) {
           String key = shardingValue.getLogicTableName() + 
shardingValue.getValues().toArray()[0];
           if(availableTargetNames.contains(key)){
               return Arrays.asList(key);
           }
           throw new UnsupportedOperationException("this key " + key + " not 
support");
       }
   
       @Override
       public Properties getProps() {
           return null;
       }
   
       @Override
       public void init(Properties properties) {
           this.props = props;
       }
   
       @Override
       public String getType() {
           return "HINT_DB_TEST";
       }
   }
   
   
   @Test
       public void queryCourseByHint(){
           HintManager hintManager = HintManager.getInstance();
           hintManager.addDatabaseShardingValue("m","1");
           hintManager.addTableShardingValue("course","1");
           List<Course> courses = courseMapper.selectList(null);
           System.out.println(courses);
       }
   **The result is to go back and look in both M1 and M2**


-- 
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