Summary of the invention
The method that it is an object of the invention to a kind of to store tenant's structural data in multi-tenant database, can be effectiveThe basic data and self-defining data of storage tenant meets the interests of service supplier while reaching tenant's demand.
In order to achieve the above-mentioned object of the invention, the technical solution adopted in the present invention is as follows:
The method that the embodiment of the invention provides a kind of to store tenant's structural data in multi-tenant database, comprising:
(1) setting structure data model:
The structural data model is by basic data model, expansion table data model and key-value pair data model groupAt;
The basic data model is used to store the basic information of tenant, and the expansion table data model is for storing tenantThe expansion information of basic information, the key-value pair data model are used to store the user-defined data table information of tenant;
(2) classify tenant:
The data type generated according to tenant classifies to the tenant in multi-tenant application;
(3) tenant data is stored:
According to classification as a result, to the data that different types of tenant generates, the difference in structural data model is usedData model is stored.
As a kind of specific embodiment, the basic data model includes tenant's table and multiple basic information tables.
As a kind of specific embodiment, the expansion packetization, which includes, expands field data and expansion field metadata.
As a kind of specific embodiment, the expansion table data model includes that a Zhang Tuo opens up tables of data and Zhang Tuo exhibition wordSection metadata table.
As a kind of specific embodiment, the user-defined data table information includes table metadata information, field metadata informationWith Value Data information.
As a kind of specific embodiment, the key-value pair data model includes a table metadata table, a field memberTables of data and a Value Data table.
As a kind of specific embodiment, it is described according to tenant generate data type to multi-tenant application in tenant intoRow classification specifically:
The tenant for only generating basic data is defined as the first tenant;
The tenant for generating basic data and custom field data is defined as the second tenant;
The tenant for generating basic data, user-defined data table data and custom field data is defined as third tenant.
As a kind of specific embodiment, it is described according to classification as a result, to different types of tenant generate data, makeIt is stored with the different data model in structural data model specifically:
The basic data that the first tenant generates is stored using basic data model;
The basic data that the second tenant generates is stored using basic data model, uses expansion table data model storage secondThe custom field data that tenant generates;
The basic data generated using basic data model storage third tenant, stores third using expansion table data modelThe custom field data that tenant generates, the user-defined data table data generated using key-value pair data model storage third tenant.
Compared with prior art, technical solution provided by the invention has the following beneficial effects:
The method that tenant's structural data is stored in multi-tenant database that embodiment of the present invention provides, passes through settingStructural data model, the models coupling basic data model, expansion table data model and key-value pair data model, overcomesThe shortcomings that single model is when storing multi-tenant data, can effectively store the basic data and self-defining data of tenant,While reaching tenant's demand, meet the interests of service supplier.
Specific embodiment
In order to be fully understood from the purpose of the present invention, feature and effect, below with reference to attached drawing 1-3 to structure of the inventionThink of, specific structure and the technical effect of generation are described further.
The present embodiment, by taking Course Website group's application system with multi-tenant architecture as an example, the system is with a subjectMono- tenant of Cheng Zuowei, the soft and hardware resource of shared system between multiple tenants, is divided into three big components, background system, foregroundSystem and database, the application system logical architecture is referring to Fig. 1, from figure 1 it appears that the system includes ftp server101, content management server 102, web application server 103, database (File Transfer Protocol Server)Server 104 and external user 105 and internal user 106.Wherein, present invention is mainly applied on database.
As shown in Fig. 2, a kind of method that tenant's structural data is stored in multi-tenant database is present embodiments provided,Include:
Step 201: setting structure data model
The structural data model is by basic data model, expansion table data model and key-value pair data model groupAt.
Wherein, the basic data model is used to store the basic information of tenant, and basic data model is arranged two tables, and oneOpen tenant's table and multiple basic information tables.Wherein, tenant's table (tenant_info) setting tenant_id (tenant ID, major key),Name (name), telephone (phone), address (address) and email (E-mail address) field, user's table (user_Info) user_id (User ID, major key) is set, tenant_id (tenant id, external key), ext_data_id (field id is expanded,External key), name (name), age (age) and telephone (phone) field.
The expansion table data model is used to store the expansion information of tenant's basic information, and the expansion information includes expandingField data and expansion field metadata.It expands data model and needs to be arranged two tables, a Zhang Tuo opens up tables of data and Zhang Tuo's exhibitionField metadata table.Wherein, tables of data (ext_data) setting ext_data_id (expanding data ID, major key), ext_ are expandedCol_id (expanding column ID, external key) and expansion data value (value) field, expand field metadata table (ext_column_Metadata) be arranged ext_col_id (expand column ID, major key), tenant_id (tenant ID, external key), name (column name) andType (column data type) field.
The key-value pair data model is used to store the user-defined data table information of tenant, and the user-defined data table information includes list cellData information, field metadata information and Value Data information, the key-value pair data model need to be arranged three tables, include oneTable metadata table, a field metadata table and a Value Data table.Wherein, table metadata table (table_matadata) is arrangedWhether id (table id, major key), tenant_id (tenant ID, external key), name (table name), enabled (come into force), info (table descriptionInformation) field, field metadata table (column_metadata) setting col_id (column ID, major key), tenant_id (tenantID, external key), table_id (table id, external key), name (column name), type (field data types), length (field length),Null (can be null), def_v (default value), enabled (whether coming into force) and info (column description information) field, Value DataId (value ID, major key) is arranged in table (data), tenant_id (tenant ID, external key), table_id (table id, external key), col_id(column ID, external key), value (field value) and info (value description information) field.As shown in figure 3, for the structuring number after settingAccording to Model B EKV.
Step 202: classification tenant
The data type generated according to tenant classifies to the tenant in multi-tenant application.It is described according to tenant generateData type classifies to the tenant in multi-tenant application specifically: the tenant for only generating basic data is defined as the first rentFamily;The tenant for generating basic data and custom field data is defined as the second tenant;Basic data, user-defined data table will be generatedThe tenant of data and custom field data is defined as third tenant.
There are three tenant A, B, C on the SaaS platform of the present embodiment, specifying information is as shown in the table:
Table 4-1 is tenant's information table (tenant_info)
| tenant_id | name | telephone | address | email |
| 001 | A | 13666666666 | Nanchang | pty@163.com |
| 002 | B | 13888888888 | ShenZhen,GuangDong | xy@163.com |
| 003 | C | 13999999999 | Beihai Fisheries Base Guangxi Province | abc@163.com |
Table 4-2 is the user message table (user_info) of tenant A
| user_id | tenant_id | name | age | telephone |
| 100001 | 001 | Zhang Xi | 18 | 13677777777 |
| 100002 | 001 | Zhao Qi | 19 | 13688888888 |
Table 4-3 is the user message table (user_info_B) of tenant B
| user_id | tenant_id | name | age | telephone | sex | email |
| 200001 | 002 | Zhao Zhong | 20 | 13699999999 | Male | a@163.com |
| 200002 | 002 | Party building | 21 | 13600000000 | Female | b@163.com |
Table 4-4 is the user message table (my_user) of tenant C
| user_id | tenant_id | father_name | birthday |
| 300001 | 003 | Ren Hang | 1988-10-10 |
| 300002 | 003 | Hu Rong | 2000-10-01 |
Wherein, for tenant A, basic data is only generated, first kind tenant is belonged to.For tenant B, need to existing tablePart modification is carried out, the second class tenant is belonged to.For tenant C, needs newly-built table with storing data, belong to third class tenant.
Step 203: storage tenant data
According to classification as a result, to the data that different types of tenant generates, the difference in structural data model is usedData model is stored.Its specifically: the basic data that the first tenant generates is stored using basic data model;Use basisData model stores the basic data that the second tenant generates, and stores the customized of the second tenant generation using expansion table data modelField data;The basic data generated using basic data model storage third tenant, uses expansion table data model storage theThe custom field data that three tenants generate, the user-defined data table data generated using key-value pair data model storage third tenant.
The data of tenant A are stored using the tables of data in basic data model.
Tenant B generates basic data and custom field data, basic data are deposited using the tables of data of basic data modelStorage, custom field data are stored using the tables of data in expansion table data model.
Tenant C can generate basic data, user-defined data table data and custom field data, wherein basic data uses baseThe tables of data of plinth data model stores, and custom field data are stored using the tables of data for expanding table data model, user-defined data tableData are stored using the tables of data of key-value pair data model.
As shown in chart 5-1 to table 5-7, stored for the data by above-mentioned steps, three tenants A, B, C of the present embodimentSchematic diagram after BEKV model.
Table 5-1 basic data model tenant information table (tenant_info)
| tenant_id | name | telephone | address | email |
| 001 | A | 13666666666 | Nanchang | pty@163.com |
| 002 | B | 13888888888 | ShenZhen,GuangDong | xy@163.com |
| 003 | C | 13999999999 | Beihai Fisheries Base Guangxi Province | abc@163.com |
The user message table (user_info) of table 5-2 basic data model tenant
| user_id | tenant_id | ext_data_id | name | age | telephone |
| 100001 | 001 | | Zhang Xi | 18 | 13677777777 |
| 100002 | 001 | | Zhao Qi | 19 | 13688888888 |
| 200001 | 002 | e_d_0 | Zhao Zhong | 20 | 13699999999 |
| 200002 | 002 | e_d_1 | Party building | 21 | 13600000000 |
Table 5-3 expands data model and expands field metadata table (ext_column_metadata)
| ext_col_id | tenant_id | name | type |
| e_d_0 | 002 | sex | char(2) |
| e_d_1 | 002 | email | varchar(100) |
Table 5-4 expands data model and expands tables of data (ext_data)
| ext_data_id | ext_col_id | value |
| e_d_0 | e_c_0 | Male |
| e_d_0 | e_c_1 | a@163.com |
| e_d_1 | e_c_0 | Female |
| e_d_1 | e_c_1 | b@163.com |
Table 5-5 key-value pair data model table metadata table (table_matadata)
| table_id | tenant_id | name | enabled | info |
| t_0 | 003 | user_info_c | yes | The table of tenant C |
Table 5-6 key-value pair data model field metadata table (column_metadata)
| col_id | tenant_id | table_id | name | type | length | null | def_v | enabled |
| c_0 | 003 | t_1 | user_id | varchar | 20 | yes | | yes |
| c_1 | 003 | t_1 | father_name | varchar | 20 | yes | | yes |
| c_2 | 003 | t_1 | birthday | date | | no | | yes |
Table 5-7 key-value pair data model value tables of data (data)
| id | tenant_id | table_id | col_id | value | info |
| d_0 | 003 | t_1 | c_0 | 300001 | |
| d_1 | 003 | t_1 | c_0 | 300002 | |
| d_2 | 003 | t_1 | c_1 | Ren Hang | |
| d_3 | 003 | t_1 | c_1 | Hu Rong | |
| d_4 | 003 | t_1 | c_3 | 1988-10-10 | |
| d_5 | 003 | t_1 | c_3 | 2000-10-01 | |
The method that tenant's structural data is stored in multi-tenant database that embodiment of the present invention provides, passes through settingStructural data model, the models coupling basic data model, expansion table data model and key-value pair data model, overcomesThe shortcomings that single model is when storing multi-tenant data, can effectively store the basic data and self-defining data of tenant,While reaching tenant's demand, meet the interests of service supplier.
It should be understood that the invention is not limited to above embodiment, it is all to various changes or modifications of the invention notBe detached from the spirit and scope of the present invention, if these modification and variations belong to claim and equivalent technologies range of the invention itInterior, then the present invention is also implied that comprising these modification and variations.