liruhui opened a new issue #15985:
URL: https://github.com/apache/shardingsphere/issues/15985


   ## reply Issue #15734
   ```properties
   I don't know how to encode the automatic allocation algorithm and the 
standard allocation algorithm.
   ```
   
   ## 1: Scene one
   
   ```properties
   There are two tables in the sharding database, the table names aaa and bbb,I 
want to use MOD algorithm to split horizontally.
   ```
   
   ### 1.1. code 
   ```java
    public static void main(String[] args) throws SQLException {
           //配置数据源
           HikariDataSource dataSource = new HikariDataSource();
           dataSource.setDriverClassName(Driver.class.getName());
           
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sharding?serverTimezone=UTC");
           dataSource.setUsername("root");
           dataSource.setPassword("rootroot");
   
           Map<String,DataSource> dataSourceMap  = new HashMap<>();
           dataSourceMap.put("ds",dataSource);
   
           //分片规则配置对象
           ShardingRuleConfiguration shardingRuleConfiguration  = new 
ShardingRuleConfiguration();
           //设置要分片的表
           ShardingAutoTableRuleConfiguration nihao = new 
ShardingAutoTableRuleConfiguration("user","ds.aaa,ds.bbb");
           nihao.setShardingStrategy(new 
StandardShardingStrategyConfiguration("id","mod"));
           Properties properties  = new Properties();
           properties.setProperty("sharding-count","2");
           shardingRuleConfiguration.getShardingAlgorithms().put("mod",new 
ShardingSphereAlgorithmConfiguration("MOD",properties));
   
           shardingRuleConfiguration.getAutoTables().add(nihao);
   
           DataSource dataSource1 = 
ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, 
Collections.singleton(shardingRuleConfiguration), new Properties());
           Connection connection = dataSource1.getConnection();
           connection.prepareStatement("insert into user 
values(1,'xxxx','yyyyy')").execute();
           connection.prepareStatement("insert into user 
values(2,'yyyy','yyyyyy')").execute();
       }
   
   
   ```
   
    ### 1.2 error 
   ```java
   Exception in thread "main" 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException:
 Invalid format for actual data nodes: 'ds.aaa.user_0'
        at 
org.apache.shardingsphere.infra.datanode.DataNode.<init>(DataNode.java:51)
        at 
org.apache.shardingsphere.sharding.rule.TableRule.generateDataNodes(TableRule.java:170)
        at 
org.apache.shardingsphere.sharding.rule.TableRule.<init>(TableRule.java:111)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.createAutoTableRule(ShardingRule.java:175)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.lambda$createAutoTableRules$9(ShardingRule.java:168)
        at 
java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at 
java.base/java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1239)
        at 
java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at 
java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at 
java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
        at 
java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at 
java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.createAutoTableRules(ShardingRule.java:169)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.<init>(ShardingRule.java:112)
        at 
org.apache.shardingsphere.sharding.rule.builder.ShardingRuleBuilder.build(ShardingRuleBuilder.java:41)
        at 
org.apache.shardingsphere.sharding.rule.builder.ShardingRuleBuilder.build(ShardingRuleBuilder.java:35)
   ```
   
   ## 2: Scene two
   ```properties
   There are two databases named shardingone and shardingtwo. There is a table 
named userone in shardingone database and a table named usertwo in Shardingtwo 
database,  There is no rule between database name and table name. How to encode 
using MOD slicing algorithm? How to encode using line expression slicing 
algorithm?
   ```
   ### 2.1. mod code
   ```java
     public static void main(String[] args) throws SQLException {
           //配置数据源
           HikariDataSource dataSource = new HikariDataSource();
           dataSource.setDriverClassName(Driver.class.getName());
           
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/shardingone?serverTimezone=UTC");
           dataSource.setUsername("root");
           dataSource.setPassword("rootroot");
   
           HikariDataSource dataSource1 = new HikariDataSource();
           dataSource1.setDriverClassName(Driver.class.getName());
           
dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/shardingtwo?serverTimezone=UTC");
           dataSource1.setUsername("root");
           dataSource1.setPassword("rootroot");
   
           Map<String,DataSource> dataSourceMap  = new HashMap<>();
           dataSourceMap.put("shardingone",dataSource);
           dataSourceMap.put("shardingtwo",dataSource1);
   
           //分片规则配置对象
           ShardingRuleConfiguration shardingRuleConfiguration  = new 
ShardingRuleConfiguration();
           //设置要分片的表
           ShardingAutoTableRuleConfiguration nihao = new 
ShardingAutoTableRuleConfiguration("user","shardingone.userone,shardingtwo.usertwo");
           nihao.setShardingStrategy(new 
StandardShardingStrategyConfiguration("id","mod"));
           Properties properties  = new Properties();
           properties.setProperty("sharding-count","2");
           shardingRuleConfiguration.getShardingAlgorithms().put("mod",new 
ShardingSphereAlgorithmConfiguration("MOD",properties));
   
           shardingRuleConfiguration.getAutoTables().add(nihao);
   
           DataSource dataSource2 = 
ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, 
Collections.singleton(shardingRuleConfiguration), new Properties());
           Connection connection = dataSource2.getConnection();
           connection.prepareStatement("insert into user 
values(1,'xxxx','yyyyy')").execute();
           connection.prepareStatement("insert into user 
values(2,'yyyy','yyyyyy')").execute();
       }
   ```
   
   ### 2.2. mod error
   
   ```java
   Exception in thread "main" 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException:
 Invalid format for actual data nodes: 'shardingone.userone.user_0'
        at 
org.apache.shardingsphere.infra.datanode.DataNode.<init>(DataNode.java:51)
        at 
org.apache.shardingsphere.sharding.rule.TableRule.generateDataNodes(TableRule.java:170)
        at 
org.apache.shardingsphere.sharding.rule.TableRule.<init>(TableRule.java:111)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.createAutoTableRule(ShardingRule.java:175)
        at 
org.apache.shardingsphere.sharding.rule.ShardingRule.lambda$createAutoTableRules$9(ShardingRule.java:168)
        at 
java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at 
java.base/java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1239)
        at 
java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at 
java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at 
java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
        at 
java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at 
java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
   ```
   ### 2.3 inline code
   ```java
     public static void main(String[] args) throws SQLException {
           //配置数据源
           HikariDataSource dataSource = new HikariDataSource();
           dataSource.setDriverClassName(Driver.class.getName());
           
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/shardingone?serverTimezone=UTC");
           dataSource.setUsername("root");
           dataSource.setPassword("rootroot");
   
           HikariDataSource dataSource1 = new HikariDataSource();
           dataSource1.setDriverClassName(Driver.class.getName());
           
dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/shardingtwo?serverTimezone=UTC");
           dataSource1.setUsername("root");
           dataSource1.setPassword("rootroot");
   
           Map<String,DataSource> dataSourceMap  = new HashMap<>();
           dataSourceMap.put("shardingone",dataSource);
           dataSourceMap.put("shardingtwo",dataSource1);
   
   
           ShardingRuleConfiguration shardingRuleConfiguration  = new 
ShardingRuleConfiguration();
           ShardingTableRuleConfiguration nihao = new 
ShardingTableRuleConfiguration("user","shardingone.userone,shardingtwo.usertwo");
           shardingRuleConfiguration.getTables().add(nihao);
           
           Properties props = new Properties();
           //database name and table name  no rule  how to code expression
           //database name and table name  no rule  how to code expression
           //database name and table name  no rule  how to code expression
           props.setProperty("algorithm-expression", "");
           
           
           shardingRuleConfiguration.getShardingAlgorithms() .put("inline", new 
ShardingSphereAlgorithmConfiguration("INLINE", props));
   
   
   
   
       }
   ```
   # Please give me  an example demo 
   # Please give me  an example demo 
   # Please give me  an example demo 
   ```properties
   The important thing to say three times
   
   database name and table name  no rule  I don't know how to encode the 
automatic allocation algorithm and the standard allocation algorithm.
   ```
   


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