Mycatプロファイルrule.xml

40528 ワード

rule.xmlプロファイルは、テーブルを分割するルール定義を定義します.テーブルに対して異なるスライスアルゴリズムを柔軟に使用したり、テーブルに対して同じアルゴリズムを使用したりすることができますが、具体的なパラメータは異なります.
このファイルには主にtableRuleとfunctionの2つのラベルがあります.
1、tableRuleラベル
<tableRule name="rule1">
    <rule>
        <columns>idcolumns>
        <algorithm>func1algorithm>
    rule>
tableRule>

nameプロパティ:
異なるテーブル・ルールを識別する一意の名前を指定します.
ruleラベル:
物理テーブルのどのカラムを分割し、どのルーティングアルゴリズムを使用するかを指定します.columnsは、分割するカラムの名前を指定します.Algorithmはfunctionラベルのnameプロパティを使用して、テーブルルールと特定のルーティングアルゴリズムを接続します.
2、functionラベル
<function name="func1" class="io.mycat.route.function.PartitionByLong">
    <property name="partitionCount">8property>
    <property name="partitionLength">128property>
function>

nameプロパティ:
アルゴリズムの名前を指定します.classプロパティ:
   ルーティングアルゴリズムの特定のクラス名を指定します.プロパティ:
特定のアルゴリズムに必要なプロパティ.
3、よく使うスライス規則
3.1、列挙法
<tableRule name="sharding-by-intfile">
    <rule>
        <columns>user_idcolumns>
        <algorithm>hash-intalgorithm>
    rule>
tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
    <property name="mapFile">partition-hash-int.txtproperty>
    <property name="type">0property>
    <property name="defaultNode">0property>
function>

partition-hash-int.txt構成:
10000=0
10010=1

上のcolumnsはスライスするテーブルフィールド、algorithmスライス関数を識別し、スライス関数構成ではmapFileはプロファイル名を識別し、typeのデフォルト値は0、0はInteger、非ゼロはStringを表し、すべてのノード構成は0から始まり、0はノード1を表す.
defaultNodeデフォルトノード:0未満はデフォルトノードを設定しないことを示し、0以上はデフォルトノードを設定することを示し、ノードは指定された値であり、デフォルトノードの役割:スライスを列挙する際に認識されない列挙値に遭遇した場合はデフォルトノードにルーティングさせ、デフォルトノードを構成しない場合は(defaultNode値が0未満はデフォルトノードを構成しないことを示す)、認識されない列挙値に遭遇するとエラーが表示されます.like this:can't find datanode for sharding column:column_nameval:ffffffff .   3.2、固定スライスhashアルゴリズム
<tableRule name="rule1">
    <rule>
         <columns>user_idcolumns>
         <algorithm>func1algorithm>
    rule>
tableRule>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
    <property name="partitionCount">2,1property>
    <property name="partitionLength">256,512property>
function>

上のcolumnsは、スライスするテーブルフィールド、algorithmスライス関数、partitionCountスライス個数リスト、partitionLengthスライス範囲リストを識別します.≪パーティション長|Partition Length|emdw≫:デフォルトは最大2^n=1024、すなわち最大1024パーティションをサポートします.コンストレイント:count、lengthの2つの配列の長さは一致している必要があります.1024=sum((count[i]*length[i]).countとlengthの2つのベクトルの点積は1024に等しい.例:
@Test
public void testPartition() {
    //        :         3 ,     25%,    50%。(        )
    // ||
    // ||||
    // | partition0 | partition1 |        partition2       |
    // |  2 , count[0]=2       |    1 , count[1]=1    |
    int[] count = new int[] { 2, 1 };
    int[] length = new int[] { 256, 512 };
    PartitionUtil pu = new PartitionUtil(count, length);
    //          offerId   memberId                 
    int DEFAULT_STR_HEAD_LEN = 8; // cobar        
    long offerId = 12345;
    String memberId = "qiushuo";

  //    offerId  ,partNo1   0,         ,offerId 12345       partition0 
    int partNo1 = pu.partition(offerId);

  //    memberId  ,partNo2   2,         ,memberId qiushuo      partition2 
    int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);

    Assert.assertEquals(0, partNo1);
    Assert.assertEquals(2, partNo2);

}

平均配分設定が必要な場合:平均4スライス、partitionCount*partitionLength=1024
<function name="func1" class="org.opencloudb.route.function.PartitionByLong">
    <property name="partitionCount">4property>
    <property name="partitionLength">256property>
function>

3.3、範囲の約束
<tableRule name="auto-sharding-long">
    <rule>
      <columns>user_idcolumns>
      <algorithm>rang-longalgorithm>
    rule>
tableRule>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong">
    <property name="mapFile">autopartition-long.txtproperty>
function>

Autopartition-long.txtファイル:
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
 
0-10000000=0
10000001-20000000=1

columnsは、スライスするテーブルフィールド、algorithmスライス関数、rang-long関数のmapFileがプロファイルパスを表し、すべてのノード構成が0から始まり、0がノード1を表します.この構成は非常に簡単です.すなわち、可能なid範囲をスライスに事前に設定します. 
3.4、型を求める方法
<tableRule name="mod-long">
    <rule>
      <columns>user_idcolumns>
      <algorithm>mod-longalgorithm>
    rule>
tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
   
    <property name="count">3property>
function>

columnsは、スライスするテーブルフィールド、algorithmスライス関数を識別します.この構成は、idとcount(あなたのノード数)に基づいてモデリング予算を行うことを明確にしています.方式1に比べて、一括挿入時にデータソースを切り替える必要があります.idは連続しません.
3.5、日付列区分法
<tableRule name="sharding-by-date">
      <rule>
        <columns>create_timecolumns>
        <algorithm>sharding-by-datealgorithm>
      rule>
tableRule> 
<function name="sharding-by-date" class="io.mycat.route.function..PartitionByDate">
   <property name="dateFormat">yyyy-MM-ddproperty>
    <property name="sBeginDate">2014-01-01property>
    <property name="sPartionDay">10property>
function>

columnsは、スライスするテーブルフィールド、algorithmスライス関数を識別し、構成では開始日、パーティション日数、すなわちデフォルトでは開始日から10日間に1つのパーティションを区切るように構成されています.
3.6、通し型取り
<tableRule name="sharding-by-pattern">
    <rule>
        <columns>user_idcolumns>
        <algorithm>sharding-by-patternalgorithm>
    rule>
tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern">
    <property name="patternValue">256property>
    <property name="defaultNode">2property>
    <property name="mapFile">partition-pattern.txtproperty>
function>

partition-pattern.txt 
# id partition range start-end ,data node index
###### first host configuration
1-32=0
33-64=1
65-96=2
97-128=3
######## second host configuration
129-160=4
161-192=5
193-224=6
225-256=7
0-0=7

columnsは、スライスするテーブルフィールド、algorithmスライス関数、patternValue、すなわちモジュールベース、defaoultNodeデフォルトノードを識別し、デフォルトを構成しない場合、デフォルトは0、すなわち最初のノードである.mapFileプロファイルパス
プロファイルでは、1-32はid%256以降の分布の範囲を表し、1-32ではパーティション1、その他の類推でidがデジタルデータでない場合はdefaoultNodeデフォルトノードに割り当てられます.コードの例:
String idVal = "0";
Assert.assertEquals(true, 7 == autoPartition.calculate(idVal));
idVal = "45a";
Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));

 3.7、ASCIIコードのモジュール化
<tableRule name="sharding-by-prefixpattern">
      <rule>
            <columns>user_idcolumns>
            <algorithm>sharding-by-prefixpatternalgorithm>
      rule>
tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPrefixPattern">
    <property name="patternValue">256property>
    <property name="prefixLength">5property>
    <property name="mapFile">partition-pattern.txtproperty>
function>

partition-pattern.txt
# range start-end ,data node index
# ASCII
# 48-57=0-9
# 64、65-90=@、A-Z
# 97-122=a-z
###### first host configuration
1-4=0
5-8=1
9-12=2
13-16=3
###### second host configuration
17-20=4
21-24=5
25-28=6
29-32=7
0-0=7

columnsはスライスするテーブルフィールドを識別し、algorithmスライス関数、patternValueはモジュール基数を求め、prefixLength ASCIIが切り取ったビット数.mapFileプロファイルパス、プロファイルのうち、1-32はid%256の後に分布する範囲を表し、1-32であればパーティション1、その他の類推.
このような方式の類似方式6は、カラム種取得前のprefixLengthをすべてのASCIIコードの和に列挙してsum%patternValueを求め、取得した値、ワイルドレンジ内のスライス数、
/**
* ASCII  :
* 48-57=0-9     
* 64、65-90=@、A-Z
* 97-122=a-z
*
*/
String idVal="gf89f9a";
Assert.assertEquals(true, 0 == autoPartition.calculate(idVal));

idVal="8df99a";
Assert.assertEquals(true, 4 == autoPartition.calculate(idVal));

idVal="8dhdf99a";
Assert.assertEquals(true, 3 == autoPartition.calculate(idVal));

3.8、プログラミングの指定
<tableRule name="sharding-by-substring">
    <rule>
        <columns>user_idcolumns>
        <algorithm>sharding-by-substringalgorithm>
    rule>
tableRule>
<function name="sharding-by-substring" class="io.mycat.route.function.PartitionDirectBySubString">
    <property name="startIndex">0property> 
    <property name="size">2property>
    <property name="partitionCount">8property>
    <property name="defaultPartition">0property>
function>

columnsは、スライスするテーブルフィールド、algorithmスライス関数を識別します.この方法は、文字列(数値でなければなりません)に直接基づいて分割番号を計算します(転送パラメータを適用し、分割番号を明示的に指定します).
例えばid=05-1000002であり、この構成ではidのstartIndex=0からsize=2ビットの数字である05、05が取得されたパーティションを表し、デフォルトでdefaultPartitionに割り当てられていない場合を表す.
3.9、文字列分割hash解析
<tableRule name="sharding-by-stringhash">
      <rule>
            <columns>user_idcolumns>
            <algorithm>sharding-by-stringhashalgorithm>
      rule>
tableRule>
<function name="sharding-by-substring" class="io.mycat.route.function.PartitionByString">
    <property name="length">512property> 
    <property name="count">2property>
    <property name="hashSlice">0:2property>
function>

columnsはスライスするテーブルフィールド、algorithmスライス関数を識別し、関数のlengthは文字列hashを表してモデル基数、countパーティション数、hashSlice hash予算ビット、すなわちサブ文字列hashに基づいて演算し、
hashSlice : 0 means str.length(), -1 means str.length()-1
/**
     * "2" -> (0,2)
* "1:2" -> (1,2)
* "1:" -> (1,0)
* "-1:" -> (-1,0)
* ":-1" -> (0,-1)
* ":" -> (0,0)
*/ public class PartitionByStringTest { @Test public void test() { PartitionByString rule = new PartitionByString(); String idVal=null; rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); rule.setHashSlice("0:2"); // idVal = "0"; // Assert.assertEquals(true, 0 == rule.calculate(idVal)); // idVal = "45a"; //Assert.assertEquals(true, 1 == rule.calculate(idVal)); //last 4 rule = new PartitionByString(); rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); //last 4 characters rule.setHashSlice("-4:0"); idVal = "aaaabbb0000"; Assert.assertEquals(true, 0 == rule.calculate(idVal)); idVal = "aaaabbb2359"; Assert.assertEquals(true, 0 == rule.calculate(idVal));
  } }

 3.10、一致性hash
<tableRule name="sharding-by-murmur">
      <rule>
        <columns>user_idcolumns>
        <algorithm>murmuralgorithm>
      rule>
tableRule>
<function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash">
      <property name="seed">0property>
      <property name="count">2property>
      <property name="virtualBucketTimes">160property>
      
      
function>

コンシステンシhash予算は分散データの拡張問題を効果的に解決し,前1−9ではidルールにデータ拡張の難題が多少存在し,10ルールはデータ拡張の難点を解決した.
 
転載先:https://www.cnblogs.com/kingsonfu/p/10627423.html