W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Mycat2支持使用SQL直接建表,請看Mycat2入門,可以通過建立SQL執(zhí)行腳本,在客戶端執(zhí)行即可.
配置的schema的邏輯庫邏輯表必須在原型庫(prototype)中有對應(yīng)的物理庫物理表,否則不能啟動
{庫名}.schema.json保存在schemas
文件夾
庫配置
{
"customTables": {},
"globalTables": {},
"normalTables": {},
"schemaName": "test",
"shardingTables": {},
"targetName": "prototype"
}
targetName
自動從prototype
目標(biāo)加載test
庫下的物理表或者視圖作為單表,prototype
必須是mysql
服務(wù)器
該配置對應(yīng)1.6
的schema
上配置dataNode
用于讀寫分離
1.18前
{
"schemaName": "mysql-test",
"normalTables": {
"role_edges": {
"createTableSQL":null,//可選
"dataNode": {
"schemaName": "mysql",//物理庫
"tableName": "role_edges",//物理表
"targetName": "prototype"http://指向集群,或者數(shù)據(jù)源
}
}
......
1.18后
{
"schemaName": "mysql-test",
"normalTables": {
"role_edges": {
"createTableSQL":null,//可選
"locality": {
"schemaName": "mysql",//物理庫
"tableName": "role_edges",//物理表
"targetName": "prototype"http://指向集群,或者數(shù)據(jù)源
}
}
......
1.18前
{
"schemaName": "mysql-test",
"globalTables": {
"role_edges": {
"dataNodes": [{"targetName": "c0"},{"targetName": "c1"}]
}
......
1.18后
{
"schemaName": "mysql-test",
"globalTables": {
"role_edges": {
"broadcast": [{"targetName": "c0"},{"targetName": "c1"}]
}
......
{
"customTables": {},
"globalTables": {},
"normalTables": {},
"schemaName": "db1",
"shardingTables": {
"travelrecord": {
"function": {
"properties": {
"dbNum": "2",//分庫數(shù)量
"tableNum": "2",//分表數(shù)量
"tableMethod": "hash(id)",//分表分片函數(shù)
"storeNum": 2,//實際存儲節(jié)點數(shù)量
"dbMethod": "hash(id)"http://分庫分片函數(shù)
}
}
}
}
}
上述配置自動使用c0
,c1
兩個集群作為存儲節(jié)點
分片表配置-hash
型自動分片算法
{
"customTables": {},
"globalTables": {},
"normalTables": {},
"schemaName": "db1",
"shadringTables": {
"travelrecord": {
"function": {
"properties": {
"dbNum": "2",//分庫數(shù)量
"tableNum": "2",//分表數(shù)量
"tableMethod": "hash(id)",//分表分片函數(shù)
"storeNum": 2,//實際存儲節(jié)點數(shù)量
"dbMethod": "hash(id)",//分庫分片函數(shù)
"mappingFormat": "c${targetIndex}/db1_${dbIndex}/travelrecord_${tableIndex}"
}
}
}
}
}
targetIndex
,dbIndex
,tableIndex
總是從0
開始計算,支持groovy
運算生成目標(biāo)名,庫名,表名
1.18前
{
"customTables": {},
"globalTables": {},
"normalTables": {},
"schemaName": "db1",
"shadingTables": {
"travelrecord": {
"function": {
"clazz": ,//具體自定義分片算法
"properties": {
...分片算法參數(shù)
}
},
"dataNode":{
"targetNames":"c$0-1",
"schemaNames":"db1_$0-1",
"tableNames":"t1_$0-1"
}
}
}
}
1.18后
{
"customTables": {},
"globalTables": {},
"normalTables": {},
"schemaName": "db1",
"shadingTables": {
"travelrecord": {
"function": {
"clazz": ,//具體自定義分片算法
"properties": {
...分片算法參數(shù)
}
},
"partition":{
"targetNames":"c$0-1",
"schemaNames":"db1_$0-1",
"tableNames":"t1_$0-1"
}
}
}
}
partitio
n配置存儲節(jié)點,在分片算法無法使用的時候就掃描這些存儲節(jié)點,所以分片算法無法正確配置的時候仍然可以查詢,但是可能插入報錯
需要注意的是在此處
partition
中的生成表達(dá)式不支持groovy
運算只支持$0-1
語法生成
partition
中的targetName-schemaName-tableName
不能重復(fù)
樣例
{
"customTables":{},
"globalTables":{},
"normalTables":{},
"schemaName":"db1",
"shadingTables":{
"sharding":{
"createTableSQL":"CREATE TABLE db1.`sharding` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `user_id` varchar(100) DEFAULT NULL,\n `create_time` 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",
"function":{
"clazz":"io.mycat.router.mycat1xfunction.PartitionByHotDate",
"properties":{
"lastTime":90,
"partionDay":180,
"dateFormat":"yyyy-MM-dd",
"columnName":"create_time"
},
"ranges":{}
},
"partition":{
"schemaNames":"db1",
"tableNames":"sharding_$0-1",
"targetNames":"c0"
}
}
}
}
for (String target : targets) {
for (String schema : schemas) {
for (String table : tables) {
....生成存儲節(jié)點
}
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: