A data compression method, decompression method and device
By generating and replacing the target key in JSON data, the problem of large JSON data storage space is solved, achieving efficient data compression and saving storage space.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING IQIYI TECH CO LTD
- Filing Date
- 2022-12-26
- Publication Date
- 2026-04-17
AI Technical Summary
In JSON format data, the keys in each key-value pair contain a large number of characters, resulting in a large storage space being occupied during storage.
By obtaining the index value of the key-value pair in the JSON format data, a target field containing preset characters and index values is generated as a new key, and the original key is replaced with the target key to generate the target data.
It reduces the space required for data storage, achieving a compression rate of up to 30%, while maintaining the data format.
Smart Images

Figure CN116126996B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a data compression method, decompression method, and apparatus. Background Technology
[0002] JSON (JavaScript Object Notation) is a lightweight data format. For NoSQL (non-relational) databases, such as Couchbase and MongoDB, data stored in these databases is typically in JSON format. JSON data contains multiple key-value pairs, each containing a key and a value. In practical applications, business data is often in the form of Java objects (an object-oriented programming language). Therefore, when storing data, Java objects can be serialized to obtain JSON data, which is then stored in the database.
[0003] However, in JSON format data, the keys in each key-value pair come directly from the original Java object. Therefore, the keys in each key-value pair contain a lot of characters, which leads to a large storage space being occupied when storing JSON format data. Summary of the Invention
[0004] The purpose of this invention is to provide a data compression method, decompression method, and apparatus to save storage space. The specific technical solution is as follows:
[0005] In a first aspect of the present invention, a data compression method is provided, the method comprising:
[0006] Obtain raw data in JSON format; wherein the raw data contains multiple key-value pairs; each key-value pair contains a raw key and a corresponding key-value pair;
[0007] Obtain the index value corresponding to the original key of the plurality of key-value pairs; wherein, the index value corresponding to an original key indicates the position of the key-value pair to which the original key belongs in the original data;
[0008] For each first key-value pair in the original data, generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair;
[0009] If the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, then the target field is determined to be the target key of the first key-value pair.
[0010] Replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data.
[0011] Optionally, before generating a target field containing a preset character and an index value corresponding to the original key in each first key-value pair in the original data, the method further includes:
[0012] Obtain the data type of the original key for each key-value pair in the original data;
[0013] For each key-value pair in the original data, if the original key of the key-value pair is a first data type, the key-value pair is determined to be a first key-value pair, where the first data type is an object type; a first data type for an original key indicates that the key value corresponding to the original key contains a second key-value pair.
[0014] If the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified numerical value, then the key-value pair is determined to be a first key-value pair; wherein the second data type is an integer type.
[0015] Optionally, after obtaining the data type of the original key of each key-value pair in the original data, the method further includes:
[0016] If the original key of the key-value pair is a second data type and the key value of the key-value pair is the specified numerical value, discard the key-value pair.
[0017] Optionally, after determining that the key-value pair is a first key-value pair if the original key of the key-value pair is a first data type, the method further includes:
[0018] For each first key-value pair in the original data, if the key value of the first key-value pair contains a second key-value pair, obtain the data type of the original key of the second key-value pair;
[0019] If the original key of the second key-value pair is of the first data type, then the second key-value pair is determined to be the first key-value pair.
[0020] If the original key of the second key-value pair is a second data type and the key value of the second key-value pair is not the specified value, then the second key-value pair is determined to be the first key-value pair.
[0021] Optionally, obtaining the index value corresponding to the original key of the plurality of key-value pairs includes:
[0022] Obtain the configuration file of the Java object corresponding to the original data; wherein, each key-value pair contained in the original data is the same as each key-value pair contained in the Java object; the configuration file contains the original keys of each of the multiple key-value pairs in the Java object;
[0023] The configuration file is subjected to reflection processing to obtain the original keys of each key-value pair in the original data;
[0024] For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0025] In a second aspect of the present invention, a data decompression method is also provided, the method comprising:
[0026] Obtain target data in JSON format; wherein the target data is obtained based on any of the data compression methods described in the first aspect above; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair;
[0027] For each key-value pair in the target data, if the target key of the key-value pair contains a preset character, the key-value pair is determined to be the first key-value pair;
[0028] For each first key-value pair, obtain the original key corresponding to the index value in the target key of the first key-value pair to obtain the original key of the first key-value pair;
[0029] Replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data.
[0030] Optionally, before obtaining the original key corresponding to the index value in the target key of each first key-value pair to obtain the original key of the first key-value pair, the method further includes:
[0031] For each key-value pair in the target data, if the key value of the key-value pair contains a second key-value pair, and the target key of the second key-value pair contains a preset character, then the second key-value pair is determined to be the first key-value pair.
[0032] Optionally, before obtaining the original key corresponding to the index value in the target key of each first key-value pair to obtain the original key of the first key-value pair, the process includes:
[0033] Obtain the configuration file of the Java object corresponding to the target data; wherein, the configuration file contains the original keys of multiple key-value pairs in the Java object;
[0034] The configuration file is subjected to reflection processing to obtain the original keys of multiple key-value pairs in the Java object.
[0035] For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0036] Optionally, after replacing the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data, the method further includes:
[0037] For each original key in the configuration file, obtain the key-value pair containing that original key from the original data;
[0038] If there is no key-value pair containing the original key in the original data, the key value corresponding to the original key is determined to a specified value to obtain a key-value pair containing the original key;
[0039] Each key-value pair is written to a pre-created blank Java object to obtain the Java object corresponding to the target data.
[0040] In a third aspect of the invention, a data compression apparatus is also provided, the apparatus comprising:
[0041] The first acquisition module is used to acquire raw data in JSON format; wherein the raw data contains multiple key-value pairs; each key-value pair contains an original key and a corresponding key-value pair;
[0042] The second acquisition module is used to acquire the index value corresponding to the original key of the plurality of key-value pairs; wherein, the index value corresponding to an original key indicates the position of the key-value pair to which the original key belongs in the original data;
[0043] The first generation module is used to generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data;
[0044] The second generation module is used to determine that the target field is the target key of the first key-value pair if the number of characters in the target field is less than the number of characters in the original key of the first key-value pair.
[0045] The replacement module is used to replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data.
[0046] Optionally, the device further includes:
[0047] The third acquisition module is used to acquire the data type of the original key of each key-value pair in the original data before the first generation module generates a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data;
[0048] The first determining module is used to determine that for each key-value pair in the original data, if the original key of the key-value pair is a first data type, the key-value pair is a first key-value pair, wherein; the first data type is an object type; a first data type for an original key indicates that the key value corresponding to the original key contains a second key-value pair;
[0049] The second determining module is used to determine the key-value pair as the first key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified numerical value; wherein the second data type is an integer type.
[0050] Optionally, the device further includes:
[0051] The discard module is configured to, after the third acquisition module has acquired the data type of the original key of each key-value pair in the original data, discard the key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is the specified value.
[0052] Optionally, the device further includes:
[0053] The fourth acquisition module is used to, after the first determination module determines that the key-value pair is a first key-value pair if the original key of the key-value pair is a first data type, execute the following steps for each first key-value pair in the original data: if the key value of the first key-value pair contains a second key-value pair, acquire the data type of the original key of the second key-value pair.
[0054] The third determining module is used to determine that the second key-value pair is the first key-value pair if the original key of the second key-value pair is the first data type;
[0055] The fourth determining module is used to determine the second key-value pair as the first key-value pair if the original key of the second key-value pair is a second data type and the key value of the second key-value pair is not the specified value.
[0056] Optionally, the second acquisition module is specifically used to acquire the configuration file of the Java object corresponding to the original data; wherein, each key-value pair contained in the original data is the same as each key-value pair contained in the Java object; the configuration file contains the original keys of each of the multiple key-value pairs in the Java object;
[0057] The configuration file is subjected to reflection processing to obtain the original keys of each key-value pair in the original data;
[0058] For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0059] In a fourth aspect of the invention, a data decompression apparatus is also provided, the apparatus comprising:
[0060] The first acquisition module is used to acquire target data in JSON format; wherein the target data is obtained based on any of the data compression methods described above; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair;
[0061] The first determining module is used to determine that for each key-value pair in the target data, if the target key of the key-value pair contains a preset character, the key-value pair is the first key-value pair.
[0062] The second acquisition module is used to acquire the original key corresponding to the index value in the target key of each first key-value pair, and obtain the original key of the first key-value pair.
[0063] The replacement module is used to replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data.
[0064] Optionally, the device further includes:
[0065] The second determining module is used to, before the second obtaining module performs the process of obtaining the original key corresponding to the index value in the target key of each first key-value pair and obtaining the original key of the first key-value pair, perform the process of obtaining the original key of the first key-value pair for each key-value pair in the target data, and if the key value of the key-value pair contains a second key-value pair and the target key of the second key-value pair contains a preset character, determine the second key-value pair as the first key-value pair.
[0066] Optionally, the device further includes:
[0067] The third acquisition module is used to acquire the configuration file of the Java object corresponding to the target data before the second acquisition module executes the process of acquiring the original key corresponding to the index value in the target key of each first key-value pair to obtain the original key of the first key-value pair; wherein, the configuration file contains the original keys of each of the multiple key-value pairs in the Java object;
[0068] The third determining module is used to perform reflection processing on the configuration file to obtain the original keys of multiple key-value pairs in the Java object.
[0069] The fourth determining module is used to determine the sequence number representing the position of each original key in the configuration file, and obtain the index value corresponding to the original key.
[0070] Optionally, the device further includes:
[0071] The fourth acquisition module is used to, after the replacement module performs the operation of replacing the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data, perform the operation of obtaining the key-value pair containing the original key from the original data for each original key in the configuration file.
[0072] The fifth determining module is used to determine the key value corresponding to the original key as a specified value if there is no key-value pair containing the original key in the original data, so as to obtain a key-value pair containing the original key.
[0073] The generation module is used to write each key-value pair into a pre-created blank Java object to obtain the Java object corresponding to the target data.
[0074] In another aspect of the present invention, an electronic device is also provided, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus.
[0075] Memory, used to store computer programs;
[0076] When a processor executes a program stored in a memory, it implements the data compression method steps described in any of the first aspects above, or the data decompression method steps described in any of the second aspects above.
[0077] In another aspect of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements the data compression method steps described in any of the first aspects above, or the data decompression method steps described in any of the second aspects above.
[0078] In another aspect of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to perform the data compression method described in any of the first aspects above, or the data decompression method described in any of the second aspects above.
[0079] This invention provides a data compression method that obtains raw data in JSON format. The raw data contains multiple key-value pairs. Each key-value pair contains an original key and a corresponding key-value pair. The method obtains the index value corresponding to the original key of the multiple key-value pairs. The index value corresponding to an original key represents the position of the key-value pair to which the original key belongs in the raw data. For each first key-value pair in the raw data, a target field containing preset characters and the index value corresponding to the original key in the first key-value pair is generated. If the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, the target field is determined as the target key of the first key-value pair. The original key of each first key-value pair is replaced with the target key of each first key-value pair to obtain the target data.
[0080] Based on the above processing, the number of characters contained in the target key of the first key-value pair is less than the number of characters contained in the original key. The original key of each first key-value pair is replaced with the target key of each first key-value pair to obtain the target data. The target data is less than the original data, and thus, storage space can be saved when storing the target data. Attached Figure Description
[0081] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0082] Figure 1 This is a flowchart of the first data compression method provided in the embodiments of the present invention;
[0083] Figure 2 This is a flowchart of the second data compression method provided in this embodiment of the invention;
[0084] Figure 3 This is a flowchart of the third data compression method provided in this embodiment of the invention;
[0085] Figure 4 This is a flowchart of the fourth data compression method provided in this embodiment of the invention;
[0086] Figure 5 This is a flowchart of the fifth data compression method provided in this embodiment of the invention;
[0087] Figure 6 This is a flowchart of the first data decompression method provided in this embodiment of the invention;
[0088] Figure 7 This is a flowchart of the second data decompression method provided in this embodiment of the invention;
[0089] Figure 8 This is a flowchart of the third data decompression method provided in this embodiment of the invention;
[0090] Figure 9 This is a flowchart of the fourth data decompression method provided in this embodiment of the invention;
[0091] Figure 10 This is a flowchart of a data processing method provided in an embodiment of the present invention;
[0092] Figure 11 This is a structural diagram of a data compression device provided in an embodiment of the present invention;
[0093] Figure 12 This is a structural diagram of a data decompression device provided in an embodiment of the present invention;
[0094] Figure 13 This is a structural diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0095] The technical solutions of the present invention will now be described with reference to the accompanying drawings in the embodiments of the present invention.
[0096] In related technologies, when storing data, Java objects are serialized to obtain JSON formatted data, which is then stored in a database. The Java objects and the JSON data contain the same key-value pairs. For example, a Java object recording user A's information might look like this:
[0097]
[0098]
[0099] For example, the key-value pairs in the above Java objects include:
[0100] name:"A", this key-value pair has the key "name" and the value "A";
[0101] age:18, the key of this key-value pair is age, and the value is 18;
[0102] The key-value pair `parents:{father:"B", mother:"C"}` has the key `parents` and the key `father:"B"` and key `mother:"C"`. Both `father:"B"` and `mother:"C"` are key-value pairs. The key `father:"B"` has the key `father` and the value `B`, while the key `mother:"C"` has the key `mother` and the value `C`.
[0103] hobbies:["123","456","789"], where the key of this key-value pair is hobbies and the value is ["123","456","789"].
[0104] Then, the above Java object is serialized to obtain JSON formatted data as follows:
[0105] {"name":"A","age":18,"parents":{"father":"B","mother":"C"},"hobbies":["123","456","789"]} / / {Name: A, Age: 18, Parents: {Father: B, Mother: C}, Hobbies: [123,456,789]}.
[0106] However, in JSON format data, the keys in each key-value pair come directly from the original Java object. Therefore, the keys in each key-value pair contain a lot of characters, such as "parents", "father", "mother", "hobbies", etc., which will result in a large storage space being occupied when storing JSON format data.
[0107] To address the aforementioned problems, this invention provides a data compression method applied to an electronic device, which can be a terminal or a server. The method involves: acquiring original data in JSON format containing multiple key-value pairs; each key-value pair containing an original key and a corresponding key-value pair; acquiring the index value corresponding to the original key of each key-value pair; the index value corresponding to an original key representing the position of the key-value pair to which the original key belongs within the original data; for each first key-value pair in the original data, generating a target field containing preset characters and the index value corresponding to the original key in that first key-value pair; if the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, determining the target field as the target key of that first key-value pair; replacing the original key of each first key-value pair with the target key of that first key-value pair to obtain target data. Since the target data is smaller than the original data, storing the target data saves storage space. Furthermore, the compressed target data is also in JSON format, without changing the format of the original data.
[0108] See Figure 1 , Figure 1 A flowchart of a data compression method provided in an embodiment of the present invention, the method may include the following steps:
[0109] S101: Retrieve raw data in JSON format.
[0110] The original data contains multiple key-value pairs; each key-value pair contains the original key and the corresponding key-value pair.
[0111] S102: Get the index value corresponding to the original key of multiple key-value pairs.
[0112] The index value corresponding to a raw key indicates the position of the key-value pair to which the raw key belongs in the raw data.
[0113] S103: For each first key-value pair in the original data, generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair.
[0114] S104: If the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, determine the target field as the target key of the first key-value pair.
[0115] S105: Replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data.
[0116] Based on the data compression method provided in this embodiment of the invention, if the number of characters contained in the target key of the first key-value pair is less than the number of characters contained in the original key, the original key of each first key-value pair is replaced with the target key of each first key-value pair to obtain the target data. The target data is less than the original data, and thus, storage space can be saved when storing the target data.
[0117] Regarding step S101, the raw data in JSON format is obtained by serializing a Java object. The raw data contains multiple key-value pairs, which are identical to the key-value pairs contained in the corresponding Java objects. In a key-value pair, the key (i.e., the raw key) represents the property name of the Java object, and the value (i.e., the key value) represents the property value of the Java object.
[0118] For example, the original data in JSON format is:
[0119] {"name":"A","age":18,"parents":{"father":"B","mother":"C"},"hobbies":["123","456","789"]} / / {Name: A, Age: 18, Parents: {Father: B, Mother: C}, Hobbies: [123,456,789]}.
[0120] The original data mentioned above contains the following key-value pairs:
[0121] "name":"A", the original key of this key-value pair is name, and the value is A;
[0122] "age":18, the original key of this key-value pair is age, and the value is 18;
[0123] The key-value pair "parents":{"father":"B","mother":"C"} has the original key "parents" and the values "father":"B" and "mother":"C"; "father":"B" and "mother":"C" are also key-value pairs, with the original key "father" being "father" and the value "B" for "father":"B" and the original key "mother" being "mother" and the value "C" for "mother":"C".
[0124] "hobbies":["123","456","789"], the original key of this key-value pair is "hobbies" and the value is ["123","456","789"].
[0125] In one implementation of step S102, for each key-value pair in the original data, the electronic device obtains the sequence number representing the position of the key-value pair in the original data, which is used as the index value corresponding to the original key of the key-value pair.
[0126] In another implementation, Figure 1 Based on this, see Figure 2 Step S102 may include the following steps:
[0127] S1021: Retrieve the configuration file of the Java object corresponding to the raw data.
[0128] The original data contains key-value pairs that are identical to those in the Java objects; the configuration file contains the original keys of multiple key-value pairs in the Java objects.
[0129] S1022: Perform reflection processing on the configuration file to obtain the original keys of each key-value pair in the original data.
[0130] S1023: For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0131] Since the original data is obtained by serializing Java objects, the key-value pairs in the original data are the same as those in the Java objects. The configuration file is the configuration file for the Java object corresponding to the original data. The configuration file is a Java class file, containing the original keys of each key-value pair in the Java object, but not the individual values of those pairs. In other words, the configuration file contains the original keys of each key-value pair in the original data, but not the individual values. For example, for the Java object in the above embodiment, the configuration file for that Java object includes the following original keys: name, age, parents, father, mother, hobbies.
[0132] The electronic device can perform Java reflection on the configuration file to obtain a list of fields representing the raw keys of each key-value pair in the original data. Then, for each raw key in the field list, the electronic device can determine the index value representing the position of that raw key in the corresponding field list of the configuration file. Since the configuration file contains the raw keys of each key-value pair in the original data, the position of that raw key in the field list of the configuration file is the same as the position of the key-value pair to which that raw key belongs in the original data. Therefore, the index value can represent the position of the key-value pair to which that raw key belongs in the original data.
[0133] For example, in the above embodiment, the Java object recording user A's information is processed using Java reflection, resulting in the following field list: name, age, parents, father, mother, hobbies. Then, the electronic device generates an encoded dictionary containing the index values corresponding to each original key based on this field list. See Table 1, which provides an encoded dictionary according to an embodiment of the present invention.
[0134] Table 1
[0135] primitive key Index value name 1 age 2 parents 3 father 4 mother 5 hobbies 6
[0136] For step S103, the first key-value pair can be all key-value pairs in the original data. Alternatively, the first key-value pair can also be a subset of key-value pairs in the original data.
[0137] In some embodiments, Figure 1 Based on this, see Figure 3 Before step S103, the method may further include the following steps:
[0138] S106: Retrieves the data type of the original key for each key-value pair in the original data.
[0139] S107: For each key-value pair in the original data, if the original key of the key-value pair is of the first data type, determine that the key-value pair is the first key-value pair.
[0140] Wherein; the first data type is an object type; a primitive key being of the first data type means that the key value corresponding to the primitive key contains a second key-value pair.
[0141] S108: If the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified value, then the key-value pair is determined to be the first key-value pair.
[0142] The second data type is an integer.
[0143] The data types of the original keys in the original data include object types and integer types. Integer types include int (an identifier used to define integer type variables) and long (an identifier used to represent long integer data).
[0144] In one implementation, the electronic device obtains the configuration file of the Java object corresponding to the original data, performs Java reflection processing on the configuration file, and obtains a list of fields containing the original keys of each key-value pair in the original data. Then, for each original key in the field list, the electronic device can determine the data type of the original key, thus obtaining the data type of the original key of each key-value pair in the original data.
[0145] For each key-value pair in the original data, if the original key of the key-value pair is of type object, then the key-value pair is determined to be the first key-value pair. A primary key being of the first data type indicates that the key-value pair corresponding to that primary key contains a second key-value pair.
[0146] For example, in the above embodiment, the original key (i.e., parents) of the key-value pair "parents":{"father":"B", "mother":"C"} is an object type, and the key values of this key-value pair include the key-value pair "father":"B" and the key-value pair "mother":"C". That is, "father":"B" and "mother":"C" are the second key-value pair.
[0147] The second data type is an integer type, such as int and long, with a specified value of 0.
[0148] For each key-value pair in the original data, if the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified value, for example, if the original key of a key-value pair is of type int and the key value of the key-value pair is not 0, it indicates that the key-value pair records meaningful information, and the electronic device can determine that the key-value pair is the first key-value pair.
[0149] In some embodiments, after step S106, the method may further include the following step: if the original key of the key-value pair is a second data type and the key value of the key-value pair is a specified value, discard the key-value pair.
[0150] If the original key of the key-value pair is a second data type and the key value of the key-value pair is a specified value, for example, if the original key of a key-value pair is of type int and the key value of the key-value pair is 0, it means that even if the original data does not contain the key value corresponding to the original key, the key value corresponding to the original key will be determined to be a specified value when the original data is deserialized. In this case, the key-value pair records meaningless information, and the electronic device can discard the key-value pair, which can reduce the size of the target data obtained by subsequent compression and save storage space.
[0151] In some embodiments, Figure 3 Based on this, see Figure 4 After steps S107 and S108, the method may further include the following steps:
[0152] S109: For each first key-value pair in the original data, if the key value of the first key-value pair contains a second key-value pair, obtain the data type of the original key of the second key-value pair.
[0153] S110: If the original key of the second key-value pair is of the first data type, determine that the second key-value pair is the first key-value pair.
[0154] S111: If the original key of the second key-value pair is a second data type and the key value of the second key-value pair is not a specified value, then the second key-value pair is determined to be the first key-value pair.
[0155] For each first key-value pair in the original data, the electronic device can determine whether the key value of the first key-value pair contains a key-value pair (i.e., a second key-value pair). If the key value of the first key-value pair contains a second key-value pair, then the data type of the original key of the second key-value pair is obtained.
[0156] For example, in the above embodiment, the original key (i.e., parents) of the key-value pair "parents":{"father":"B", "mother":"C"} is an object type, and the key values of the key-value pair include the second key-value pair "father":"B" and the second key-value pair "mother":"C".
[0157] If the original key of the second key-value pair is of the first data type, then the second key-value pair is determined to be the first key-value pair. If the original key of the second key-value pair is of the second data type, and the key value of the second key-value pair is not a specified numerical value, for example, if the original key of a second key-value pair is of type long, and the key value of the second key-value pair is not 0, it indicates that the second key-value pair records meaningful information, and thus the second key-value pair is determined to be the first key-value pair.
[0158] In some embodiments, if the original key of the second key-value pair is a second data type and the key value of the second key-value pair is a specified numerical value, for example, if the original key of a second key-value pair is of type long and the key value of the second key-value pair is 0, indicating that the second key-value pair records meaningless information, the electronic device can discard the second key-value pair, which can reduce the size of the target data obtained by subsequent compression and save storage space.
[0159] For each first key-value pair, the electronic device can obtain the index value (which can be called the target index value) corresponding to the original key of the first key-value pair. For example, in the above embodiment, the target index value corresponding to the original key (i.e., name) in the key-value pair "name":"A" is 1; the target index value corresponding to the original key (i.e., age) in the key-value pair "age":"18" is 2.
[0160] The preset characters can be set by technicians according to actual needs. For example, the preset character can be "#", or it can be "+", or it can be "*", etc., but it is not limited to these.
[0161] For each first key-value pair, after obtaining the target index value corresponding to the original key of the first key-value pair, the electronic device can generate a field (i.e., the target field) containing preset characters and the target index value.
[0162] Regarding step S104, for each first key-value pair, the target index value corresponding to the original key of the first key-value pair represents the position of the first key-value pair in the original data. If the original data contains a large number of key-value pairs, the target index value corresponding to the original key of the first key-value pair may contain more characters, resulting in the target field corresponding to the first key-value pair containing more characters. That is, the number of characters contained in the target field may be greater than the number of characters contained in the original key of the first key-value pair.
[0163] For example, the original data contains 200 key-value pairs. The last key-value pair in the original data is "age":"18". The target index value corresponding to the original key of the last key-value pair in the original data is 200, the default character is "#", and the target field corresponding to the last key-value pair is #200. The number of characters contained in the target field is greater than the number of characters contained in the original key of the first key-value pair.
[0164] To reduce the number of characters in the target data obtained from compression, for each first key-value pair, the electronic device can compare the original key of the first key-value pair with the target field. If the number of characters in the target field corresponding to the first key-value pair is less than the number of characters in the original key of the first key-value pair, the target field is determined to be the target key of the first key-value pair.
[0165] Regarding step S105, in one implementation, after obtaining the target key of each first key-value pair, the electronic device can replace the original key of each first key-value pair with the target key of each first key-value pair in the original data to obtain the target data.
[0166] In another implementation, the electronic device replaces the original key of each first key-value pair with the target key of each first key-value pair, obtaining the replaced first key-value pairs. Then, the electronic device can create a blank JSON data file, write the replaced first key-value pairs into this blank JSON data file, and write the key-value pairs whose original keys have not been replaced into this blank JSON data file, thus obtaining the target data.
[0167] For example, the preset character is "+". The target data obtained by compressing the original data in the above embodiment is: {"+1":"A","+2":18,"+3":{"+4":"B","+5":"C"},"+6":["123","456","789"]} / / {+1:A,+2:18,+3:{+4:B,+5:C},+6:[123,456,789]}.
[0168] See Figure 5 , Figure 5 A flowchart of a data compression method provided in an embodiment of the present invention, the method comprising the following steps:
[0169] S501: Construct the encoding dictionary and type dictionary.
[0170] In this step, the electronic device can obtain the configuration file of the Java object corresponding to the raw data and generate an encoding dictionary and a type dictionary for the raw data based on the configuration file. The encoding dictionary contains the index value corresponding to the raw key of each key-value pair in the raw data. The index value corresponding to a raw key indicates the position of the key-value pair to which the raw key belongs in the raw data. The type dictionary contains the data type of the raw key of each key-value pair in the raw data.
[0171] S502: Create a new blank JSON.
[0172] In this step, the electronic device can create blank JSON data, which is blank data in JSON format that does not contain key-value pairs.
[0173] S503: Iterate through the key-value pairs of JSON data.
[0174] In this step, the JSON data is the original data from the aforementioned embodiments, and the key-value pairs in the JSON data are the multiple key-value pairs in the original data. The electronic device can iterate through the multiple key-value pairs in the original data, and process each key-value pair according to the data compression method provided in this embodiment of the invention.
[0175] S504. Determine if the key-value pair is an object. If it is, proceed to step S503. If not, proceed to step S505.
[0176] In this step, for each key-value pair in the original data, the electronic device can obtain the data type of the original key of the key-value pair from the type dictionary, and determine whether the data type of the original key of the key-value pair is an object type (i.e., the first data type in the aforementioned embodiment). If the original key of the key-value pair is an object type, then the key-value pair is determined to be the first key-value pair.
[0177] The original key of the key-value pair is an object type, indicating that the key value of the key-value pair contains a second key-value pair. The electronic device can traverse each second key-value pair contained in the key value of the key-value pair. For each second key-value pair, the electronic device can obtain the data type of the original key of the second key-value pair from the type dictionary and determine whether the data type of the original key of the second key-value pair is an object type. If the original key of the second key-value pair is an object type, then the second key-value pair is determined to be the first key-value pair.
[0178] S505: Determine whether to discard. If yes, proceed to step S510; otherwise, proceed to step S506.
[0179] In this step, for each key-value pair in the original data, after the electronic device obtains the data type of the original key of the key-value pair from the type dictionary, if it determines that the data type of the original key of the key-value pair is not an object type, the electronic device determines whether to discard the key-value pair. That is, it determines whether the type of the original key of the key-value pair is a second data type and whether the key value of the key-value pair is a specified value, and performs corresponding processing based on the determination result.
[0180] If the original key of the key-value pair is of type second data type and the key value of the key-value pair is not a specified numeric value, then the key-value pair is determined to be a first key-value pair. If the original key of the key-value pair is of type second data type and the key value of the key-value pair is a specified numeric value, then the key-value pair is discarded.
[0181] S506: Obtain the index value of the key from the encoded dictionary.
[0182] In this step, for each first key-value pair, the electronic device obtains the index value of the original key of the first key-value pair from the encoding dictionary, obtains the target index value of the original key of the first key-value pair, and generates a target field containing preset characters and target index values.
[0183] S507: Determine if it exists. If yes, proceed to step S508; otherwise, proceed to step S509.
[0184] In this step, for each first key-value pair, the electronic device determines whether the target key of the first key-value pair exists, that is, by comparing the number of characters in the target field of the first key-value pair with the number of characters in the original key of the first key-value pair.
[0185] S508: Construct a new key.
[0186] In this step, for each first key-value pair, if the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, the electronic device constructs a new key for the first key-value pair, that is, determines the target field as the target key of the first key-value pair.
[0187] S509: Insert new JSON.
[0188] In this step, for each first key-value pair, if the number of characters in the target field of the first key-value pair is not less than the number of characters in the original key of the first key-value pair, then the first key-value pair is directly written into blank data in JSON format.
[0189] After obtaining the target key for each first key-value pair, the electronic device can use the target key to replace the original key of the first key-value pair, obtaining the replaced first key-value pairs. Then, the replaced first key-value pairs are written into blank data in JSON format.
[0190] S510: Determine whether the traversal is complete. If yes, proceed to step S511; otherwise, proceed to step S503.
[0191] In this step, the electronic device determines whether all key-value pairs in the original data have been traversed. If not, meaning that not all key-value pairs in the original data have been compressed, the electronic device continues to process the uncompressed key-value pairs.
[0192] S511: Returns a new JSON.
[0193] In this step, if the electronic device determines that it has traversed all key-value pairs in the original data, that is, has compressed all key-value pairs in the original data, then the corresponding target data is obtained.
[0194] Based on the above processing, since the number of characters in the target key of each first key-value pair is less than the number of characters in the original key, replacing the original key of each first key-value pair with its target key yields the target data. The target data is smaller than the original data, and the compression ratio of the original data can reach 30%. Therefore, storing the target data saves storage space. Furthermore, by creating an encoding dictionary based on the configuration file corresponding to the original data, lossless compression of the original data can be achieved. There is no need to store the encoding dictionary in the JSON-formatted original data, and the compressed target data is also in JSON format, without changing the format of the original data.
[0195] See Figure 6 , Figure 6 This is a flowchart of a data decompression method provided in an embodiment of the present invention.
[0196] S601: Retrieves target data in JSON format.
[0197] The target data is obtained based on the data compression method in the aforementioned embodiments; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair.
[0198] S602: For each key-value pair in the target data, if the target key of the key-value pair contains a preset character, determine that the key-value pair as the first key-value pair.
[0199] S603: For each first key-value pair, obtain the original key corresponding to the index value in the target key of the first key-value pair, and obtain the original key of the first key-value pair.
[0200] S604: Replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data.
[0201] Based on the data decompression method provided in this invention, the target data is obtained by compressing the original data. Storing the target data can save storage space. Furthermore, the compressed target data can be decompressed to obtain the corresponding original data. Subsequently, users can obtain the original data, which can improve the user experience.
[0202] For step S601, the target data is obtained by compressing the original data based on the data compression method in the aforementioned embodiment. The target data contains multiple key-value pairs, and each key-value pair contains a target key and a corresponding key value.
[0203] Regarding step S602, since a target key is generated based on the index value corresponding to the original key of the key-value pair in the original data and a preset character when compressing the original data, for each key-value pair in the target data, if the target key of the key-value pair contains a preset character, it indicates that the target key of the key-value pair is obtained by compression, and the electronic device can determine that the key-value pair is the first key-value pair.
[0204] For step S603, for each first key-value pair, the electronic device obtains the original key corresponding to the index value in the target key of the first key-value pair, and can obtain the original key of the first key-value pair.
[0205] In some embodiments, Figure 6 Based on this, see Figure 7 Before step S603, the method may further include the following steps:
[0206] S605: Retrieves the configuration file of the Java object corresponding to the target data.
[0207] The configuration file contains the original keys of multiple key-value pairs in the Java object.
[0208] S606: Perform reflection processing on the configuration file to obtain the original keys of multiple key-value pairs in the Java object.
[0209] S607: For each original key, determine the sequence number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0210] The Java object corresponding to the target data is the same as the Java object corresponding to the original data. The configuration file for the Java object corresponding to the target data is also the configuration file for the Java object corresponding to the original data. The configuration file contains the primitive keys of each key-value pair in the Java object, but does not contain the individual values of those pairs. In other words, the configuration file contains the primitive keys of each key-value pair in the original data, but not the individual values of those pairs.
[0211] The electronic device can perform Java reflection on the configuration file to obtain a list of fields containing the original keys of each key-value pair in the original data. Then, for each original key in the field list, the electronic device can determine the index value representing the position of that original key within the list. Correspondingly, for each key-value pair in the target data, the target key is generated based on the corresponding original key and index value in the original data, thus allowing the electronic device to obtain the original key corresponding to the index value of the target key for multiple key-value pairs in the target data.
[0212] For example, in the above embodiment, the configuration file of the Java object recording user A's information is processed using Java reflection, resulting in the following field list: name, age, parents, father, mother, hobbies. Then, the electronic device generates a decoding dictionary containing the original keys corresponding to the index values of each target key based on this field list. See Table 2, which provides an encoding dictionary according to an embodiment of the present invention.
[0213] Table 2
[0214] Index value in the target key primitive key 1 name 2 age 3 parents 4 father 5 mother 6 hobbies
[0215] The target data obtained by compressing the original data is:
[0216] {"+1":"A","+2":18,"+3":{"+4":"B","+5":"C"},"+6":["123","456","789"]} / / {+1:A,+2:18,+3:{+4:B,+5:C},+6:[123,456,789]}.
[0217] For the first key-value pair "+1":"A", the electronic device retrieves the original key corresponding to the index value (i.e., 1) of the target key (i.e., +1) of the first key-value pair from Table 2, which is name. In other words, the original key of the first key-value pair is name. For the first key-value pair "+2":"18", the electronic device retrieves the original key corresponding to the index value (i.e., 2) of the target key (i.e., +2) of the first key-value pair from Table 2, which is age. In other words, the original key of the first key-value pair is age.
[0218] In some embodiments, since a target key is generated based on the index value corresponding to the original key of the key-value pair in the original data and a preset character when compressing the original data, if the target key of each key-value pair in the target data does not contain the preset character, it indicates that the original key of the key value was not compressed when compressing the original data. That is, the target key of the key-value pair is the original key of the key-value pair. Therefore, the electronic device can determine that the target key of the key-value pair will not be processed.
[0219] In some embodiments, prior to step S603, the method may further include the following steps:
[0220] For each key-value pair in the target data, if the key value of the key-value pair contains a second key-value pair, and the target key of the second key-value pair contains a preset character, then the second key-value pair is determined to be the first key-value pair.
[0221] For each key-value pair in the target data, if the key value of the key-value pair contains a second key-value pair, then the second key-value pair contained in the key value of the key-value pair may be compressed. For each second key-value pair, if the target key of the second key-value pair contains a preset character, it indicates that the target key of the second key-value pair is compressed. The electronic device can determine that the second key-value pair is the first key-value pair, and determine the original key corresponding to the target key of the first key-value pair in accordance with the method in the aforementioned embodiment.
[0222] If the target key of the second key-value pair does not contain a preset character, it indicates that the original key of the second key-value pair was not compressed when the original data was compressed. In other words, the target key of the second key-value pair is the original key of the second key-value pair. Therefore, the electronic device can determine that the target key of the second key-value pair will not be processed.
[0223] Regarding step S604, in one implementation, after obtaining the original keys of each first key-value pair, the electronic device can replace the target keys of each first key-value pair with the original keys of each first key-value pair in the target data to obtain the original data.
[0224] In another implementation, the electronic device replaces the target key of each first key-value pair with the original key of the first key-value pair, obtaining the replaced first key-value pair. Then, the electronic device can create a blank JSON data file, write the replaced first key-value pairs into this blank JSON data file, and write the key-value pairs whose original keys have not been replaced into this blank JSON data file, obtaining the original data.
[0225] For example, the target data is: {"+1":"A","+2":18,"+3":{"+4":"B","+5":"C"},"+6":["123","456","789"]} / / {+1:A,+2:18,+3:{+4:B,+5:C},+6:[123,456,789]}.
[0226] The original data obtained by decompressing the target data according to the decoding dictionary shown in Table 2 is as follows:
[0227] {"name":"A","age":18,"parents":{"father":"B","mother":"C"},"hobbies":["123","456","789"]} / / {Name: A, Age: 18, Parents: {Father: B, Mother: C}, Hobbies: [123,456,789]}.
[0228] In some embodiments, after obtaining the original data, the original data can be deserialized to obtain the corresponding Java object. Accordingly, in... Figure 6 Based on this, see Figure 8 After step S604, the method may further include the following steps:
[0229] S608: For each raw key in the configuration file, retrieve the key-value pair containing that raw key from the raw data.
[0230] S609: If there is no key-value pair containing the original key in the original data, determine the key value corresponding to the original key as the specified value to obtain a key-value pair containing the original key.
[0231] S610: Write each key-value pair to a pre-created blank Java object to obtain the Java object corresponding to the target data.
[0232] Serializing a Java object yields data in a convenient JSON format, which is still needed to retrieve the Java object later. Therefore, after decompressing the original data, it can be deserialized to obtain the corresponding Java object.
[0233] In one implementation, for each raw key in the configuration file of a Java object, the electronic device can retrieve the key-value pair containing that raw key from the decompressed raw data. If the raw data does not contain a key-value pair containing that raw key, it indicates that the key-value pair containing that raw key was discarded during the compression of the raw data. Since key-value pairs where the key is a second data type and the key value is a specified numerical value are discarded during the compression of the raw data, the electronic device can determine the key value corresponding to that raw key as the specified numerical value to obtain the key-value pair containing that raw key.
[0234] For example, if a primitive key is of type int, the electronic device determines that the key value corresponding to the primitive key is 0, and obtains a key-value pair containing the primitive key; or, if a primitive key is of type long, the electronic device determines that the key value corresponding to the primitive key is 0, and obtains a key-value pair containing the primitive key.
[0235] Then, the electronic device writes each key-value pair containing the original keys into a pre-created blank Java object, thus obtaining the Java object corresponding to the target data.
[0236] See Figure 9 , Figure 9 A flowchart of a data decompression method provided in an embodiment of the present invention, the method may include the following steps:
[0237] S901: Construct the decoding dictionary.
[0238] In this step, the electronic device acquires the target data in JSON format and the configuration file of the corresponding Java object, and generates a decoding dictionary for the target data based on the configuration file. The decoding dictionary contains the original key corresponding to the index value of the target key in each key-value pair in the target data.
[0239] S902: Create a new blank JSON.
[0240] In this step, the electronic device can create blank JSON data, which is blank data in JSON format that does not contain key-value pairs.
[0241] S903: Traverse the key-value pairs in JSON.
[0242] In this step, the JSON data is the target data from the aforementioned embodiments, and the key-value pairs in the JSON data are the multiple key-value pairs in the target data. The electronic device can iterate through the multiple key-value pairs in the target data, and for each key-value pair, it processes it according to the data decompression method provided in the embodiments of the present invention.
[0243] S904: Determine if the key-value pair is an object. If it is, proceed to step S903; otherwise, proceed to step S905.
[0244] In this step, for each key-value pair, it is determined whether the target key of the key-value pair is an object type. If the target key of the key-value pair is an object type, it means that the key value of the key-value pair contains a second key-value pair. The electronic device can traverse the second key-value pairs contained in the key-value pair. For each second key-value pair, it is processed according to the data decompression method provided in the embodiment of the present invention.
[0245] S905: Obtain the index value of the key from the decoding dictionary.
[0246] In this step, for each key-value pair in the target data, the index value of the target key in the target key-value pair is obtained from the decoding dictionary of the target data.
[0247] S906: Determine if it is compressed. If yes, proceed to step S907; otherwise, proceed to step S908.
[0248] For each key-value pair in the target data, if the target key of the key-value pair contains a preset character, the key-value pair is determined to be the first key-value pair, that is, the target key of the key-value pair is obtained by compression, i.e., the key-value pair is the first key-value pair.
[0249] S907: Construct a new key.
[0250] In this step, for each first key-value pair, the electronic device constructs a new key for the first key-value pair, that is, it obtains the original key corresponding to the index value in the target key of the first key-value pair from the decoding dictionary of the target data, and replaces the target key of the first key-value pair with the original key of the first key-value pair to obtain the replaced first key-value pairs.
[0251] S908: Insert new JSON.
[0252] In this step, for each key-value pair, if the target key of the key-value pair does not contain a preset character, it indicates that the target key of the key-value pair is not obtained through compression. The electronic device can determine that the target key of the key-value pair is the original key of the first key-value pair, and the electronic device can write the key-value pair into blank data in JSON format.
[0253] For each first key-value pair, the electronic device replaces the target key of the first key-value pair with the original key of the first key-value pair, obtaining the replaced first key-value pairs. Then, the replaced first key-value pairs are written into blank data in JSON format.
[0254] S909: Determine whether the traversal is complete. If yes, proceed to step S910; otherwise, proceed to step S903.
[0255] In this step, the electronic device determines whether all key-value pairs in the target data have been traversed. If not, that is, if all key-value pairs in the target data have not been decompressed, the electronic device continues to process the undecompressed key-value pairs in the target data.
[0256] S910: Returns a new JSON.
[0257] In this step, if the electronic device determines that it has traversed all key-value pairs in the target data, that is, has decompressed all key-value pairs in the target data, then the corresponding original data is obtained.
[0258] Based on the above processing, the target data is obtained by compressing the original data. Storing the target data saves storage space. Furthermore, the compressed target data can be decompressed to obtain the corresponding original data. Subsequently, users can retrieve the original data, which improves the user experience.
[0259] See Figure 10 , Figure 10 A flowchart of a data processing method provided in an embodiment of the present invention.
[0260] The electronic device acquires a Java object used to record business information and serializes the Java object to obtain the raw data in JSON format (i.e., Figure 10(JSON in the original data). Furthermore, the electronic device compresses the original data according to the data compression method provided in this embodiment of the invention to obtain target data in JSON format (i.e., JSON). Figure 10 (small JSON in the file). Figure 10 The compression process in this invention refers to the process by which the electronic device compresses the original data according to the data compression method provided in this embodiment. Furthermore, the electronic device can store the compressed target data in a database, which can be a NoSQL database.
[0261] Subsequently, when the electronic device needs to retrieve a Java object used to record business information, it obtains the target data in JSON format from the database (i.e., Figure 10 (The small JSON in the middle). Electronic devices can decompress the target data according to the data decompression method provided in the embodiments of the present invention to obtain the original data in JSON format (i.e., Figure 10 (JSON in the text). Figure 10 The decompression process refers to the process by which the electronic device decompresses the target data according to the data decompression method provided in this embodiment of the invention. Furthermore, the electronic device can deserialize the target data to obtain a Java object containing business information.
[0262] Based on the above processing, for the first key-value pair in the original data, a target key can be generated based on the index value corresponding to the original key of the first key-value pair and a preset character. The number of characters contained in the target key is less than the number of characters contained in the original key of the first key-value pair. The original key of each first key-value pair is replaced with the target key of each first key-value pair to obtain the target data. The target data is smaller than the original data. Therefore, storing the target data can save storage space.
[0263] and Figure 1 For the corresponding method implementation examples, see [link to relevant documentation]. Figure 11 , Figure 11 This is a structural diagram of a data compression device provided in an embodiment of the present invention. The device includes:
[0264] The first acquisition module 1101 is used to acquire raw data in JSON format; wherein the raw data contains multiple key-value pairs; each key-value pair contains an original key and a corresponding key-value pair.
[0265] The second acquisition module 1102 is used to acquire the index value corresponding to the original key of the plurality of key-value pairs; wherein, the index value corresponding to an original key indicates the position of the key-value pair to which the original key belongs in the original data;
[0266] The first generation module 1103 is used to generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data.
[0267] The second generation module 1104 is used to determine the target field as the target key of the first key-value pair if the number of characters in the target field is less than the number of characters in the original key of the first key-value pair.
[0268] Replacement module 1105 is used to replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data.
[0269] Optionally, the device further includes:
[0270] The third acquisition module is used to acquire the data type of the original key of each key-value pair in the original data before the first generation module 1103 generates a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data.
[0271] The first determining module is used to determine that for each key-value pair in the original data, if the original key of the key-value pair is a first data type, the key-value pair is a first key-value pair, wherein; the first data type is an object type; a first data type for an original key indicates that the key value corresponding to the original key contains a second key-value pair;
[0272] The second determining module is used to determine the key-value pair as the first key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified numerical value; wherein the second data type is an integer type.
[0273] Optionally, the device further includes:
[0274] The discard module is configured to, after the third acquisition module has acquired the data type of the original key of each key-value pair in the original data, discard the key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is the specified value.
[0275] Optionally, the device further includes:
[0276] The fourth acquisition module is used to, after the first determination module determines that the key-value pair is a first key-value pair if the original key of the key-value pair is a first data type, execute the following steps for each first key-value pair in the original data: if the key value of the first key-value pair contains a second key-value pair, acquire the data type of the original key of the second key-value pair.
[0277] The third determining module is used to determine that the second key-value pair is the first key-value pair if the original key of the second key-value pair is the first data type;
[0278] The fourth determining module is used to determine the second key-value pair as the first key-value pair if the original key of the second key-value pair is a second data type and the key value of the second key-value pair is not the specified value.
[0279] Optionally, the second acquisition module 1102 is specifically used to acquire the configuration file of the Java object corresponding to the original data; wherein, each key-value pair contained in the original data is the same as each key-value pair contained in the Java object; the configuration file contains the original keys of each of the multiple key-value pairs in the Java object;
[0280] The configuration file is subjected to reflection processing to obtain the original keys of each key-value pair in the original data;
[0281] For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
[0282] Based on the data compression device provided in this embodiment of the invention, if the number of characters contained in the target key of the first key-value pair is less than the number of characters contained in the original key, the original key of each first key-value pair is replaced with the target key of each first key-value pair to obtain target data. The target data is less than the original data, and thus, storage space can be saved when storing the target data.
[0283] and Figure 6 For the corresponding method implementation examples, see [link to relevant documentation]. Figure 12 , Figure 12 This is a structural diagram of a data decompression device provided in an embodiment of the present invention. The device includes:
[0284] The first acquisition module 1201 is used to acquire target data in JSON format; wherein the target data is obtained based on any of the data compression methods described above; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair;
[0285] The first determining module 1202 is used to determine the key-value pair as the first key-value pair if the target key of the key-value pair contains a preset character for each key-value pair in the target data.
[0286] The second acquisition module 1203 is used to acquire the original key corresponding to the index value in the target key of each first key-value pair, and obtain the original key of the first key-value pair.
[0287] Replacement module 1204 is used to replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data.
[0288] Optionally, the device further includes:
[0289] The second determining module is used to, before the second obtaining module 1203 performs the process of obtaining the original key corresponding to the index value in the target key of each first key-value pair and obtaining the original key of the first key-value pair, perform the process of obtaining the original key of the first key-value pair for each key-value pair in the target data, and if the key value of the key-value pair contains a second key-value pair and the target key of the second key-value pair contains a preset character, determine the second key-value pair as the first key-value pair.
[0290] Optionally, the device further includes:
[0291] The third acquisition module is used to acquire the configuration file of the Java object corresponding to the target data before the second acquisition module 1203 executes the process of acquiring the original key corresponding to the index value in the target key of each first key-value pair to obtain the original key of the first key-value pair; wherein, the configuration file contains the original keys of each of the multiple key-value pairs in the Java object;
[0292] The third determining module is used to perform reflection processing on the configuration file to obtain the original keys of multiple key-value pairs in the Java object.
[0293] The fourth determining module is used to determine the sequence number representing the position of each original key in the configuration file, and obtain the index value corresponding to the original key.
[0294] Optionally, the device further includes:
[0295] The fourth acquisition module is used to replace the target key of each first key-value pair with the original key of each first key-value pair after the replacement module 1204 performs the operation of replacing the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data, and then, for each original key in the configuration file, to obtain the key-value pair containing the original key from the original data.
[0296] The fifth determining module is used to determine the key value corresponding to the original key as a specified value if there is no key-value pair containing the original key in the original data, so as to obtain a key-value pair containing the original key.
[0297] The generation module is used to write each key-value pair into a pre-created blank Java object to obtain the Java object corresponding to the target data.
[0298] Based on the data decompression apparatus provided in this embodiment of the invention, the target data is obtained by compressing the original data. Storing the target data can save storage space. Furthermore, the compressed target data can be decompressed to obtain the corresponding original data. Subsequently, users can obtain the original data, which can improve the user experience.
[0299] This invention also provides an electronic device, such as... Figure 13 As shown, it includes a processor 1301, a communication interface 1302, a memory 1303, and a communication bus 1304. The processor 1301, the communication interface 1302, and the memory 1303 communicate with each other through the communication bus 1304.
[0300] Memory 1303 is used to store computer programs;
[0301] When the processor 1301 executes the program stored in the memory 1303, it implements the steps of the data compression method or the data decompression method described in any of the above embodiments.
[0302] The communication bus mentioned in the above electronic devices can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not indicate that there is only one bus or one type of bus.
[0303] The communication interface is used for communication between the aforementioned electronic devices and other devices.
[0304] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0305] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0306] In another embodiment of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the data compression methods or data decompression methods described in the above embodiments.
[0307] In another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the data compression methods or data decompression methods described in the above embodiments.
[0308] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0309] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0310] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, electronic devices, computer-readable storage media, and computer program products are basically similar to the method embodiments, and therefore the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0311] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.
Claims
1. A data compression method characterized by, The method includes: Obtain raw data in JSON format; wherein the raw data contains multiple key-value pairs; each key-value pair contains a raw key and a corresponding key-value pair; Obtain the index value corresponding to the original key of the plurality of key-value pairs; wherein, the index value corresponding to an original key indicates the position of the key-value pair to which the original key belongs in the original data; For each first key-value pair in the original data, generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair; If the number of characters in the target field is less than the number of characters in the original key of the first key-value pair, then the target field is determined to be the target key of the first key-value pair. Replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data; Before generating a target field containing a preset character and an index value corresponding to the original key in each first key-value pair in the original data, the method further includes: Obtain the data type of the original key for each key-value pair in the original data; For each key-value pair in the original data, if the original key of the key-value pair is a first data type, the key-value pair is determined to be a first key-value pair, where the first data type is an object type; a first data type for an original key indicates that the key value corresponding to the original key contains a second key-value pair. If the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified numerical value, then the key-value pair is determined to be a first key-value pair; wherein, the second data type is an integer type. If the original key of the key-value pair is a second data type and the key value of the key-value pair is the specified numerical value, discard the key-value pair.
2. The method of claim 1, wherein, After determining that the key-value pair is a first key-value pair if the original key of the key-value pair is a first data type, the method further includes: For each first key-value pair in the original data, if the key value of the first key-value pair contains a second key-value pair, obtain the data type of the original key of the second key-value pair; If the original key of the second key-value pair is of the first data type, then the second key-value pair is determined to be the first key-value pair. If the original key of the second key-value pair is a second data type and the key value of the second key-value pair is not the specified value, then the second key-value pair is determined to be the first key-value pair.
3. The method according to any one of claims 1 to 2, characterized in that, Obtaining the index value corresponding to the original key of the plurality of key-value pairs includes: Obtain the configuration file of the Java object corresponding to the original data; wherein, each key-value pair contained in the original data is the same as each key-value pair contained in the Java object; the configuration file contains the original keys of each of the multiple key-value pairs in the Java object; The configuration file is subjected to reflection processing to obtain the original keys of each key-value pair in the original data; For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
4. A data decompression method, characterized in that, The method includes: Obtain target data in JSON format; wherein the target data is obtained based on the data compression method according to any one of claims 1 to 3; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair; For each key-value pair in the target data, if the target key of the key-value pair contains a preset character, the key-value pair is determined to be the first key-value pair; For each first key-value pair, obtain the original key corresponding to the index value in the target key of the first key-value pair to obtain the original key of the first key-value pair; Replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data; Before obtaining the original key corresponding to the index value in the target key of each first key-value pair, and thus obtaining the original key of the first key-value pair, the method further includes: For each key-value pair in the target data, if the key value of the key-value pair contains a second key-value pair, and the target key of the second key-value pair contains a preset character, then the second key-value pair is determined to be the first key-value pair.
5. The method according to claim 4, characterized in that, Before obtaining the original key corresponding to the index value in the target key of each first key-value pair, and thus obtaining the original key of the first key-value pair, the method further includes: Obtain the configuration file of the Java object corresponding to the target data; wherein, the configuration file contains the original keys of multiple key-value pairs in the Java object; The configuration file is subjected to reflection processing to obtain the original keys of multiple key-value pairs in the Java object. For each original key, determine the index number representing the position of the original key in the configuration file, and obtain the index value corresponding to the original key.
6. The method according to claim 5, characterized in that, After replacing the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data, the method further includes: For each original key in the configuration file, obtain the key-value pair containing that original key from the original data; If there is no key-value pair containing the original key in the original data, the key value corresponding to the original key is determined to a specified value to obtain a key-value pair containing the original key; Each key-value pair is written to a pre-created blank Java object to obtain the Java object corresponding to the target data.
7. A data compression device, characterized in that, The device includes: The first acquisition module is used to acquire raw data in JSON format; wherein the raw data contains multiple key-value pairs; each key-value pair contains an original key and a corresponding key-value pair; The second acquisition module is used to acquire the index value corresponding to the original key of the plurality of key-value pairs; wherein, the index value corresponding to an original key indicates the position of the key-value pair to which the original key belongs in the original data; The first generation module is used to generate a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data; The second generation module is used to determine that the target field is the target key of the first key-value pair if the number of characters in the target field is less than the number of characters in the original key of the first key-value pair. The replacement module is used to replace the original key of each first key-value pair with the target key of each first key-value pair to obtain the target data; The device further includes: The third acquisition module is used to acquire the data type of the original key of each key-value pair in the original data before the first generation module generates a target field containing a preset character and the index value corresponding to the original key in the first key-value pair for each first key-value pair in the original data; The first determining module is used to determine that for each key-value pair in the original data, if the original key of the key-value pair is a first data type, the key-value pair is a first key-value pair, wherein; the first data type is an object type; a first data type for an original key indicates that the key value corresponding to the original key contains a second key-value pair; The second determining module is used to determine the key-value pair as the first key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is not a specified numerical value; wherein the second data type is an integer type. The device further includes: The discard module is configured to, after the third acquisition module has acquired the data type of the original key of each key-value pair in the original data, discard the key-value pair if the original key of the key-value pair is a second data type and the key value of the key-value pair is the specified value.
8. A data decompression device, characterized in that, The device includes: The first acquisition module is used to acquire target data in JSON format; wherein the target data is obtained based on the data compression method according to any one of claims 1 to 3; the target data contains multiple key-value pairs; each key-value pair contains a target key and a corresponding key-value pair; The first determining module is used to determine that for each key-value pair in the target data, if the target key of the key-value pair contains a preset character, the key-value pair is the first key-value pair. The second acquisition module is used to acquire the original key corresponding to the index value in the target key of each first key-value pair, and obtain the original key of the first key-value pair. The replacement module is used to replace the target key of each first key-value pair with the original key of each first key-value pair to obtain the original data; The device further includes: The second determining module is used to, before the second obtaining module performs the process of obtaining the original key corresponding to the index value in the target key of each first key-value pair and obtaining the original key of the first key-value pair, perform the process of obtaining the original key of the first key-value pair for each key-value pair in the target data, and if the key value of the key-value pair contains a second key-value pair and the target key of the second key-value pair contains a preset character, determine the second key-value pair as the first key-value pair.
9. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the method steps of any one of claims 1-3, or the method steps of any one of claims 4-6.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method steps of any one of claims 1-3, or the method steps of any one of claims 4-6.
Citation Information
Patent Citations
Compression and decompression method and system for key value pair text and operation machine
CN112818081A