cobar rule 简介

来源:互联网 发布:电脑怎么安装淘宝 编辑:程序博客网 时间:2024/05/23 01:20
rule可以自己编写比较函数,系统提供了Long 和 String的 其中Long用于例子的已经讲解的很详细。

代码中已经将count和length之积写死,我们要做的是提供count和length要符合这个要求。
可以在PartitionUtil.java中找到;
  1. // 分区长度:数据段分布定义,其中取模的数一定要是2^n, 因为这里使用x % 2^n == x & (2^n - 1)等式,来优化性能。
  2. private static final int PARTITION_LENGTH = 1024;

下面贴一个按照String作为规则的rule:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. - Copyright 1999-2012 Alibaba Group.
  4. -
  5. - Licensed under the Apache License, Version 2.0 (the "License");
  6. - you may not use this file except in compliance with the License.
  7. - You may obtain a copy of the License at
  8. -
  9. - http://www.apache.org/licenses/LICENSE-2.0
  10. -
  11. - Unless required by applicable law or agreed to in writing, software
  12. - distributed under the License is distributed on an "AS IS" BASIS,
  13. - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. - See the License for the specific language governing permissions and
  15. - limitations under the License.
  16. -->
  17. <!DOCTYPE cobar:rule SYSTEM "rule.dtd">
  18. <cobar:rule xmlns:cobar="http://cobar.alibaba.com/">
  19. <!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法 -->
  20. <tableRule name="rule2">
  21. <rule>
  22. <columns>siteid</columns>
  23. <algorithm><![CDATA[diffsite(${siteid}) ]]></algorithm>
  24. </rule>
  25. </tableRule>
  26. <function name="diffsite" class="com.alibaba.cobar.route.function.PartitionByString">
  27. <property name="partitionCount">512</property>
  28. <property name="partitionLength">2</property>
  29. <property name="hashSlice">-5:</property>
  30. </function>
  31. </cobar:rule>

如果使用的是PartitionByString那么则根据提供的String的hash值来做判断,如果hash是1那么就将该条记录分布在第一个库里。如果是2则分布在第二个库里。hash值可以计算某一段String里的,其hash方式如下,类似python。
  1. /**
  2. * "2" -> (0,2)<br/>
  3. * "1:2" -> (1,2)<br/>
  4. * "1:" -> (1,0)<br/>
  5. * "-1:" -> (-1,0)<br/>
  6. * ":-1" -> (0,-1)<br/>
  7. * ":" -> (0,0)<br/>
  8. */
  9. public static Pair<Integer, Integer> sequenceSlicing(String slice) {
  10. int ind = slice.indexOf(':');
  11. if (ind < 0) {
  12. int i = Integer.parseInt(slice.trim());
  13. if (i >= 0) {
  14. return new Pair<Integer, Integer>(0, i);
  15. } else {
  16. return new Pair<Integer, Integer>(i, 0);
  17. }
  18. }
  19. String left = slice.substring(0, ind).trim();
  20. String right = slice.substring(1 + ind).trim();
  21. int start, end;
  22. if (left.length() <= 0) {
  23. start = 0;
  24. } else {
  25. start = Integer.parseInt(left);
  26. }
  27. if (right.length() <= 0) {
  28. end = 0;
  29. } else {
  30. end = Integer.parseInt(right);
  31. }
  32. return new Pair<Integer, Integer>(start, end);
  33. }
说明 如下:
  1. str = 0123456789
  2. print str[0:3] #截取第一位到第三位的字符
  3. print str[:] #截取字符串的全部字符
  4. print str[6:] #截取第七个字符到结尾
  5. print str[:-3] #截取从头开始到倒数第三个字符之前
  6. print str[2] #截取第三个字符
  7. print str[-1] #截取倒数第一个字符
  8. print str[::-1] #创造一个与原字符串顺序相反的字符串
  9. print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
  10. print str[-3:] #截取倒数第三位到结尾
  11. print str[:-5:-3] #逆序截取,具体啥意思没搞明白?
0 0
原创粉丝点击