Mycat2 庫 schema

2021-09-09 14:43 更新

Mycat2支持使用SQL直接建表,請看Mycat2入門,可以通過建立SQL執(zhí)行腳本,在客戶端執(zhí)行即可.

配置的schema的邏輯庫邏輯表必須在原型庫(prototype)中有對應(yīng)的物理庫物理表,否則不能啟動

庫配置

{庫名}.schema.json保存在schemas文件夾 庫配置

  1. {
  2. "customTables": {},
  3. "globalTables": {},
  4. "normalTables": {},
  5. "schemaName": "test",
  6. "shardingTables": {},
  7. "targetName": "prototype"
  8. }

targetName自動從prototype目標(biāo)加載test庫下的物理表或者視圖作為單表,prototype必須是mysql服務(wù)器

該配置對應(yīng)1.6schema上配置dataNode用于讀寫分離

單表配置

1.18前

  1. {
  2. "schemaName": "mysql-test",
  3. "normalTables": {
  4. "role_edges": {
  5. "createTableSQL":null,//可選
  6. "dataNode": {
  7. "schemaName": "mysql",//物理庫
  8. "tableName": "role_edges",//物理表
  9. "targetName": "prototype"http://指向集群,或者數(shù)據(jù)源
  10. }
  11. }
  12. ......

1.18后

  1. {
  2. "schemaName": "mysql-test",
  3. "normalTables": {
  4. "role_edges": {
  5. "createTableSQL":null,//可選
  6. "locality": {
  7. "schemaName": "mysql",//物理庫
  8. "tableName": "role_edges",//物理表
  9. "targetName": "prototype"http://指向集群,或者數(shù)據(jù)源
  10. }
  11. }
  12. ......

全局表配置

1.18前

  1. {
  2. "schemaName": "mysql-test",
  3. "globalTables": {
  4. "role_edges": {
  5. "dataNodes": [{"targetName": "c0"},{"targetName": "c1"}]
  6. }
  7. ......

1.18后

  1. {
  2. "schemaName": "mysql-test",
  3. "globalTables": {
  4. "role_edges": {
  5. "broadcast": [{"targetName": "c0"},{"targetName": "c1"}]
  6. }
  7. ......

分片表配置

  1. {
  2. "customTables": {},
  3. "globalTables": {},
  4. "normalTables": {},
  5. "schemaName": "db1",
  6. "shardingTables": {
  7. "travelrecord": {
  8. "function": {
  9. "properties": {
  10. "dbNum": "2",//分庫數(shù)量
  11. "tableNum": "2",//分表數(shù)量
  12. "tableMethod": "hash(id)",//分表分片函數(shù)
  13. "storeNum": 2,//實際存儲節(jié)點數(shù)量
  14. "dbMethod": "hash(id)"http://分庫分片函數(shù)
  15. }
  16. }
  17. }
  18. }
  19. }

上述配置自動使用c0,c1兩個集群作為存儲節(jié)點

分片表配置-hash型自動分片算法

  1. {
  2. "customTables": {},
  3. "globalTables": {},
  4. "normalTables": {},
  5. "schemaName": "db1",
  6. "shadringTables": {
  7. "travelrecord": {
  8. "function": {
  9. "properties": {
  10. "dbNum": "2",//分庫數(shù)量
  11. "tableNum": "2",//分表數(shù)量
  12. "tableMethod": "hash(id)",//分表分片函數(shù)
  13. "storeNum": 2,//實際存儲節(jié)點數(shù)量
  14. "dbMethod": "hash(id)",//分庫分片函數(shù)
  15. "mappingFormat": "c${targetIndex}/db1_${dbIndex}/travelrecord_${tableIndex}"
  16. }
  17. }
  18. }
  19. }
  20. }

targetIndex,dbIndex,tableIndex總是從0開始計算,支持groovy運算生成目標(biāo)名,庫名,表名

分片表配置-自定義分片算法

1.18前

  1. {
  2. "customTables": {},
  3. "globalTables": {},
  4. "normalTables": {},
  5. "schemaName": "db1",
  6. "shadingTables": {
  7. "travelrecord": {
  8. "function": {
  9. "clazz": ,//具體自定義分片算法
  10. "properties": {
  11. ...分片算法參數(shù)
  12. }
  13. },
  14. "dataNode":{
  15. "targetNames":"c$0-1",
  16. "schemaNames":"db1_$0-1",
  17. "tableNames":"t1_$0-1"
  18. }
  19. }
  20. }
  21. }

1.18后

  1. {
  2. "customTables": {},
  3. "globalTables": {},
  4. "normalTables": {},
  5. "schemaName": "db1",
  6. "shadingTables": {
  7. "travelrecord": {
  8. "function": {
  9. "clazz": ,//具體自定義分片算法
  10. "properties": {
  11. ...分片算法參數(shù)
  12. }
  13. },
  14. "partition":{
  15. "targetNames":"c$0-1",
  16. "schemaNames":"db1_$0-1",
  17. "tableNames":"t1_$0-1"
  18. }
  19. }
  20. }
  21. }

partition配置存儲節(jié)點,在分片算法無法使用的時候就掃描這些存儲節(jié)點,所以分片算法無法正確配置的時候仍然可以查詢,但是可能插入報錯 需要注意的是在此處

partition中的生成表達(dá)式不支持groovy運算只支持$0-1語法生成

partition中的targetName-schemaName-tableName不能重復(fù)

樣例

  1. {
  2. "customTables":{},
  3. "globalTables":{},
  4. "normalTables":{},
  5. "schemaName":"db1",
  6. "shadingTables":{
  7. "sharding":{
  8. "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",
  9. "function":{
  10. "clazz":"io.mycat.router.mycat1xfunction.PartitionByHotDate",
  11. "properties":{
  12. "lastTime":90,
  13. "partionDay":180,
  14. "dateFormat":"yyyy-MM-dd",
  15. "columnName":"create_time"
  16. },
  17. "ranges":{}
  18. },
  19. "partition":{
  20. "schemaNames":"db1",
  21. "tableNames":"sharding_$0-1",
  22. "targetNames":"c0"
  23. }
  24. }
  25. }
  26. }
  1. for (String target : targets) {
  2. for (String schema : schemas) {
  3. for (String table : tables) {
  4. ....生成存儲節(jié)點
  5. }
  6. }
  7. }
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號