You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/11/23 06:51:48 UTC
[GitHub] [shardingsphere] DishJi commented on issue #8300: Configuration property name 'spring.shardingsphere.rules.sharding.sharding-algorithms.XXX.props' is not valid
DishJi commented on issue #8300:
URL: https://github.com/apache/shardingsphere/issues/8300#issuecomment-731963933
**sharding algorithm code**
`public class UserRecordShardingAlgorithm implements StandardShardingAlgorithm<Integer> {
private UserService userService = null;
private static final String REFER = "2019-01-02";
private boolean allowRangeQuery;
private Properties props = new Properties();
private boolean isAllowRangeQuery() {
return Boolean.parseBoolean(this.props.getOrDefault("allow-range-query-with-record-sharding", Boolean.FALSE.toString()).toString());
}
@Override
public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Integer> rangeShardingValue) {
if (this.allowRangeQuery) {
return collection;
} else {
throw new UnsupportedOperationException("Since the property of `allow-range-query-with-user_record-sharding` is false, inline sharding algorithm can not tackle with range query.");
}
}
@SneakyThrows
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue){
//collection:["t_order_1","t_order_2"],preciseShardingValue:{"logicTableName":"t_order","columnName":"order_id","value":396416249350848512}
//name为两张订单表 t_order_1 和 t_order_2
if(null == userService){
synchronized (UserRecordShardingAlgorithm.class){
if(null == userService){
userService = (UserService) SpringTools.getBeanByClass(UserServiceImpl.class);
}
}
}
String tableName = preciseShardingValue.getLogicTableName();
int userId = preciseShardingValue.getValue();
User user = userService.queryById(userId);
Date loginTime = user.getLogintime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//参考时间
Date referTime = sdf.parse(REFER);
if (loginTime.after(referTime)) {
Calendar loginCal = Calendar.getInstance();
//设置登陆时间
loginCal.setTime(loginTime);
//获取登陆年份
int year = loginCal.get(Calendar.YEAR);
StringBuilder sb = new StringBuilder(tableName);
sb.append("_");
sb.append(year);
return sb.toString();
}
return tableName;
}
@Override
public void init() {
this.allowRangeQuery = this.isAllowRangeQuery();
}
@Override
public String getType() {
return "UserRecordShardingAlgorithm";
}
@Override
public Properties getProps() {
return props;
}
@Override
public void setProps(Properties props) {
this.props = props;
}
}`
----------------------------------------------------------------
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:
users@infra.apache.org