tmjGitAccount commented on issue #3964: Hintmanager  databaseShardingOnly 
problem
URL: 
https://github.com/apache/incubator-shardingsphere/issues/3964#issuecomment-584464879
 
 
   > I have added, but I don't know your SQLs. I try my SQL, and it still is 
unicast routing.
   > 
   > So I need your SQLs and `OrderShardingAlgorithm`
   
   Here is my OrderShardingAlorithm.javaļ¼Œthe sql is just a very normal **select 
* from table**
   All I want is to route to different datasource by different header
   for example 
   1. if the httpheader  agency: a
     i want this sql be excuted in datasource dsa
   2. if the httpheader agency: b
    i want this sql be excuted in datasource dsb
   
   maybe could you please  provide me an example config for the purpose i want ?
   ```
   import lombok.NoArgsConstructor;
   import lombok.extern.slf4j.Slf4j;
   import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
   import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
   import org.springframework.stereotype.Component;
   import org.springframework.web.context.request.RequestAttributes;
   import org.springframework.web.context.request.RequestContextHolder;
   import org.springframework.web.context.request.ServletRequestAttributes;
   
   import javax.servlet.http.HttpServletRequest;
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.List;
   import java.util.Optional;
   import java.util.stream.Collectors;
   
   
   @Slf4j
   @NoArgsConstructor
   @Component("orderShardingAlgorithm")
   public class OrderShardingAlgorithm implements HintShardingAlgorithm<String> 
{
   
       @Override
       public List<String> doSharding(final Collection<String> 
availableTargetNames, final HintShardingValue<String> shardingValue) {
           RequestAttributes requestAttributes = 
RequestContextHolder.getRequestAttributes();
           HttpServletRequest request = ((ServletRequestAttributes) 
requestAttributes).getRequest();
           return Optional.ofNullable(request.getHeader("agency"))
                   .map(value -> availableTargetNames.stream().filter(v -> 
v.equals(value)).collect(Collectors.toList()))
                   .filter(value -> !value.isEmpty())
                   .orElse(new ArrayList<>(availableTargetNames));
   
       }
   }
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to