This topic describes the format of a routing expression and how to configure a routing algorithm for a logical table.
Background information
You can configure a routing algorithm for a logical table to efficiently query data of a specific physical table that matches the logical table based on a routing field. This way, you do not need to perform manual calculations or switch between physical tables. This reduces the routing overhead.
For more information, see Routing algorithm.
Scenarios
- Query the data of table shards.
- Change the data of table shards.
- Export the data of table shards.
Expression format
Data Management (DMS) allows you to define a routing algorithm to query table shards by using a Groovy expression. The format of a routing expression is similar to that used in application code.
You can specify a routing field in the following format: #
Routing field#
. Example: #shardKey#
.
Procedure
In the following example, a routing algorithm is configured to perform a simple modulo operation.
Create a logical table in a logical database. For more information, see Logical table.
After the logical table is configured, find the logical database on the Global Search page. Then, click Query in the Actions column to go to the SQL Console page.
NoteYou can also choose
in the top navigation bar. Then, select the logical database from the drop-down list to go to the SQL Console page.In the upper-right corner of the page that appears, click the icon to go to the table list page.
Find the logical table that you have created. Then, click Configure Algorithm in the Routing Algorithm column.
On the page that appears, click Create. In the Create Algorithm dialog box, configure the Algorithm Type, Modulo Operation, Table Partitioning Field, and Modulus parameters.
In this example, Modulo Operation on One Column is selected as Algorithm Type, Simple Modulo Operation is selected as Modulo Operation, id is selected as Table Partitioning Field, and the Modulus parameter is set to 4. The expression of the routing algorithm is
#id#%4
.NoteYou can perform simple modulo operations only on numeric fields.
You can perform modular hashing operations on both numeric and string fields.
Click Save.
After the routing algorithm is configured, query data in the logical table and execute INSERT statements. The system inserts the data into the corresponding table shard based on the result calculated by using the routing algorithm.
For example, if you insert a data row whose value of the ID column is 9, the routing result is 1 and the data row is inserted into the table shard whose name is logic_table_01.
Sample routing expressions
In the following sample routing expressions, user_id
is a field in a logical table.
Routing based on table sharding
Simple modulo operations
Modulo operation on a numeric field:
#user_id#%100
Secondary modulo operation:
#user_id#%10000%100
Modulo operation on the Java hash code of a numeric field:
Math.abs(#user_id#.hashCode())%100
Modulo operations on a string field
Numeric string hashing:
Math.abs(#user_id#.toString().hashCode())%100
String hashing:
Math.abs(#user_id#.hashCode())%100
CobarHash:
Math.abs(cobarHash(#column#,start, end)).intdiv(8)
CobarOldHash:
Math.abs(cobarOldHash(#column#, len)).intdiv(8)
Routing based on database sharding
Route data to the tables that have the same name in different databases:
'schema_prefix_'+(#user_id#%10)+'.table_name'
Route data to the specified database and table
'schema_prefix_'+(#user_id#%100)+'.table_name_prefix_'+(#user_id#%1000)
Route data to the same table shard in each database shard:
'schema_prefix_'+lastSwapZero(String.valueOf((#user_id#%1024).intdiv(128),4)+'.table_name_prefix_'+lastSwapZero(String.valueOf((#user_id#%128)),4)
Route data to the database whose name is determined by dividing the integer formed by the sixteenth and seventh characters of the EXTEND_ID field by 2 and to the table whose name is determined by the integer formed by the sixteenth and seventh characters of the EXTEND_ID field:
'schema_prefix_'+substring(#EXTEND_ID#,16,18).toLong().intdiv(2)+'.table_name_prefix_'+substring(#EXTEND_ID#,16,18)
Routing based on the date
Route data to the same table on the same day of each month:
dayOfMonth(#time#)
Routing based on the third last character of a string field
Integer.valueOf(substring(#ip_id#,-3,-2))*10
: If the index value of a table name increases by 10, the integer converted from the third last character of the ip_id field is multiplied by 10. If the index value of a table name increases by 1, the integer converted from the third last character of the ip_id field is not multiplied by 10.Other complex routing methods
Route data by using user-defined functions (UDFs):
String func(String arg){ return arg.hashCode()%10;} 'table_name_'+func(#user_id#)+'_other_'func(#user_id#)
NoteYou must use line breaks between functions and expressions.
DMS supports
CRC32(java.util.zip.CRC32)
function routing.
General built-in functions
cobarOldHash
cobarHash
weekOfYear
dayOfYear
dayOfMonth
dayOfWeek
substring
last4swap
lastSwapZero