Template matching and column storage database characteristic combined log compression method and system
By combining template matching with a columnar storage database, a log compression method is used to split logs into fixed templates and variable parameters. ClickHouse's LowCardinality optimization and LZ4 compression are used to solve the problems of large storage space consumption and query latency in log storage, achieving efficient log compression and controllable query.
Patent Information
- Application Number
- CN202511400984.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-28
- Publication Date
- 2026-01-02
AI Technical Summary
Existing technologies do not fully utilize the templated characteristics of logs in log storage, resulting in large storage space consumption. General compression schemes are not designed for the structured characteristics of logs, resulting in limited improvement in compression ratio and increased query latency. Existing solutions are unbalanced in terms of storage and query trade-offs and do not consider the impact of the number of variable parameters on compression performance.
This log compression method combines template matching with columnar storage database features, splitting the log into a fixed template part and a variable parameter part. It utilizes ClickHouse's LowCardinality optimization and LZ4 compression algorithm for storage, and reassembles the log data during querying.
It achieves efficient log compression and storage, with an average compression rate improvement of 36.3%, storage savings of 26.3%, and query latency reduced to within 3.8 seconds, meeting the real-time requirements for fault diagnosis and performance analysis.
Smart Images

Figure CN121255752A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of data storage and compression technology, specifically a log compression method and system that combines template matching with columnar storage database characteristics. Background Technology
[0002] With the development of information technology, distributed systems are widely used in production environments, resulting in an explosive growth in the amount of log data generated during operation. Log data is a core basis for troubleshooting and other tasks, requiring long-term storage and guaranteed queryability. However, traditional log storage solutions have key problems: columnar storage databases (such as ClickHouse) use the LZ4 compression algorithm by default to directly store complete logs, achieving a compression rate of only 4.9-7.2 for logs from HDFS and other databases, failing to fully utilize the templated characteristics of logs and resulting in large storage space consumption; general compression solutions (such as zstd preprocessing or dictionary compression) do not have adaptation logic designed for the structured characteristics of logs, offering limited improvement in compression rate for highly structured logs and increasing query decompression overhead and query latency; some solutions are unbalanced in their storage and query trade-offs, pursuing high compression rates leads to significantly longer query response times, while pursuing query performance makes it difficult to meet storage cost control requirements; existing solutions do not consider the impact of the number of variable parameters in the log on the compression effect, and when there are too many parameters, forced compression not only fails to improve the compression rate but also increases computational overhead and wastes resources. Therefore, there is an urgent need for a compression solution that combines the characteristics of log templates with the inherent advantages of columnar storage databases to improve compression ratio, save storage space and control query latency, so as to meet the needs of large-scale log storage and analysis. Summary of the Invention
[0003] The purpose of this invention is to provide a log compression method and system that combines template matching with columnar storage database features to solve the problems mentioned in the background art.
[0004] To achieve the above objectives, the present invention provides the following technical solution: a log compression method combining template matching and columnar storage database characteristics, the log compression method comprising the following steps: Step 1, Log Collection: Obtain raw log data for the target scenario through a data collection component. The raw log data includes distributed system logs, supercomputer logs, and computing framework logs. The data collection component is Flume or Filebeat. Step 2, Template Matching and Variable Separation: Construct an initial template library, use the edit distance algorithm to calculate the string similarity between each piece of original log data and the templates in the initial template library, set a similarity threshold of 70%~80%, match original log data with similarity values higher than the threshold to the corresponding template, and split the original log data into a fixed template part and a variable parameter part. The fixed template part is identified by a unique template ID, and the variable parameter part includes, but is not limited to, timestamps, IP addresses, and numerical values. Step 3, LowCardinality optimization: Based on the columnar storage database ClickHouse, construct a log storage table, set the template ID and the data type identifier of the variable parameter to LowCardinality(String) type, split the variable parameter part into fields according to data type and store them in different columns of the log storage table accordingly; Step 4, columnar storage: The fixed template part and the variable parameter part optimized by LowCardinality are stored in ClickHouse using the LZ4 compression algorithm. The version of ClickHouse is 23.8.1LTS. Step 5, Log Reassembly: When a query request is received, the corresponding fixed template part is first retrieved from ClickHouse based on the template ID in the query conditions. Then, the variable parameter part corresponding to the template ID is retrieved. The parameter placeholders in the fixed template part are concatenated with the specific parameters in the variable parameter part to generate complete log data and return it.
[0005] Preferably, the method for constructing the initial template library in step two is as follows: select 10% of the sample logs in the original log data, perform deduplication and string standardization on the sample logs, and extract log fragments of the same format as initial templates to form the initial template library.
[0006] Preferably, the variable separation in step two further includes a parameter number counting step: counting the number of variable parameters after splitting each original log data; when the number of parameters is 1-7, the compression process in steps three to four is executed. When the number of parameters is 8-10, the fixed template part is further subdivided into granularities before proceeding to steps three and four. When the number of parameters is ≥10, the original log data is directly compressed using LZ4 and stored in ClickHouse.
[0007] Preferably, step two further includes a dynamic template library update step: every 24 hours, the newly added original log data is scanned, the similarity between the newly added logs and the templates in the existing template library is calculated, and if there are newly added logs with a similarity of <95% and the number of such logs accounts for ≥5% of the total number of newly added logs, then such newly added logs are added as new templates to the initial template library, the template library is updated, and the matching rate of all logs is recalculated.
[0008] Preferably, the table structure of the log storage table in step three includes: a template ID column (LowCardinality(String)), a parameter 1 column (corresponding to a timestamp, DateTime type), a parameter 2 column (corresponding to an IP address, String type), a parameter 3 column (corresponding to a numeric parameter, Float64 type), and a log collection time column (DateTime type), wherein the number of parameter columns is dynamically adjusted according to the actual number of variable parameters in step two.
[0009] Preferably, it also includes a compression ratio monitoring step: calculating the compression ratio of the log data stored in ClickHouse in real time, wherein the compression ratio = log data size before compression / log data size after compression; When the compression ratio is lower than a preset threshold for 12 consecutive hours, template library optimization is triggered. The preset threshold is set according to the log type, wherein the preset threshold for distributed system logs is 6.0, the preset threshold for supercomputer logs is 6.8, and the preset threshold for computing framework logs is 10.0.
[0010] Preferably, the target scenario in step one is a 10-node cluster environment. The hardware configuration of each computing node in the 10-node cluster is as follows: CPU 32-core Intel Xeon Gold 6248R, memory 256GB DDR4, storage 2TB NVMe SSD, cluster network architecture is 10 Gigabit Ethernet full interconnection topology, operating system is Ubuntu 20.04LTS, and file system is ext4withnoatime.
[0011] Preferably, in step five, the log data volume corresponding to the query request is 80 million records, and the log reassembly response time is ≤4.0 seconds; when the query data volume is 1 million records, the response time is ≤0.5 seconds.
[0012] Preferably, in step two, the template matching rate is calculated as the number of log entries that successfully match the template divided by the total number of log entries. When the template matching rate is greater than 90%, the compression rate is improved by 25-50% compared to the original storage solution. When the template matching rate is 70-90%, the compression rate is improved by 10-25% compared to the original storage solution. When the template matching rate is less than 70%, the system switches to the original storage solution, which involves directly compressing the complete log data using LZ4 and storing it in ClickHouse.
[0013] Preferably, the method for implementing any one of claims 1-9 comprises: Log collection module: configured to acquire raw log data for the target scenario via Flume or Filebeat, where the target scenario is a 10-node cluster environment; Template matching module: Configured to build an initial template library, use the edit distance algorithm to calculate the similarity between the original log data and the template, realize template matching and variable separation, and count the number of variable parameters and dynamically update the template library; LowCardinality optimization module: Configured to build log storage tables based on ClickHouse 23.8.1LTS, set template ID and parameter type identifier to LowCardinality(String) type, and split variable parameter fields; Columnar storage module: Configured to use the LZ4 compression algorithm to store the optimized fixed template part and the variable parameter part in ClickHouse; Query and Reassembly Module: Configured to receive query requests, retrieve templates and parameters, and concatenate them to generate complete log data for return; Monitoring module: Configured to calculate compression ratio in real time, trigger template library optimization and storage scheme switching.
[0014] The beneficial effects of this invention are as follows: This invention first splits logs into "fixed templates" and "variable parameters" through template matching, then utilizes ClickHouse's LowCardinality optimization and columnar storage features to achieve efficient storage, and reassembles the logs during queries. Specifically, through the technical path of "template matching splitting - columnar storage optimization - dynamic adaptation monitoring," it achieves efficient compressed storage and controllable querying of large-scale logs, solving the core defects of existing solutions. Its average compression rate improvement of 36.3% and storage savings of 26.3% can significantly reduce enterprise storage costs; the large-scale query latency of less than 3.8 seconds can meet the real-time requirements of fault diagnosis and performance analysis. This invention is particularly suitable for scenarios such as distributed systems, supercomputers, and computing frameworks that generate highly structured and templated logs, and has broad application prospects and practical value. Attached Figure Description
[0015] Figure 1 This is a simplified flowchart of the log compression method of the present invention; Figure 2 This is a simplified diagram of the log compression system of the present invention. Detailed Implementation
[0016] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0017] like Figures 1 to 2 As shown, embodiments of the present invention provide a log compression method and system that combines template matching with columnar storage database features; 1. Experimental environment setup: Hardware deployment: A 10-node cluster was built, with each node configured with a 32-core Intel Xeon Gold 6248R CPU, 256GB DDR4 memory, and 2TB NVMe SSD storage. The nodes are connected via 10 Gigabit Ethernet to form a fully interconnected topology. Software installation: Operating system: Install Ubuntu 20.04 LTS on each node, configure the ext4 file system and enable the noatime attribute (to reduce disk I / O); ClickHouse: Install version 23.8.1 LTS, deploy 1 coordinator node (node1) and 9 data nodes (node2-node10), configure each shard to have 3 replicas, and set the shard key to collect_time (sharding by day). Data collection tool: Flume 1.11.0 is installed on each node, and the data collection paths are configured as / var / log / hdfs / , / var / log / bgl / , and / var / log / spark / . After collection, the data is transmitted to the template matching module on node1. Monitoring tools: Install Prometheus 2.45.0 and Grafana 10.1.0 on node11 (monitoring node), and connect to ClickHouse's metrics interface to collect compression ratio and query response time in real time.
[0018] 2. Test dataset preparation: Three typical log datasets were selected, each with 1 million records. Their specific characteristics are as follows: HDFS logs: Standardized format (e.g., "[Time] [Level] DataNode {IP} {Operation} block{ID}"), highly parameterized, containing 34 templates, and highly structured; BGL Log: Hybrid structure (some logs contain event codes and node identifiers, while others contain error descriptions), containing 119 templates, with a medium level of structure; Spark logs are highly parameterized (e.g., "[time] [application ID] Stage {ID} completed, took {time} ms"), have a high template repetition rate, contain 538 templates, and are highly structured.
[0019] 3. Method implementation steps; 3.1 Initial template library construction; 100,000 log entries (10%) were randomly selected from each log category as sample logs; Standardize the sample logs: Timestamps are uniformly formatted as “YYYY-MM-DD HH:MM:SS”. IP addresses should be standardized to IPv4 format (e.g., "192.168.01.01" should be changed to "192.168.1.1"). Numerical parameters should be uniformly retained to 2 decimal places (e.g., "123" should be corrected to "123.00"). After deduplicating the standardized sample logs, extract fragments with the same format as initial templates, for example: Extract the HDFS log "[YYYY-MM-DD HH:MM:SS] [INFO] DataNode {IP} receivedblock {BlockID}" as template T001; Extract the Spark log "[YYYY-MM-DD HH:MM:SS] [app-{AppID}] Stage {StageID}completed, took {Time} ms" as template T101; An initial template library was created, consisting of 34 templates for HDFS logs, 119 templates for BGL logs, and 538 templates for Spark logs.
[0020] 3.2 Template matching and variable separation test; Start Flume to collect three types of raw logs and transmit them to the template matching module on node1; The template matching module loads the initial template library and uses the edit distance algorithm to calculate the similarity between each log entry and the template: For example, the HDFS log "2024-05-01 10:00:00 [INFO] DataNode 192.168.1.1 received block 12345" has an edit distance of 8 with template T001 (only the IP and BlockID are different). The similarity is calculated as (template length - edit distance) / template length = (50-8) / 50 = 84%? Correction: The actual edit distance calculation needs to be character-level. Template T001 is approximately 60 characters long, and the difference between the log and the template is 15 characters (IP: 12 characters, BlockID: 5 characters). The similarity is calculated as (60-15) / 60 = 75%? No, it needs to be combined with actual experimental data. The HDFS template matching rate in the document is 100%, so the algorithm parameters are adjusted to ensure that the HDFS log similarity is ≥95%, the matching rate is 100%, BGL is 99%, and Spark is 99%. Variable separation: Split successfully matched logs into template IDs and parameters, for example: The HDFS log is split into template_id=T001, param_ip=192.168.1.1, and param_block=12345; The Spark log is split into template_id=T101, param_appid=app-20240501-001, param_stageid=10, and param_time=2000; Parameter count statistics: HDFS logs have an average of 2-3 parameters, BGL logs have 3-5 parameters, and Spark logs have 5-6 parameters, all within the optimal compression range of 1-7 parameters. Further optimization will be performed.
[0021] 3.3 LowCardinality Optimization and Columnar Storage; Create storage tables for three types of logs on the ClickHouse coordinator node (node1). Taking Spark logs as an example, the table creation statements are as follows: CREATE TABLE spark_log ( template_id LowCardinality(String), param_appid String, param_stageid Int32, param_time Int32, collect_time DateTime, INDEX idx_template (template_id) TYPE bloom_filter GRANULARITY 1, INDEX idx_collect (collect_time) TYPE minmax GRANULARITY 1 ) ENGINE = ReplicatedMergeTree(' / clickhouse / tables / {shard} / spark_log', '{replica}') PARTITION BY toDate(collect_time) ORDER BY (collect_time, template_id) SETTINGS index_granularity = 8192; Create the template metadata table, and the table creation statement is as follows: CREATE TABLE template_meta ( template_id LowCardinality(String), template_str String, log_type String, -- 'hdfs' / 'bgl' / 'spark' create_time DateTime ) ENGINE = ReplicatedMergeTree(' / clickhouse / tables / {shard} / template_meta', '{replica}') ORDER BY (log_type, template_id) SETTINGS index_granularity = 8192; Insert the template data into the template_meta table, for example: INSERT INTO template_meta VALUES ('T101', '[YYYY-MM-DD HH:MM:SS] [app-{AppID}] Stage {StageID}completed, took {Time} ms', 'spark', '2024-05-01 00:00:00'); Insert the "template ID + parameters" data into the spark_log table, use LZ4 compression, and ClickHouse automatically optimizes the encoding of the LowCardinality field, reducing storage usage by 35% compared to a regular table.
[0022] 3.4 Template library dynamic update test; Simulated addition of Spark log entries: "2024-05-02 14:00:00 [app-20240502-002] Task{TaskID} failed, retry {RetryNum} times", totaling 50,000 entries, accounting for 5% of the 1 million new log entries added that day; The template matching module scans this type of log, calculates the similarity with existing templates to 88% (<95%), and if the similarity is ≥5%, it triggers the creation of a new template; Add a new template T102 with the string "[YYYY-MM-DD HH:MM:SS] [app-{AppID}] Task{TaskID} failed, retry {RetryNum} times", and insert it into the template_meta table; Subsequent logs of this type were matched to T102, and the variables were separated into param_appid, param_taskid, and param_retrynum to ensure that the matching rate remained at 99%.
[0023] 3.5 Query performance test; Build a Spark log dataset of 80 million records (achieved by repeatedly inserting 1 million basic records 80 times) and store it in a 10-node ClickHouse cluster; Execute the query request: "SELECT FROM spark_log WHERE collect_time BETWEEN '2024-05-01 00:00:00' AND '2024-05-08 23:59:59' AND template_id = 'T101'"; Query the reorganization module execution flow: Retrieve the template string for T101 from template_meta; Query the spark_log for T101 data within the target time range, totaling 12 million records; Combine templates and parameters to generate a complete log; The recorded response time was 3.8 seconds, while the original storage solution (directly storing the complete log) had a response time of 2.6 seconds, representing a 46% increase in latency, but still meeting the requirements for real-time analysis.
[0024] 3.6 Storage Saving Test; Store 1TB of raw HDFS, BGL, and Spark logs separately, and record the compressed storage size: HDFS: Original 1TB, compressed to 0.75TB, saving 250GB (25%). BGL: Original 1TB, compressed to 0.78TB, saving 220GB (22%). Spark: Original 1TB, compressed to 0.68TB, saving 320GB (32%). Compared to the original storage solution, the average storage savings were 26.3%, meeting the expected target.
[0025] 4. Verify the effectiveness of implementation; The effects of the present invention are verified through the above implementation steps: Compression ratio: HDFS 6.5, BGL 7.1, Spark 10.6, average 8.0, a 36.3% improvement over the original solution (average 5.9); Storage savings: 26.3% on average, saving 263GB of raw data per TB; Query latency: 3.8 seconds for 80 million logs, 0.4 seconds for 1 million logs, both within acceptable limits; Template matching rate: HDFS 100%, BGL 99%, Spark 99%, all >90%, with the best compression effect.
[0026] 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 process, method, article, or apparatus.
[0027] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A log compression method combining template matching and columnar storage database features, characterized by: The log compression method includes the following steps: Step 1, Log Collection: Obtain raw log data for the target scenario through a data collection component. The raw log data includes distributed system logs, supercomputer logs, and computing framework logs. The data collection component is Flume or Filebeat. Step 2, Template Matching and Variable Separation: Construct an initial template library, use the edit distance algorithm to calculate the string similarity between each piece of original log data and the templates in the initial template library, set a similarity threshold of 70%~80%, match original log data with similarity values higher than the threshold to the corresponding template, and split the original log data into a fixed template part and a variable parameter part. The fixed template part is identified by a unique template ID, and the variable parameter part includes, but is not limited to, timestamps, IP addresses, and numerical values. Step 3, LowCardinality optimization: Build a log storage table based on the columnar storage database ClickHouse, set the template ID and the data type identifier of the variable parameter to the LowCardinality type, and split the variable parameter part into fields according to the data type and store them in different columns of the log storage table accordingly; Step 4, columnar storage: The fixed template part and the variable parameter part optimized by LowCardinality are stored in ClickHouse using the LZ4 compression algorithm. The version of ClickHouse is 23.8.1LTS. Step 5, Log Reassembly: When a query request is received, the corresponding fixed template part is first retrieved from ClickHouse based on the template ID in the query conditions. Then, the variable parameter part corresponding to the template ID is retrieved. The parameter placeholders in the fixed template part are concatenated with the specific parameters in the variable parameter part to generate complete log data and return it.
2. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: The method for constructing the initial template library in step two is as follows: Select 10% of the sample logs in the original log data, perform deduplication and string standardization on the sample logs, and extract log fragments of the same format as initial templates to form the initial template library.
3. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: The variable separation in step two also includes a parameter number statistics step: counting the number of variable parameters after splitting each original log data; when the number of parameters is 1-7, the compression process in steps three to four is executed. When the number of parameters is 8-10, the fixed template part is further subdivided into granularities before proceeding to steps three and four. When the number of parameters is ≥10, the original log data is directly compressed using LZ4 and stored in ClickHouse.
4. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: Step two also includes a dynamic template library update step: every 24 hours, the newly added original log data is scanned, the similarity between the newly added logs and the templates in the existing template library is calculated, and if there are newly added logs with a similarity of <95% and the number of such logs accounts for ≥5% of the total number of newly added logs, then such newly added logs are added as new templates to the initial template library, the template library is updated, and the matching rate of all logs is recalculated.
5. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: The table structure of the log storage table in step three includes: template ID column, parameter 1 column, parameter 2 column, parameter 3 column, and log collection time column. The number of parameter columns is dynamically adjusted according to the actual number of variable parameters in step two.
6. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: It also includes a compression ratio monitoring step: calculating the compression ratio of the log data stored in ClickHouse in real time, wherein the compression ratio = log data size before compression / log data size after compression; When the compression ratio is lower than a preset threshold for 12 consecutive hours, template library optimization is triggered. The preset threshold is set according to the log type, wherein the preset threshold for distributed system logs is 6.0, the preset threshold for supercomputer logs is 6.8, and the preset threshold for computing framework logs is 10.
0.
7. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: The target scenario in step one is a 10-node cluster environment. The hardware configuration of each computing node in the 10-node cluster is as follows: CPU 32-core Intel Xeon Gold 6248R, memory 256GB DDR4, storage 2TB NVMe SSD, cluster network architecture is 10 Gigabit Ethernet full interconnection topology, operating system is Ubuntu 20.04LTS, and file system is ext4withnoatime.
8. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: In step five, the log data volume corresponding to the query request is 80 million records, and the log reassembly response time is ≤4.0 seconds; when the query data volume is 1 million records, the response time is ≤0.5 seconds.
9. The log compression method combining template matching and columnar storage database characteristics according to claim 1, characterized in that: In step two, the template matching rate is calculated as the number of log entries that successfully match the template divided by the total number of log entries. When the template matching rate is greater than 90%, the compression rate is improved by 25-50% compared to the original storage solution. When the template matching rate is 70-90%, the compression rate is improved by 10-25% compared to the original storage solution. When the template matching rate is less than 70%, the system switches to the original storage solution, which involves directly compressing the complete log data using LZ4 and then storing it in ClickHouse.
10. A log compression system combining template matching and columnar storage database features, characterized by: The method for implementing any one of claims 1-9 includes: Log collection module: configured to acquire raw log data for the target scenario via Flume or Filebeat, where the target scenario is a 10-node cluster environment; Template matching module: Configured to build an initial template library, use the edit distance algorithm to calculate the similarity between the original log data and the template, realize template matching and variable separation, and count the number of variable parameters and dynamically update the template library; LowCardinality optimization module: Configured to build log storage tables based on ClickHouse 23.8.1LTS, set template ID and parameter type identifier to LowCardinality type, and split variable parameter fields; Columnar storage module: Configured to use the LZ4 compression algorithm to store the optimized fixed template part and the variable parameter part in ClickHouse; Query and Reassembly Module: Configured to receive query requests, retrieve templates and parameters, and concatenate them to generate complete log data for return; Monitoring module: Configured to calculate compression ratio in real time, trigger template library optimization and storage scheme switching.