Search in sources :

Example 26 with MetadataManager

use of io.mycat.MetadataManager 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)

Example 27 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class YYYYDDFunctionTest method testYYYYDDHashId.

@Test
public void testYYYYDDHashId() {
    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 mod_hash(id) tbpartitions 8 dbpartitions 366;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"dbNum\":\"366\",\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\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_157', tableName='sharding_0', index=314, dbIndex=157, tableIndex=0}, {targetName='c0', schemaName='db1_157', tableName='sharding_1', index=315, dbIndex=157, tableIndex=1}]"));
    Assert.assertEquals(732, 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 28 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashTableId.

@Test
public void testHashTableId() {
    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" + " tbpartition by mod_hash(id) tbpartitions 2;");
    mainSharding.setFunction(ShardingFunction.builder().properties(JsonUtil.from("{\n" + "\t\t\t\t\t\"mappingFormat\":\"c${targetIndex}/db1_${dbIndex}/sharding_${tableIndex}\",\n" + "\t\t\t\t\t\"tableNum\":\"2\",\n" + "\t\t\t\t\t\"tableMethod\":\"hash(id)\",\n" + "\t\t\t\t\t\"storeNum\":1,\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))));
    Assert.assertEquals(1, calculate.size());
    Assert.assertEquals(2, 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) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ShardingTable(io.mycat.calcite.table.ShardingTable) Test(org.junit.Test)

Example 29 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class HashFunctionTest method testHashDbId.

@Test
public void testHashDbId() {
    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);");
    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_${tableIndex}\",\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();
    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_1', tableName='sharding_0', index=1, dbIndex=1, tableIndex=0}]", s1);
    System.out.println();
}
Also used : 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 30 with MetadataManager

use of io.mycat.MetadataManager in project Mycat2 by MyCATApache.

the class MySQLLogConsumer method init.

private void init() throws SQLException {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(metadataManager.getPrototype())) {
        JdbcUtils.execute(connection.getRawConnection(), "CREATE TABLE  IF NOT EXISTS mycat.`sql_log` (\n" + "  `instanceId` bigint(20) DEFAULT NULL,\n" + "  `user` varchar(64) DEFAULT NULL,\n" + "  `connectionId` bigint(20) DEFAULT NULL,\n" + "  `ip` varchar(22) DEFAULT NULL,\n" + "  `port` bigint(20) DEFAULT NULL,\n" + "  `traceId` varchar(22) NOT NULL,\n" + "  `hash` varchar(22) DEFAULT NULL,\n" + "  `sqlType` varchar(22) DEFAULT NULL,\n" + "  `sql` longtext,\n" + "  `transactionId` varchar(22) DEFAULT NULL,\n" + "  `sqlTime` bigint(20) DEFAULT NULL,\n" + "  `responseTime` datetime DEFAULT NULL,\n" + "  `affectRow` int(11) DEFAULT NULL,\n" + "  `result` tinyint(1) DEFAULT NULL,\n" + "  `externalMessage` tinytext,\n" + "  PRIMARY KEY (`traceId`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", Collections.emptyList());
    }
}
Also used : MetadataManager(io.mycat.MetadataManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Aggregations

MetadataManager (io.mycat.MetadataManager)34 ShardingTable (io.mycat.calcite.table.ShardingTable)22 Test (org.junit.Test)22 Partition (io.mycat.Partition)17 ShardingTableConfig (io.mycat.config.ShardingTableConfig)12 CustomRuleFunction (io.mycat.router.CustomRuleFunction)12 RangeVariable (io.mycat.RangeVariable)11 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)10 ValuePredicateAnalyzer (io.mycat.calcite.rewriter.ValuePredicateAnalyzer)10 java.util (java.util)10 Map (java.util.Map)10 RexNode (org.apache.calcite.rex.RexNode)10 TableHandler (io.mycat.TableHandler)9 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)7 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 MetaClusterCurrent (io.mycat.MetaClusterCurrent)5 MycatCalciteSupport (io.mycat.calcite.MycatCalciteSupport)5 Connection (java.sql.Connection)5