Mycat三 数据库分片

1 枚举分片

schemas.xml, 主要在于<schema>中table标签的rule属性 newsrule对应 rule.xml中的newrule

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<schema name="shenyi" checkSQLschema="false" sqlMaxLimit="100"> 
		 <table name="news_main" primaryKey="news_id" autoIncrement="true"  rule="newsrule"   
		 dataNode="dn1,dn2" />
	</schema> 
	 
	<dataNode name="dn1" dataHost="localhost1" database="jtthink" />
    <dataNode name="dn2" dataHost="localhost2" database="jtthink" />
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
		writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="192.168.1.108:3306" user="root"
			password="123123">
		</writeHost>
 
	</dataHost>

	<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
		writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM2" url="192.168.1.100:3306" user="root"
			password="123">
		</writeHost>
 
	</dataHost>
	 
	 
	 
</mycat:schema>

rule.xml  :   tableRule中name对应sechema-table-rule属性

tableRule - rule - columns 是数据库中的需要分片的字段名称 (真实数据库中的字段名称)

tableRule - rule - algorithm 对应 function的name

function - property - name="mapFile" 对应new_area_rule

function - property - name="type" = 1 表示从文件中读取

function - property - name="defaultNode" 表示如果不符合规则默认读写到0节点

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<tableRule name="newsrule">
		<rule>
			<columns>news_area</columns>
			<algorithm>abc</algorithm>
		</rule>
	</tableRule>
 
	<function name="abc"
		class="org.opencloudb.route.function.PartitionByFileMap">
		<property name="mapFile">news_area_rule</property>
		<property name="type">1</property>
		<property name="defaultNode">0</property>
	</function>
	 
</mycat:rule>

new_area_rule

jiangsu=0
beijing=1

2 范围分片

rule.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
    <tableRule name="idrange">
		<rule>
             <!-- 分片字段 -->
			<columns>news_id</columns>
			<algorithm>range-long</algorithm>
		</rule>
	</tableRule>

	<function name="range-long" class="org.opencloudb.route.function.AutoPartitionByLong">
          <!-- 从range_rule文件中读取规则 -->
		 <property name="mapFile">range_rule</property>
         <!-- 默认节点0 -->
		 <property name="defaultNode">0</property>
	</function>
	 
</mycat:rule>

range_rule

0-30=0    #0-30到0节点
31-100=1  #31-100到1节点

3 按天分片

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
    <tableRule name="date">
		<rule>
			<columns>news_date</columns>
			<algorithm>bydate</algorithm>
		</rule>
	</tableRule>
	<function name="bydate" class="org.opencloudb.route.function.PartitionByDate">
        <!-- 格式化日期格式 -->
		 <property name="dateFormat">yyyy-MM-dd</property>
        <!-- 开始时间 如果设置了开始时间则从这天起开始计算 -->
		 <property name="sBeginDate">2016-02-01</property>
         <!-- 结束时间 如果到这个时间循环从0节点开始  -->
		 <property name="sEndDate">2016-02-20</property>
         <!-- 间隔多久一个分片  -->
		 <property name="sPartionDay">2</property>
	</function>
	 
</mycat:rule>
发布了166 篇原创文章 · 获赞 26 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/qq_28710983/article/details/103760655