Search in sources :

Example 6 with ShardingTableConfig

use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashIdHashId2.

/*
    test for mappingFormat
     */
@Test
public void testHashIdHashId2() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by mod_hash(id) tbpartitions 2 dbpartitions 4;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"2\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"4\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"hash(id)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("id", (new RangeVariable("id", RangeVariableType.EQUAL, 15))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_1', tableName='sharding_7', index=7, dbIndex=1, tableIndex=3}]"));
    String s1 = shardingFuntion.calculate(Collections.emptyMap()).toString();
    Assert.assertEquals("[{targetName='c0', schemaName='db1_0', tableName='sharding_0', index=0, dbIndex=0, tableIndex=0}, {targetName='c0', schemaName='db1_0', tableName='sharding_1', index=1, dbIndex=0, tableIndex=1}, {targetName='c0', schemaName='db1_0', tableName='sharding_2', index=2, dbIndex=0, tableIndex=2}, {targetName='c0', schemaName='db1_0', tableName='sharding_3', index=3, dbIndex=0, tableIndex=3}, {targetName='c0', schemaName='db1_1', tableName='sharding_4', index=4, dbIndex=1, tableIndex=0}, {targetName='c0', schemaName='db1_1', tableName='sharding_5', index=5, dbIndex=1, tableIndex=1}, {targetName='c0', schemaName='db1_1', tableName='sharding_6', index=6, dbIndex=1, tableIndex=2}, {targetName='c0', schemaName='db1_1', tableName='sharding_7', index=7, dbIndex=1, tableIndex=3}]", s1);
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 7 with ShardingTableConfig

use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.

the class YYYYWEEKFunctionTest method testYYYYWEEK.

/**
 *     https://help.aliyun.com/document_detail/71334.html
 */
@Test
public void testYYYYWEEK() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYWEEK(traveldate) tbpartition by YYYYWEEK(traveldate) tbpartitions 3 dbpartitions 8;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"14\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYWEEK(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYWEEK(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_1', tableName='sharding_15', index=15, dbIndex=1, tableIndex=1}]"));
    Assert.assertEquals(112, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 8 with ShardingTableConfig

use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.

the class YYYYMMFunctionTest method testYYYYMM.

/**
 *     https://help.aliyun.com/document_detail/71334.html
 */
@Test
public void testYYYYMM() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYMM(traveldate) tbpartition by YYYYMM(traveldate) tbpartitions 3 dbpartitions 8;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYMM(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_2', tableName='sharding_6', index=6, dbIndex=2, tableIndex=0}]"));
    Assert.assertEquals(24, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 9 with ShardingTableConfig

use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.

the class YYYYMMFunctionTest method testYYYYMMHash.

@Test
public void testYYYYMMHash() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by mod_hash(id) tbpartition by YYYYMM(traveldate) tbpartitions 3 dbpartitions 24;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"24\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"3\",\n" + "\t\t\t\t\t\"tableMethod\":\"HASH(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYMM(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_6', tableName='sharding_0', index=18, dbIndex=6, tableIndex=0}, {targetName='c0', schemaName='db1_6', tableName='sharding_1', index=19, dbIndex=6, tableIndex=1}, {targetName='c0', schemaName='db1_6', tableName='sharding_2', index=20, dbIndex=6, tableIndex=2}]"));
    Assert.assertEquals(72, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 10 with ShardingTableConfig

use of io.mycat.config.ShardingTableConfig in project Mycat2 by MyCATApache.

the class YYYYDDFunctionTest method testYYYYDD.

/**
 *     https://help.aliyun.com/document_detail/71337.html
 */
@Test
public void testYYYYDD() {
    ShardingTableConfig mainSharding = new ShardingTableConfig();
    mainSharding.setCreateTableSQL("CREATE TABLE db1.`sharding` (\n" + "  `id` bigint NOT NULL AUTO_INCREMENT,\n" + "  `user_id` varchar(100) DEFAULT NULL,\n" + "  `traveldate` date DEFAULT NULL,\n" + "  `fee` decimal(10,0) DEFAULT NULL,\n" + "  `days` int DEFAULT NULL,\n" + "  `blob` longblob,\n" + "  PRIMARY KEY (`id`),\n" + "  KEY `id` (`id`)\n" + ") ENGINE=InnoDB  DEFAULT CHARSET=utf8" + " dbpartition by YYYYDD(traveldate) tbpartition by YYYYDD(traveldate) tbpartitions 92 dbpartitions 8;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"8\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${index}\",\n" + "\t\t\t\t\t\"tableNum\":\"92\",\n" + "\t\t\t\t\t\"tableMethod\":\"YYYYDD(traveldate)\",\n" + "\t\t\t\t\t\"storeNum\":1,\n" + "\t\t\t\t\t\"dbMethod\":\"YYYYDD(traveldate)\"\n" + "\t\t\t\t}", Map.class)).build());
    MetadataManager metadataManager = getMetadataManager(mainSharding);
    ShardingTable tableHandler = (ShardingTable) metadataManager.getTable("db1", "sharding");
    CustomRuleFunction shardingFuntion = tableHandler.getShardingFuntion();
    List<Partition> calculate = shardingFuntion.calculate(Collections.singletonMap("traveldate", (new RangeVariable("traveldate", RangeVariableType.EQUAL, LocalDate.of(2020, 6, 5)))));
    String s = calculate.toString();
    Assert.assertTrue(s.contains("[{targetName='c0', schemaName='db1_5', tableName='sharding_533', index=533, dbIndex=5, tableIndex=73}]"));
    Assert.assertEquals(736, shardingFuntion.calculate(Collections.emptyMap()).size());
    System.out.println();
}
Also used : Partition(io.mycat.Partition) RangeVariable(io.mycat.RangeVariable) MetadataManager(io.mycat.MetadataManager) ShardingTableConfig(io.mycat.config.ShardingTableConfig) CustomRuleFunction(io.mycat.router.CustomRuleFunction) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Aggregations

ShardingTableConfig (io.mycat.config.ShardingTableConfig)14 MetadataManager (io.mycat.MetadataManager)12 ShardingTable (io.mycat.calcite.table.ShardingTable)12 CustomRuleFunction (io.mycat.router.CustomRuleFunction)12 Test (org.junit.Test)12 RangeVariable (io.mycat.RangeVariable)11 Partition (io.mycat.Partition)10 Map (java.util.Map)10 ImmutableMap (com.google.common.collect.ImmutableMap)5 IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 TableView (javafx.scene.control.TableView)1