cedar0822 opened a new issue, #32778:
URL: https://github.com/apache/shardingsphere/issues/32778
Hi,
I configured the data source and database and table splitting strategy
through the JAVA API,
Currently, there is no problem with single data sources for table queries
that do not participate in database partitioning. When there are multiple data
sources, it is prompted that the table does not exist.
this is sphere config:
pom:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.4.1</version>
</dependency>
private Map<String, DataSource> initShardingDataSources(Environment env)
throws SQLException {
String dataname = "sharding";
Map<String, DataSource> dataSourceMap = new HashMap<>();
HikariDataSource dataSourceWrite = new HikariDataSource();
dataSourceWrite.setDriverClassName(env.getProperty("spring.datasource.druid.driver-class-name"));
dataSourceWrite.setJdbcUrl(env.getProperty("spring.datasource.url"));
dataSourceWrite.setUsername(env.getProperty("spring.datasource.username"));
dataSourceWrite.setPassword(env.getProperty("spring.datasource.password"));
dataSourceMap.put(dataname, dataSourceWrite);
slaveDataSources.put(dataname, shardingDataSource(dataSourceMap));
return dataSourceMap;
}
DataSource shardingDataSource(Map<String, DataSource> dataSourceMap)
throws SQLException {
Properties props = new Properties();
props.setProperty("sql-show", Boolean.TRUE.toString());
Collection<RuleConfiguration> ruleConfigs = new ArrayList<>();
ShardingRuleConfiguration shardingRuleConfig = new
ShardingRuleConfiguration();
shardingRuleConfig.getBindingTableGroups().add(new
ShardingTableReferenceRuleConfiguration("nabd", "*.*"));
shardingRuleConfig.setDefaultTableShardingStrategy(new
StandardShardingStrategyConfiguration("create_time", "tb-sharding-algorithm"));
shardingRuleConfig.getTables().add(getGiftSendRecordTableRuleConfiguration());
shardingRuleConfig.setShardingAlgorithms(getShardingAlgorithms());
DataSource dataSource =
ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, ruleConfigs,
props);
return dataSource;
}
ShardingTableRuleConfiguration getGiftSendRecordTableRuleConfiguration()
{
ShardingTableRuleConfiguration result = new
ShardingTableRuleConfiguration("gift_send_record",
"nabd.gift_send_record_$->{[2024]}_Q$->{1..4}");
result.setTableShardingStrategy(new
StandardShardingStrategyConfiguration("create_time","tb-sharding-algorithm"));
return result;
}
private Map<String, AlgorithmConfiguration> getShardingAlgorithms() {
Map<String, AlgorithmConfiguration> shardingAlgorithms = new
LinkedHashMap<>();
Properties tableAlgorithms = new Properties();
tableAlgorithms.setProperty("strategy","standard");
tableAlgorithms.setProperty("algorithmClassName",
TableShardingAlgorithmByQuarter.class.getName());
shardingAlgorithms.put("tb-sharding-algorithm", new
AlgorithmConfiguration("CLASS_BASED", tableAlgorithms));
return shardingAlgorithms;
}
there is an error when query table of no separate database:
Caused by: org.apache.ibatis.executor.ExecutorException: Error preparing
statement. Cause:
org.apache.shardingsphere.infra.exception.TableNotExistsException: Table or
view `gift_send_record` does not exist.
at
org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:97)
at
org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49)
at
com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.intercept(PaginationInterceptor.java:174)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy715.prepare(Unknown Source)
at
com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.prepareStatement(MybatisSimpleExecutor.java:92)
at
com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:53)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at
com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.update(MybatisCachingExecutor.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy714.update(Unknown Source)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
... 100 common frames omitted
Caused by:
org.apache.shardingsphere.infra.exception.TableNotExistsException: Table or
view `gift_send_record` does not exist.
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.lambda$checkTableExists$4(SimpleTableSegmentBinder.java:146)
at
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:41)
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.checkTableExists(SimpleTableSegmentBinder.java:143)
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.bind(SimpleTableSegmentBinder.java:82)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.lambda$bind$0(InsertStatementBinder.java:58)
at java.util.Optional.ifPresent(Optional.java:159)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:58)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:48)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bindDMLStatement(SQLBindEngine.java:103)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:80)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:71)
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:202)
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:167)
at
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:83)
at
org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:86)
at
org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88)
... 126 common frames omitted

I don't see configure single table rule with java API, can you give me some
java API configure demo?
--
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]