Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use RangeShardingAlgorithm with PreciseShardingAlgorithm #1052

Closed
naah69 opened this issue Jul 26, 2018 · 3 comments
Closed

how to use RangeShardingAlgorithm with PreciseShardingAlgorithm #1052

naah69 opened this issue Jul 26, 2018 · 3 comments
Assignees

Comments

@naah69
Copy link

naah69 commented Jul 26, 2018

I use sharding-jdbc-spring-boot-starter 3.0.0.M1
i can't find relevent document on the website.
i try some times

but i can't enter implementation Class of RangeShardingAlgorithm.it will enter implementation Class of PreciseShardingAlgorithm.

please tell me how to use it,or give me a demo
this is my config and my code;

yml

config:
sharding:
default-database-strategy:
standard:
range-algorithm-class-name: shardingsphere.demo.strategy.NaahRangeShardingAlgorithm
precise-algorithm-class-name: shardingsphere.demo.strategy.HashCodePreciseShardingAlgorithm
sharding-column: user_id

RangeShardingAlgorithm

public class NaahRangeShardingAlgorithm implements RangeShardingAlgorithm {

private final Logger logger = LoggerFactory.getLogger(NaahRangeShardingAlgorithm.class);

public NaahRangeShardingAlgorithm() {
}


@Override
public Collection<String> doSharding(Collection availableTargetNames, RangeShardingValue shardingValue) {
    logger.info("Range availableTargetNames:" + JSON.toJSONString(availableTargetNames) + ",shardingValue:" + JSON.toJSONString(shardingValue));
    return null;
}

}

PreciseShardingAlgorithm

public class HashCodePreciseShardingAlgorithm implements PreciseShardingAlgorithm {

private final Logger logger = LoggerFactory.getLogger(HashCodePreciseShardingAlgorithm.class);

@Override
public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
    logger.info("availableTargetNames:" + JSON.toJSONString(availableTargetNames) + ",preciseShardingValue:" + JSON.toJSONString(shardingValue));
    //通过hashcode取模
    int num= (shardingValue.getValue()+"").hashCode()%availableTargetNames.size();
    String[] list = availableTargetNames.toArray(new String[availableTargetNames.size()]);
    if(num<=availableTargetNames.size()){
        return list[num];
    }
    return null;
}

}

@tuohai666
Copy link
Member

I'll add an example for RangeShardingAlgorithm later.

@naah69
Copy link
Author

naah69 commented Jul 30, 2018

i had solved that problem.
but i have a new problem.
Data sharding and Read-write splitting how to use together.
i find few document and demo online.i will upload my demo after that i study it.
can u Add me Wechat?
WeChat:278567949

@terrymanu
Copy link
Member

add example via apache/shardingsphere-example#69

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants