提交 9c72ae75 编写于 作者: C cherrylzhao

add hint-database-tables & hint-database-only.

上级 96ca47f1
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.shardingsphere.example.hint.raw.jdbc;
public enum HintType {
DATABASE_ONLY, DATABASE_TABLES, MASTER_ONLY
}
......@@ -36,8 +36,11 @@ import java.sql.Statement;
*/
public class YamlConfigurationExample {
// private static final HintType TYPE = HintType.DATABASE_ONLY;
private static final HintType TYPE = HintType.DATABASE_TABLES;
public static void main(final String[] args) throws SQLException, IOException {
DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases.yaml"));
DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile());
CommonService commonService = getCommonService(dataSource);
commonService.initEnvironment();
try (HintManager hintManager = HintManager.getInstance()) {
......@@ -46,8 +49,17 @@ public class YamlConfigurationExample {
commonService.cleanEnvironment();
}
private static File getFile(final String fileName) {
return new File(Thread.currentThread().getClass().getResource(fileName).getFile());
private static File getFile() {
switch (TYPE) {
case DATABASE_ONLY:
return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-only.yaml").getFile());
case DATABASE_TABLES:
return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-tables.yaml").getFile());
case MASTER_ONLY:
return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-only.yaml").getFile());
default:
throw new UnsupportedOperationException("unsupported type");
}
}
private static CommonService getCommonService(final DataSource dataSource) {
......@@ -55,12 +67,27 @@ public class YamlConfigurationExample {
}
private static void processWithHintValue(final DataSource dataSource, final HintManager hintManager) throws SQLException {
hintManager.addDatabaseShardingValue("t_order", 1L);
hintManager.addTableShardingValue("t_order", 1L);
setHintValue(hintManager);
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("select * from t_order");
statement.execute("SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id");
statement.execute("select * from t_order_item");
statement.execute("INSERT INTO t_order (user_id, status) VALUES (1, 'init')");
}
}
private static void setHintValue(final HintManager hintManager) {
switch (TYPE) {
case DATABASE_ONLY:
hintManager.setDatabaseShardingValue(1L);
return;
case DATABASE_TABLES:
hintManager.addDatabaseShardingValue("t_order", 1L);
hintManager.addTableShardingValue("t_order", 1L);
return;
default:
throw new UnsupportedOperationException("unsupported type");
}
}
}
......
dataSources:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
username: root
password:
ds_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
username: root
password:
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
hint:
algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm
defaultTableStrategy:
none:
props:
sql.show: true
......@@ -13,15 +13,18 @@ dataSources:
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order
actualDataNodes: ds_${0..1}.t_order_${0..1}
databaseStrategy:
hint:
algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm
tableStrategy:
hint:
algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm
keyGenerator:
type: SNOWFLAKE
column: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
bindingTables:
- t_order,t_order_item
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册