You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shardingsphere.apache.org by Eric <et...@gmail.com> on 2019/05/24 21:33:33 UTC

How to do database level horizontal sharding bases on table key

 Hi,

I read the documents that the sharding-proxy example uses user_id in the
defaultDatabaseStrategy to decide which database the query routed to. If I
want to separate the data horizontally for all tables which each table may
have their own keys, how to configure the yaml file?

For example I have database 0 and database 1 with all the tables identical.
I want the data separated in half bases on the tables' primary keys like
the one below. How to write the sharding rules in the yaml file?

database0.employee
Primary Key: emp_id
Sharding Rule: emp_id % 2 = 0

database0.computers
Primary Key: serial_no
Sharding Rule: serial_no % 2 = 0

database1.employee
Primary Key: emp_id
Sharding Rule: emp_id % 2 = 1

database1.computers
Primary key: serial_no
Sharding Rule: serial_no % 2 =1

Regards,
Eric

Re: How to do database level horizontal sharding bases on table key

Posted by "zhangliang@apache.org" <zh...@apache.org>.
I just create a new issue here:
https://github.com/apache/incubator-shardingsphere-doc/issues/183
You can trace this issue to get new information on this.

------------------

Liang Zhang (John)
Apache ShardingSphere & Dubbo


zhangliang@apache.org <zh...@apache.org> 于2019年5月26日周日 下午9:49写道:

> There are tow different sharding strategies  for databases and tables
> which you can config.
> FIY:
> https://github.com/apache/incubator-shardingsphere-example/blob/dev/sharding-jdbc-example/sharding-example/sharding-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables-range.yaml for
> Sharding-JDBC, but Sharding-Proxy is similar.
>
> We will add using sharding with databases and tables together's document
> later.
>
> ------------------
>
> Liang Zhang (John)
> Apache ShardingSphere & Dubbo
>
>
> Eric <et...@gmail.com> 于2019年5月25日周六 上午5:58写道:
>
>>  Hi,
>>
>> I read the documents that the sharding-proxy example uses user_id in the
>> defaultDatabaseStrategy to decide which database the query routed to. If I
>> want to separate the data horizontally for all tables which each table may
>> have their own keys, how to configure the yaml file?
>>
>> For example I have database 0 and database 1 with all the tables
>> identical.
>> I want the data separated in half bases on the tables' primary keys like
>> the one below. How to write the sharding rules in the yaml file?
>>
>> database0.employee
>> Primary Key: emp_id
>> Sharding Rule: emp_id % 2 = 0
>>
>> database0.computers
>> Primary Key: serial_no
>> Sharding Rule: serial_no % 2 = 0
>>
>> database1.employee
>> Primary Key: emp_id
>> Sharding Rule: emp_id % 2 = 1
>>
>> database1.computers
>> Primary key: serial_no
>> Sharding Rule: serial_no % 2 =1
>>
>> Regards,
>> Eric
>>
>

Re: How to do database level horizontal sharding bases on table key

Posted by zhaojun <zh...@126.com>.
Hi, Eric

For your question, you can config the yaml like this:

shardingRule:
  tables:
    employee:
      actualDataNodes: database${0..1}.employee
      databaseStrategy:
        inline:
          shardingColumn: emp_id
          algorithmExpression: database${emp_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: emp_id
    computers:
      actualDataNodes: database${0..1}.computers
      databaseStrategy:
        inline:
          shardingColumn: serial_no
          algorithmExpression: database${serial_no % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: serial_no
  defaultDatabaseStrategy:
    none:
  defaultTableStrategy:
    none:

------------------
Zhao Jun
Apache Sharding-Sphere & ServiceComb

> On May 26, 2019, at 11:40 PM, Eric <et...@gmail.com> wrote:
> 
> Hi John,
> 
> Thank you very much for your help.
> 
> Would you have more details about the rules behind org.apache.shardingsphere
> .example.algorithm.PreciseModuloShardingTableAlgorithmand and org.apache.
> shardingsphere.example.algorithm.RangeModuloShardingTableAlgorithm?
> 
> Regards,
> Eric Poon
> 
> On Sun, May 26, 2019 at 9:49 PM zhangliang@apache.org <zh...@apache.org>
> wrote:
> 
>> There are tow different sharding strategies  for databases and tables which
>> you can config.
>> FIY:
>> 
>> https://github.com/apache/incubator-shardingsphere-example/blob/dev/sharding-jdbc-example/sharding-example/sharding-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables-range.yaml
>> for
>> Sharding-JDBC, but Sharding-Proxy is similar.
>> 
>> We will add using sharding with databases and tables together's document
>> later.
>> 
>> ------------------
>> 
>> Liang Zhang (John)
>> Apache ShardingSphere & Dubbo
>> 
>> 
>> Eric <et...@gmail.com> 于2019年5月25日周六 上午5:58写道:
>> 
>>> Hi,
>>> 
>>> I read the documents that the sharding-proxy example uses user_id in the
>>> defaultDatabaseStrategy to decide which database the query routed to. If
>> I
>>> want to separate the data horizontally for all tables which each table
>> may
>>> have their own keys, how to configure the yaml file?
>>> 
>>> For example I have database 0 and database 1 with all the tables
>> identical.
>>> I want the data separated in half bases on the tables' primary keys like
>>> the one below. How to write the sharding rules in the yaml file?
>>> 
>>> database0.employee
>>> Primary Key: emp_id
>>> Sharding Rule: emp_id % 2 = 0
>>> 
>>> database0.computers
>>> Primary Key: serial_no
>>> Sharding Rule: serial_no % 2 = 0
>>> 
>>> database1.employee
>>> Primary Key: emp_id
>>> Sharding Rule: emp_id % 2 = 1
>>> 
>>> database1.computers
>>> Primary key: serial_no
>>> Sharding Rule: serial_no % 2 =1
>>> 
>>> Regards,
>>> Eric
>>> 
>> 


Re: How to do database level horizontal sharding bases on table key

Posted by Eric <et...@gmail.com>.
Hi John,

Thank you very much for your help.

Would you have more details about the rules behind org.apache.shardingsphere
.example.algorithm.PreciseModuloShardingTableAlgorithmand and org.apache.
shardingsphere.example.algorithm.RangeModuloShardingTableAlgorithm?

Regards,
Eric Poon

On Sun, May 26, 2019 at 9:49 PM zhangliang@apache.org <zh...@apache.org>
wrote:

> There are tow different sharding strategies  for databases and tables which
> you can config.
> FIY:
>
> https://github.com/apache/incubator-shardingsphere-example/blob/dev/sharding-jdbc-example/sharding-example/sharding-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables-range.yaml
> for
> Sharding-JDBC, but Sharding-Proxy is similar.
>
> We will add using sharding with databases and tables together's document
> later.
>
> ------------------
>
> Liang Zhang (John)
> Apache ShardingSphere & Dubbo
>
>
> Eric <et...@gmail.com> 于2019年5月25日周六 上午5:58写道:
>
> >  Hi,
> >
> > I read the documents that the sharding-proxy example uses user_id in the
> > defaultDatabaseStrategy to decide which database the query routed to. If
> I
> > want to separate the data horizontally for all tables which each table
> may
> > have their own keys, how to configure the yaml file?
> >
> > For example I have database 0 and database 1 with all the tables
> identical.
> > I want the data separated in half bases on the tables' primary keys like
> > the one below. How to write the sharding rules in the yaml file?
> >
> > database0.employee
> > Primary Key: emp_id
> > Sharding Rule: emp_id % 2 = 0
> >
> > database0.computers
> > Primary Key: serial_no
> > Sharding Rule: serial_no % 2 = 0
> >
> > database1.employee
> > Primary Key: emp_id
> > Sharding Rule: emp_id % 2 = 1
> >
> > database1.computers
> > Primary key: serial_no
> > Sharding Rule: serial_no % 2 =1
> >
> > Regards,
> > Eric
> >
>

Re: How to do database level horizontal sharding bases on table key

Posted by "zhangliang@apache.org" <zh...@apache.org>.
There are tow different sharding strategies  for databases and tables which
you can config.
FIY:
https://github.com/apache/incubator-shardingsphere-example/blob/dev/sharding-jdbc-example/sharding-example/sharding-raw-jdbc-example/src/main/resources/META-INF/sharding-databases-tables-range.yaml
for
Sharding-JDBC, but Sharding-Proxy is similar.

We will add using sharding with databases and tables together's document
later.

------------------

Liang Zhang (John)
Apache ShardingSphere & Dubbo


Eric <et...@gmail.com> 于2019年5月25日周六 上午5:58写道:

>  Hi,
>
> I read the documents that the sharding-proxy example uses user_id in the
> defaultDatabaseStrategy to decide which database the query routed to. If I
> want to separate the data horizontally for all tables which each table may
> have their own keys, how to configure the yaml file?
>
> For example I have database 0 and database 1 with all the tables identical.
> I want the data separated in half bases on the tables' primary keys like
> the one below. How to write the sharding rules in the yaml file?
>
> database0.employee
> Primary Key: emp_id
> Sharding Rule: emp_id % 2 = 0
>
> database0.computers
> Primary Key: serial_no
> Sharding Rule: serial_no % 2 = 0
>
> database1.employee
> Primary Key: emp_id
> Sharding Rule: emp_id % 2 = 1
>
> database1.computers
> Primary key: serial_no
> Sharding Rule: serial_no % 2 =1
>
> Regards,
> Eric
>