ShardingSphereDataSource 5 .0 取SNOWFLAKE主键值 问题

之前用4.1的版本写了个取雪花主键的方法,但是现在升级到5.0后报错了,要怎么引用方法呢?

可以通过这个方法获取 ShardingRule

Optional<ShardingRule> rule = ((ShardingSphereDataSource) dataSource).getContextManager()
                .getMetaDataContexts().getMetaData("").getRuleMetaData().getRules().stream()
                .filter(each -> each instanceof ShardingRule).map(each -> (ShardingRule) each).findFirst();

那如果我想针对某表的去主键呢?

ShardingRule

    /**
     * Find the Generated key of logic table.
     *
     * @param logicTableName logic table name
     * @return generated key
     */
    public Comparable<?> generateKey(final String logicTableName) {
        Optional<TableRule> tableRule = findTableRule(logicTableName);
        if (!tableRule.isPresent()) {
            throw new ShardingSphereConfigurationException("Cannot find strategy for generate keys.");
        }
        KeyGenerateAlgorithm keyGenerator = null != tableRule.get().getKeyGeneratorName() ? keyGenerators.get(tableRule.get().getKeyGeneratorName()) : defaultKeyGenerateAlgorithm;
        return keyGenerator.generateKey();
    }
    
2 个赞

感谢 大佬!!!!

大佬,问一下,现在5.1.0之后,API变化很大
怎么样才能取到
这是我原来5.0.1的代码

Optional<ShardingRule> rule = dataSourceManager.getDataSource(importerConfig.getDataSourceConfig()).unwrap(ShardingSphereDataSource.class).getContextManager()
                .getMetaDataContexts().getMetaData(connection.getSchema()).getRuleMetaData().getRules().stream()
                .filter(each -> each instanceof ShardingRule).map(each -> (ShardingRule) each).findFirst();

我尝试直接引入原来的包,会提示循环依赖:

[ERROR] [ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='org.apache.shardingsphere:shardingsphere-sharding-core:5.1.1-SNAPSHOT'}' and 'Vertex{label='org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT'}' introduces to cycle in the graph org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT --> org.apache.shardingsphere:shardingsphere-sharding-core:5.1.1-SNAPSHOT --> org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT @ 
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='org.apache.shardingsphere:shardingsphere-sharding-core:5.1.1-SNAPSHOT'}' and 'Vertex{label='org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT'}' introduces to cycle in the graph org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT --> org.apache.shardingsphere:shardingsphere-sharding-core:5.1.1-SNAPSHOT --> org.apache.shardingsphere:shardingsphere-data-pipeline-core:5.1.1-SNAPSHOT -> [Help 1]
[ERROR] 

@tuichenchuxin 可以帮忙看下怎么解决吗

看不出来问题,能不能提供个demo 啥的

@tuichenchuxin
嗯嗯,我说明一下
我想在执行scaling的时候,自动填充一个主键ID
所以我是在org/apache/shardingsphere/data/pipeline/core/importer/AbstractImporter.java
这个文件下(shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/pom.xml)引入了上面的代码
因此就需要引入下面这两个包:

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-sharding-core</artifactId>
            <version>5.1.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.1.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

结果就会提示:

java: Annotation processing is not supported for module cycles. 
Please ensure that all modules from cycle [shardingsphere-sharding-distsql-handler,shardingsphere-sharding-core,shardingsphere-jdbc-core,shardingsphere-data-pipeline-core] are excluded from annotation processing

之前在5.0.1的版本是可以使用的
所以在5.1.0li如果我想实现在shardingsphere-data-pipeline-core里获取分片策略应该怎么做

代码demo: https://github.com/imchao9/shardingsphere/tree/feature/generate-distributed-key-id-when-scaling-to-new-database-build-failed

谢谢大佬的帮助!

京ICP备2021015875号