Sharding-Jdbc custom sub-library sub-table strategy - composite slicing algorithm custom implementation

Fragmentation strategy Sharding-JDBC has two dimensions, namely:

  • Source data partitioning strategy (DatabaseShardingStrategy)
  • Table partitioning strategy (TableShardingStrategy)

Wherein the data source represented by partitioning strategy: target route data to physical data source policy table showing slice data is routed to the destination table.

In particular, table partitioning strategy is dependent on the data source partitioning strategy, that first sub-library sub-table again, of course, can only score sheet.

           

Learn Sharding-JDBC data partitioning strategy

       Sharding-JDBC fragment policy involves fragmentation and segmentation algorithm key. As the realization is closely related to fragmentation algorithms and business, so Sharding-JDBC does not provide a built-in algorithm fragmentation, but fragmentation strategy by the various scenes extracted, providing a high level of abstraction, by providing an interface to allow developers to implement their own slicing algorithm.

The following quote from the official documents. Official Documents

First introduced four kinds of slicing algorithm.

By slicing algorithm data piece, support via =, BETWEEN IN and fragmentation.
Slicing algorithm requires application developers themselves implement side, it can be achieved very high flexibility.

It is now offering four slicing algorithm. As the realization is closely related to fragmentation algorithms and business,
and therefore does not provide a built-sliced arithmetic, but by fragmented policy derives from a variety of scenarios,
providing a higher level of abstraction and provides an interface to allow application developers to implement their own slice algorithm.

  • Exact sliced ​​arithmetic -PreciseShardingAlgorithm

          For processing using a single key as a key = fragment scene for fragmentation and IN. Need to meet StandardShardingStrategy use.

  • Range-sliced ​​arithmetic -RangeShardingAlgorithm

          For processing the scene using a single key as a key BETWEEN AND fragments for fragmentation. Need to meet StandardShardingStrategy use.

  • Composite sliced ​​arithmetic -ComplexKeysShardingAlgorithm

For processing as a scene using a multi-key shard key for fragmentation, comprising a plurality of logic more complex shard key, application developers need to handle the complexity of which self. Need to meet ComplexShardingStrategy use.

Note  : We are in business development, often based on user id list of records a user's query, but also based on a natural key query needs a record of the user, which need to use complex fragmentation algorithm. For example, the Orders table, we only need to check your order list data for a certain period of time userId, but also need to check certain pieces of data according to the order orderId. Here, orderId and userId composite shard key belongs.

  • Hint sliced ​​arithmetic -HintShardingAlgorithm

Hint fragmentation refers to the fragmentation field determined non-SQL, and other external conditions are determined by the scene can be flexibly injection Frag field Hint by using SQL.

Hint fragmentation strategy is to bypass the SQL parsing, it is possible to achieve the limit Sharding-JDBC syntax is not supported by implementing the algorithm.

Hint used for processing the scene row slice. Need to meet HintShardingStrategy use.

Then introduce the next five kinds of partitioning strategy.

  • Standard partitioning strategy -StandardShardingStrategy

= Provide, slicing BETWEEN AND IN and support of the SQL statement. StandardShardingStrategy only supports single-shard key, and providing PreciseShardingAlgorithm RangeShardingAlgorithm two sliced ​​arithmetic. PreciseShardingAlgorithm must be selected for processing and IN = fragmentation. RangeShardingAlgorithm is optional for processing BETWEEN AND fragments, if not configured RangeShardingAlgorithm, BETWEEN AND SQL database in accordance whole routing process.

  • Composite partitioning strategy -ComplexShardingStrategy

= Provide, slicing BETWEEN AND IN and support of the SQL statement. Multiply key ComplexShardingStrategy support, due to the complex relationship between Multiply key, not so much for a package, but directly to the key-value pair fragment and transparently transmitted to the operator slice sliced ​​arithmetic entirely by the application developer those who achieve, providing maximum flexibility.

Here reflects the framework designer thorough understanding of the design principles, it will expose the user to change point, within the same package, clear delineation of the boundaries of abstraction and implementation, it is worth learning.

  • Line expression fragmentation strategy -InlineShardingStrategy

Groovy expressions used to provide operational support for SQL statements and IN = fragmentation, fragmentation only supports single bond. For simple slice algorithm, by a simple configuration using, in order to avoid cumbersome Java code development, such as: T User $ -> {u_id% 8} represents t_user table according to u_id die 8, is divided into eight table, the table name as t_user_0 to t_user_7.

  • Hint partitioning strategy -HintShardingStrategy

Hint rather than by way of parsing SQL fragmentation strategy.

  • Do not Fragment strategy -NoneShardingStrategy

The strategy without fragmentation strategy.

Real - custom composite partitioning strategy

Since the purpose of close combat, so that focused on how to implement complex partitioning strategy, namely to achieve partitioning strategy ComplexShardingStrategy Interface customization available.

Guess you like

Origin www.cnblogs.com/rinack/p/11241111.html