Data consumption method, device, electronic device, storage medium and program product
By monitoring Kafka partition reallocation events in the Flume system and caching uncommitted data, the Flume system solves the problem of repeated data consumption in the Kafka message queue, improves data consumption efficiency and reduces costs.
Patent Information
- Application Number
- CN202211379891.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-04
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2042-11-04
AI Technical Summary
When the Flume system consumes data from the Kafka message queue, it is easy to cause the problem of repeated data consumption. Especially when there is a network connection problem or the Kafka partition is reallocated, the submitted offset cannot be successfully received, resulting in repeated consumption.
The Flume system requests the Kafka server to allocate Kafka partitions, listens for partition reallocation events, caches uncommitted target data, and re-consumes data after partition reallocation is completed to avoid repeated consumption.
By monitoring Kafka partition reallocation events and caching uncommitted data, the Flume system avoids repeated data consumption, improves data consumption efficiency and saves costs.
Smart Images

Figure CN115658347B
Abstract
Description
Technical Field
[0001] The embodiments of the present disclosure relate to the field of computer technology, and in particular to a data consumption method, device, electronic device, storage medium, and program product. Background Art
[0002] Flume is a distributed, highly available system for collecting, aggregating, and transmitting massive amounts of logs. In the era of big data, Flume, as an excellent data collection tool, is widely used in multiple scenarios. Flume mainly includes four parts: Source (component used to obtain data in Flume), Interceptor (component used for data processing in Flume), Channel (storage component in Flume), and Sink (data transmission component in Flume) to complete the collection, aggregation, and transmission of logs.
[0003] Kafka is essentially a message queue. Data producers can put their production data into the Kafka message queue. Flume, as a consumer, can consume data from the Kafka message queue and complete the corresponding processing of the consumed data. However, when Flume consumes data from the Kafka message queue, it is easy to cause duplicate consumption data. Therefore, a solution is needed to solve the problem of duplicate consumption data on the Flume side. Summary of the invention
[0004] Embodiments of the present disclosure provide a data consumption method, device, electronic device, storage medium, and program product.
[0005] In a first aspect, an embodiment of the present disclosure provides a data consumption method, including:
[0006] Request the Kafka server to allocate Kafka partitions;
[0007] After receiving the information of the allocated Kafka partition returned by the Kafka server, consume data from the allocated Kafka partition and monitor the reallocation event of the allocated Kafka partition;
[0008] After monitoring the reallocation event of the Kafka partition, obtain the target data consumed from the Kafka partition but not submitted;
[0009] The target data is cleared, and after the Kafka partition is reallocated, the target data is consumed from the Kafka partition again.
[0010] Further, obtaining target data consumed from the Kafka partition but not submitted includes:
[0011] The data consumed from the Kafka partition is obtained from the cache and used as the target data.
[0012] Further, consuming data from the allocated Kafka partition includes:
[0013] Read consumption data from the message queue of the Kafka partition;
[0014] The read consumption data is cached, and the offset of the consumed data is updated based on the data volume of the read consumption data.
[0015] Furthermore, after caching the read consumption data and updating the offset of the consumed data based on the data volume of the read consumption data, the method further includes:
[0016] Submit the updated offset of consumed data to the Kafka partition;
[0017] After the updated offset of the consumed data is successfully submitted, the cached consumed data is written to the target storage device.
[0018] Furthermore, after caching a predetermined amount of the consumption data and updating the offset of the consumed data based on the predetermined amount, the method further includes:
[0019] Submit the updated offset of consumed data to the Kafka partition;
[0020] After the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted.
[0021] Furthermore, the method further comprises:
[0022] After clearing the target data, determining whether the offset of the consumed data has been updated based on the data volume of the target data;
[0023] When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
[0024] In a second aspect, an embodiment of the present disclosure provides a data consumption method, including:
[0025] Request the Kafka server to allocate Kafka partitions;
[0026] After receiving the information of the allocated Kafka partition returned by the Kafka server, a predetermined amount of consumption data is read from the message queue of the allocated Kafka partition;
[0027] caching a predetermined amount of the consumption data, and updating an offset of the consumed data based on the predetermined amount;
[0028] Submit the updated offset of consumed data to the coordinator of the Kafka partition;
[0029] After the updated offset of the consumed data is successfully submitted, writing the cached consumed data into a target storage device;
[0030] After the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted.
[0031] Furthermore, the method further comprises:
[0032] Listens for reassignment events of assigned Kafka partitions.
[0033] Furthermore, the method further comprises:
[0034] After monitoring the reallocation event of the Kafka partition, obtain the target data consumed from the Kafka partition but not submitted;
[0035] The target data is cleared, and after the Kafka partition is reallocated, the target data is consumed from the Kafka partition again.
[0036] Furthermore, the method further comprises:
[0037] After clearing the target data, determining whether the offset of the consumed data has been updated based on the data volume of the target data;
[0038] When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
[0039] In a third aspect, an embodiment of the present disclosure provides a data consumption device, including:
[0040] A first request module is configured to request allocation of a Kafka partition from a Kafka server;
[0041] A first receiving module is configured to consume data from the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server, and monitor the reallocation event of the allocated Kafka partition;
[0042] A first acquisition module is configured to acquire target data consumed from the Kafka partition but not submitted after monitoring the reallocation event of the Kafka partition;
[0043] The first clearing module is configured to clear the target data and consume the target data from the Kafka partition again after the Kafka partition is reallocated.
[0044] Furthermore, the first acquisition module includes:
[0045] The first acquisition submodule is configured to acquire data consumed from the Kafka partition from the cache and use it as target data.
[0046] Furthermore, the first receiving module includes:
[0047] A reading submodule, configured to read consumption data from the message queue of the Kafka partition;
[0048] The first cache submodule is configured to cache the read consumption data and update the offset of the consumed data based on the data volume of the read consumption data.
[0049] Further, after the first cache submodule, the device further includes:
[0050] A first submission module is configured to submit the updated offset of the consumed data to the Kafka partition;
[0051] The first writing module is configured to write the cached consumption data into a target storage device after the updated offset of the consumed data is successfully submitted.
[0052] Further, after the first cache submodule, the device further includes:
[0053] A second submission module is configured to submit the updated offset of the consumed data to the Kafka partition;
[0054] The first deletion module is configured to restore the offset of the consumed data to a value before the update and delete the cached consumed data after the updated offset of the consumed data fails to be submitted.
[0055] Furthermore, the device also includes:
[0056] A first determination module is configured to determine whether the offset of the consumed data has been updated based on the data volume of the target data after the target data is cleared;
[0057] The first recovery module is configured to restore the offset of the consumed data to a value between updates if the offset of the consumed data has been updated based on the data volume of the target data.
[0058] In a fourth aspect, an embodiment of the present disclosure provides a data consumption device, including:
[0059] The second request module is configured to request the Kafka server to allocate a Kafka partition;
[0060] A second receiving module is configured to read a predetermined amount of consumption data from the message queue of the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server;
[0061] a cache module configured to cache a predetermined amount of the consumption data and update an offset of the consumed data based on the predetermined amount;
[0062] A third submission module is configured to submit the updated offset of the consumed data to the coordinator of the Kafka partition;
[0063] A second writing module is configured to write the cached consumption data into a target storage device after the updated offset of the consumed data is successfully submitted;
[0064] The second deletion module is configured to restore the offset of the consumed data to the value before the update and delete the cached consumed data after the updated offset of the consumed data fails to be submitted.
[0065] Furthermore, the device also includes:
[0066] A listener module is configured to listen for reallocation events of assigned Kafka partitions.
[0067] Furthermore, the device also includes:
[0068] A second acquisition module is configured to acquire target data consumed from the Kafka partition but not submitted after monitoring the reallocation event of the Kafka partition;
[0069] The second clearing module is configured to clear the target data and consume the target data from the Kafka partition again after the Kafka partition is reallocated.
[0070] Furthermore, the device also includes:
[0071] A second determination module is configured to determine whether the offset of the consumed data has been updated based on the data volume of the target data after the target data is cleared;
[0072] The second recovery module is configured to restore the offset of the consumed data to a value between updates if the offset of the consumed data has been updated based on the data volume of the target data.
[0073] The functions can be implemented by hardware or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the functions.
[0074] In one possible design, the structure of the above-mentioned device includes a memory and a processor, the memory is used to store one or more computer instructions that support the above-mentioned device to execute the above-mentioned corresponding method, and the processor is configured to execute the computer instructions stored in the memory. The above-mentioned device may also include a communication interface for the above-mentioned device to communicate with other devices or communication networks.
[0075] In a fifth aspect, an embodiment of the present disclosure provides an electronic device, including a memory and a processor, wherein the memory is used to store one or more computer instructions that support any of the above-mentioned devices to execute the above-mentioned corresponding methods, and the processor is configured to execute the computer instructions stored in the memory. Any of the above-mentioned devices may also include a communication interface for communicating with other devices or communication networks.
[0076] In a sixth aspect, an embodiment of the present disclosure provides a computer-readable storage medium for storing computer instructions used by any of the above-mentioned devices, which includes computer instructions involved in executing any of the above-mentioned methods.
[0077] In a seventh aspect, an embodiment of the present disclosure provides a computer program product, which includes computer instructions, and when the computer instructions are executed by a processor, they are used to implement the steps of the method described in any of the above aspects.
[0078] The technical solution provided by the embodiments of the present disclosure may have the following beneficial effects:
[0079] In this method, the Flume system requests the Kafka server to allocate a Kafka partition. After receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition for consumption data. The Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data submitted by the Flume system from the message queue. After the Flume system reads the consumption data, it temporarily caches it, updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition.
[0080] In order to avoid partition reallocation events at Kafka partitions, which may cause the offset of consumed data submitted by the Flume system to fail to be successfully received by the Kafka partition, resulting in repeated consumption of previously consumed data by the Flume system after the partition reallocation is completed, the Flume system listens to the partition reallocation events of the Kafka partitions while consuming data. If the Flume system listens to the partition reallocation events of the Kafka partitions, it will clear the target data in the cache without writing it to the target storage device, which can be a file storage device in the Flume system or a database storage device. In this way, the problem of repeated data consumption after the partition reallocation event of the Kafka partition can be avoided.
[0081] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the embodiments of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0082] Other features, purposes and advantages of the embodiments of the present disclosure will become more apparent through the following detailed description of non-limiting embodiments in conjunction with the accompanying drawings. In the accompanying drawings:
[0083] Figure 1 A flow chart showing a data consumption method according to an embodiment of the present disclosure;
[0084] Figure 2 A flow chart showing a data consumption method according to another embodiment of the present disclosure;
[0085] Figure 3 An overall flow chart of a data consumption method according to an embodiment of the present disclosure is shown;
[0086] Figure 4 A structural block diagram of a data consumption device according to an embodiment of the present disclosure is shown;
[0087] Figure 5 A structural block diagram showing a data consumption device according to another embodiment of the present disclosure;
[0088] Figure 6 It is a structural diagram of a computer system suitable for implementing a data consumption method according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0089] Hereinafter, exemplary embodiments of the present disclosure will be described in detail with reference to the accompanying drawings so that those skilled in the art can easily implement them. In addition, for the sake of clarity, parts not related to the description of the exemplary embodiments are omitted in the accompanying drawings.
[0090] In the embodiments of the present disclosure, it should be understood that terms such as "including" or "having" are intended to indicate the existence of features, numbers, steps, behaviors, components, parts, or a combination thereof disclosed in this specification, and are not intended to exclude the possibility of one or more other features, numbers, steps, behaviors, components, parts, or a combination thereof existing or being added.
[0091] It should also be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present disclosure may be combined with each other. The embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0092] Flume systems usually include multiple data receiving Source layers, interceptors, Channel transmission channels, and data transmission components Sink. Among them, Source (which can be from log files, network ports, Kafka, etc.) collects data from multiple data sources and encapsulates it into Event (the basic transmission unit for transmitting data in Flume, consisting of Event Header and Event Body). Event can be processed through a series of interceptors and written to Channel. After the data is successfully written to the Channel, Sink (the data transmission component in Flume) will actively pull data from the Channel and write it to HDFS, HBase, Hive, ES and other big data components for subsequent data processing.
[0093] There are many kinds of built-in interceptors in Flume, including timestamp adding interceptor, host adding interceptor, regular extraction interceptor, etc. The timestamp adding interceptor adds the current time stamp to the event header in the transmitted event. The host adding interceptor can add the current host name at the time of transmission to the event header in the transmitted event. The regular extraction interceptor can extract fields in the event body into the event header through regular expressions.
[0094] Figure 1 A flow chart of a data consumption method according to an embodiment of the present disclosure is shown as follows: Figure 1 As shown, the data consumption method includes the following steps:
[0095] In step S101, a request is made to the Kafka server to allocate a Kafka partition;
[0096] In step S102, after receiving the information of the allocated Kafka partition returned by the Kafka server, data is consumed from the allocated Kafka partition, and at the same time, reallocation events of the allocated Kafka partition are monitored;
[0097] In step S103, after monitoring the reallocation event of the Kafka partition, the target data consumed from the Kafka partition but not submitted is obtained;
[0098] In step S104, the target data is cleared, and after the Kafka partition is reallocated, the target data is consumed again from the Kafka partition.
[0099] As mentioned above, Flume is a distributed, highly available system for collecting, aggregating, and transmitting massive logs. In the era of big data, Flume, as an excellent data collection tool, is widely used in multiple scenarios. Flume mainly includes four parts: Source (component used to obtain data in Flume), interceptor (component used for data processing in Flume), Channel (storage component in Flume), and Sink (data transmission component in Flume) to complete the collection, aggregation, and transmission of logs.
[0100] Kafka is essentially a message queue. The data produced by data producers can be put into the Kafka message queue. Flume, as a consumer, can consume data from the Kafka message queue and complete the corresponding processing of the consumed data. However, when Flume consumes data from the Kafka message queue, it is easy to cause duplication of consumed data. Therefore, a solution is needed to solve the problem of repeated consumption of data on the Flume side.
[0101] The inventors of the present disclosure have discovered that when an existing Flume system consumes data from a Kafka message queue, if a network connection problem or a partition reallocation problem of the Kafka message queue is encountered, the offset of the consumed data submitted by the Flume system to the Kafka partition may not successfully reach the Kafka end, resulting in the Kafka partition still transmitting the last consumed data during the next consumption, resulting in duplicate data consumption.
[0102] Considering the above problems, in this implementation, a data consumption method is proposed, in which the Flume system requests the Kafka server to allocate a Kafka partition. After receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition to consume data. The Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data from the message queue based on the offset of the consumed data submitted by the Flume system. After the Flume system reads the consumed data, it temporarily caches it, updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition.
[0103] In order to avoid partition reallocation events at Kafka partitions, which may cause the offset of consumed data submitted by the Flume system to fail to be successfully received by the Kafka partition, resulting in repeated consumption of previously consumed data by the Flume system after the partition reallocation is completed, the Flume system listens to the partition reallocation events of the Kafka partitions while consuming data. If the Flume system listens to the partition reallocation events of the Kafka partitions, it will clear the target data in the cache without writing it to the target storage device, which can be a file storage device in the Flume system or a database storage device. In this way, the problem of repeated data consumption after the partition reallocation event of the Kafka partition can be avoided.
[0104] In one embodiment of the present disclosure, the data consumption method may be applicable to be executed on an Agent server of a Flume system.
[0105] In one embodiment of the present disclosure, the data source system can be any system that generates data, and the data generated by the data source system can be collected, aggregated, and transmitted by the Flume system and distributed to the corresponding data receiving system. In some embodiments, the data receiving system can be any data storage system. The Flume system can include multiple Agents, each Agent corresponds to a set of Source layer, Channel layer, and Sink layer, and each Agent can correspond to a data source system, and different data source systems correspond to different Agents. Each Agent can correspond to one or more data receiving systems, and the data received from the same data source system can be transmitted to one or more data receiving systems after being processed. The data receiving systems corresponding to different Agents may be different.
[0106] The data generated by the data source system can be placed in the Kafka message queue for reading by the Flume system.
[0107] In one embodiment of the present disclosure, the target data consumed from the Kafka partition but not submitted can be understood as data read from the message queue of the Kafka partition, but the data is only cached in the Flume system and has not yet been written to the target storage device.
[0108] When Kafka partitions are being reallocated, a notification message can be sent to the Flume system that the Kafka partitions are being reallocated. The Flume system can monitor whether a partition reallocation event occurs in the Kafka partitions based on the notification message.
[0109] Through the above-mentioned method of the embodiment of the present disclosure, the problem of data duplication caused when the Flume system consumes data from the Kafka partition can be solved, which can improve the data consumption efficiency and save the data consumption cost.
[0110] In one embodiment of the present disclosure, step S103, i.e., the step of obtaining target data consumed from the Kafka partition but not submitted, further includes the following steps:
[0111] The data consumed from the Kafka partition is obtained from the cache and used as the target data.
[0112] In this implementation, the target data is the data stored in the Flume system cache, which is the data read by the Flume system from the message queue of the Kafka partition, and has not yet fed back to the Kafka partition that the data has been read successfully, and the data has not been submitted to the target storage device such as a database or file. This data can be deleted from the cache and no longer submitted to the target storage device such as a database or file for storage. After the partition redistribution of the Kafka partition is completed, the consumption data can be obtained again from the data cleared before consumption, that is, from the Kafka partition, so that the occurrence of repeated consumption of data can be prevented.
[0113] In one embodiment of the present disclosure, step S102, i.e., the step of consuming data from the allocated Kafka partition, further includes the following steps:
[0114] Read consumption data from the message queue of the Kafka partition;
[0115] The read consumption data is cached, and the offset of the consumed data is updated based on the data volume of the read consumption data.
[0116] In this optional implementation, the process of the Flume system consuming data from the Kafka partition includes the Flume system reading the consumed data from the message queue of the Kafka partition allocated to it by the Kafka server. The consumed data is determined by the Kafka partition based on the offset of the consumed data fed back by the previous Flume system, that is, the Flume system currently consumes data based on the offset of the consumed data, thereby putting part of the unconsumed data into the message queue for Flume data reading.
[0117] After the Flume system reads the consumption data, the consumption data is first stored in the Flume system's cache, and the Flume system updates the consumed data offset based on the current amount of consumption data, and then feeds the consumed data offset back to the Kafka partition so that the next time the data is consumed, the Kafka partition can put the new consumption data into the message queue for the Flume system to read.
[0118] In one embodiment of the present disclosure, after the step of caching the read consumption data and updating the offset of the consumed data based on the data volume of the read consumption data, the method further comprises the following steps:
[0119] Submit the updated offset of consumed data to the Kafka partition;
[0120] After the updated offset of the consumed data is successfully submitted, the cached consumed data is written to the target storage device.
[0121] In this optional implementation, in order to further avoid the problem of repeated data consumption caused by network problems or partition reallocation, the Flume system first caches the data consumed from the Kafka partition, and then writes the cached consumption data to the target storage device after the Flume system successfully submits the offset of the consumed data to the Kafka partition. The offset of the consumed data is updated based on the current amount of consumed data after each consumption of data. For example, if the original offset of the consumed data is x, and the current amount of data consumed is n, the updated offset of the consumed data can be x+n.
[0122] In one embodiment of the present disclosure, after the step of caching the read consumption data and updating the offset of the consumed data based on the data volume of the read consumption data, the method further comprises the following steps:
[0123] Submit the updated offset of consumed data to the Kafka partition;
[0124] After the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted.
[0125] In this optional implementation, in order to further avoid the problem of repeated data consumption caused by network problems or partition reallocation, the Flume system caches the data consumed from the Kafka partition first. After the Flume system fails to submit the offset of the consumed data to the Kafka partition, the cached consumption data is cleared without writing it to the target storage device, and the offset of the updated consumption data is restored to the value before the update. This ensures that the current consumption data whose offset has not been successfully submitted will not be written to the target storage device, and the next time the data is consumed from the Kafka partition again, it will not cause data duplication on the target storage device. The offset of the consumed data is updated based on the current amount of consumed data after each consumption of data. For example, if the offset of the original consumed data is x, and the current amount of data consumed is n, the offset of the updated consumed data can be x+n, and after the offset of the updated consumed data is not successfully submitted, the consumed offset is restored to x.
[0126] In one embodiment of the present disclosure, the method further comprises the following steps:
[0127] After the target data is cleared, determining whether the offset of the consumed data has been updated based on the data volume of the target data;
[0128] When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
[0129] In this optional implementation, after listening to the event of partition reallocation of Kafka partitions, the Flume system needs to restore the offset of the consumed data after clearing the target data. In other words, if the offset of the consumed data is updated by the amount of the cleared target data, the offset of the consumed data can be restored to the unupdated value, so that the currently cleared target data can be consumed again after the partition reallocation is completed.
[0130] Figure 2 A flow chart showing a data consumption method according to another embodiment of the present disclosure is shown as follows: Figure 2 As shown, the data consumption method includes the following steps:
[0131] In step S201, a request is made to the Kafka server to allocate a Kafka partition;
[0132] In step S202, after receiving the information of the allocated Kafka partition returned by the Kafka server, a predetermined amount of consumption data is read from the message queue of the allocated Kafka partition;
[0133] In step S203, a predetermined amount of the consumption data is cached, and an offset of the consumed data is updated based on the predetermined amount;
[0134] In step S204, submitting the updated offset of the consumed data to the coordinator of the Kafka partition;
[0135] In step S205, after the updated offset of the consumed data is successfully submitted, the cached consumed data is written into the target storage device;
[0136] In step S206, after the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted.
[0137] As mentioned above, Flume is a distributed, highly available system for collecting, aggregating, and transmitting massive logs. In the era of big data, Flume, as an excellent data collection tool, is widely used in multiple scenarios. Flume mainly includes four parts: Source (component used to obtain data in Flume), interceptor (component used for data processing in Flume), Channel (storage component in Flume), and Sink (data transmission component in Flume) to complete the collection, aggregation, and transmission of logs.
[0138] Kafka is essentially a message queue. The data produced by data producers can be put into the Kafka message queue. Flume, as a consumer, can consume data from the Kafka message queue and complete the corresponding processing of the consumed data. However, when Flume consumes data from the Kafka message queue, it is easy to cause duplication of consumed data. Therefore, a solution is needed to solve the problem of repeated consumption of data on the Flume side.
[0139] The inventors of the present disclosure have discovered that when an existing Flume system consumes data from a Kafka message queue, if a network connection problem or a partition reallocation problem of the Kafka message queue is encountered, the offset of the consumed data submitted by the Flume system to the Kafka partition may not successfully reach the Kafka end, resulting in the Kafka partition still transmitting the last consumed data during the next consumption, resulting in duplicate data consumption.
[0140] In view of the above problems, in this embodiment, a data consumption method is proposed, in which the Flume system requests the Kafka server to allocate a Kafka partition. After receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition to consume data. The Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data from the message queue based on the offset of the consumed data submitted by the Flume system. After the Flume system reads the consumed data, it temporarily caches it, updates the offset of the consumed data based on the read data, and submits the updated offset of the consumed data to the Kafka partition. If the updated offset of the consumed data is submitted successfully, the cached consumption data is written to the target storage device. If it is not submitted successfully, the offset of the consumed data is restored to the value between updates, and the cached consumption data is deleted.
[0141] In this way, when the network is unstable or Kafka partitions are redistributed, data will not be consumed repeatedly, which can improve data consumption efficiency and save data consumption costs.
[0142] In one embodiment of the present disclosure, the data consumption method may be applicable to be executed on an Agent server of a Flume system.
[0143] In one embodiment of the present disclosure, the data source system can be any system that generates data, and the data generated by the data source system can be collected, aggregated, and transmitted by the Flume system and distributed to the corresponding data receiving system. In some embodiments, the data receiving system can be any data storage system. The Flume system can include multiple Agents, each Agent corresponds to a set of Source layer, Channel layer, and Sink layer, and each Agent can correspond to a data source system, and different data source systems correspond to different Agents. Each Agent can correspond to one or more data receiving systems, and the data received from the same data source system can be transmitted to one or more data receiving systems after being processed. The data receiving systems corresponding to different Agents may be different.
[0144] The data generated by the data source system can be placed in the Kafka message queue for reading by the Flume system.
[0145] In one embodiment of the present disclosure, the target data consumed from the Kafka partition but not submitted can be understood as data read from the message queue of the Kafka partition, but the data is only cached in the Flume system and has not yet been written to the target storage device.
[0146] To further avoid data duplication caused by network problems or partition redistribution, the Flume system caches the data consumed from the Kafka partition first, and writes the cached consumed data to the target storage device after the Flume system successfully submits the offset of the consumed data to the Kafka partition. The offset of the consumed data is updated based on the current amount of consumed data after each consumption of data. For example, if the original offset of the consumed data is x, and the current amount of consumed data is n, the updated offset of the consumed data can be x+n.
[0147] After the Flume system fails to submit the offset of consumed data to the Kafka partition, the cached consumed data will be cleared without writing it to the target storage device, and the offset of the updated consumed data will be restored to the value before the update. This ensures that the current consumed data whose offset has not been successfully submitted will not be written to the target storage device. The next time the data is consumed from the Kafka partition again, it will not cause data duplication on the target storage device. The offset of consumed data is updated based on the current amount of consumed data after each consumption of data. For example, if the original offset of consumed data is x, and the current amount of consumed data is n, the offset of consumed data after the update can be x+n. After the offset of consumed data after the update is not successfully submitted, the consumed offset is restored to x.
[0148] In one embodiment of the present disclosure, the method further comprises the following steps:
[0149] Listens for reassignment events of assigned Kafka partitions.
[0150] In this optional implementation, in order to avoid partition reallocation events at the Kafka partition, which causes the offset of the consumed data submitted by the Flume system to fail to be successfully received by the Kafka partition, resulting in repeated consumption of previously consumed data by the Flume system after the partition reallocation is completed, the Flume system listens to the partition reallocation events of the Kafka partition while consuming data.
[0151] In one embodiment of the present disclosure, the method further comprises the following steps:
[0152] After monitoring the reallocation event of the Kafka partition, obtain the target data consumed from the Kafka partition but not submitted;
[0153] The target data is cleared, and after the Kafka partition is reallocated, the target data is consumed from the Kafka partition again.
[0154] In this optional implementation, if the Flume system monitors the partition reallocation event of the Kafka partition, it will clear the target data in the cache without writing it to the target storage device, which can be a file storage device or a database storage device in the Flume system. In this way, the problem of repeated data consumption after the partition reallocation event of the Kafka partition can be avoided.
[0155] In one embodiment of the present disclosure, the target data consumed from the Kafka partition but not submitted can be understood as data read from the message queue of the Kafka partition, but the data is only cached in the Flume system and has not yet been written to the target storage device.
[0156] When Kafka partitions are being reallocated, a notification message can be sent to the Flume system that the Kafka partitions are being reallocated. The Flume system can monitor whether a partition reallocation event occurs in the Kafka partitions based on the notification message.
[0157] In one embodiment of the present disclosure, the method further comprises the following steps:
[0158] After clearing the target data, determining whether the offset of the consumed data has been updated based on the data volume of the target data;
[0159] When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
[0160] In this optional implementation, the process of the Flume system consuming data from the Kafka partition includes the Flume system reading the consumed data from the message queue of the Kafka partition allocated to it by the Kafka server. The consumed data is determined by the Kafka partition based on the offset of the consumed data fed back by the previous Flume system, that is, the Flume system currently consumes data based on the offset of the consumed data, thereby putting part of the unconsumed data into the message queue for Flume data reading.
[0161] After the Flume system reads the consumption data, the consumption data is first stored in the Flume system's cache, and the Flume system updates the consumed data offset based on the current amount of consumption data, and then feeds the consumed data offset back to the Kafka partition so that the next time the data is consumed, the Kafka partition can put the new consumption data into the message queue for the Flume system to read.
[0162] After listening to the event of partition reallocation of Kafka partitions, the Flume system needs to restore the offset of the consumed data after clearing the target data. In other words, if the offset of the consumed data is updated by the amount of the cleared target data, the offset of the consumed data can be restored to the unupdated value, so that the currently cleared target data can be consumed again after the partition reallocation is completed.
[0163] Figure 2 The technical terms and technical features involved in the embodiments shown and related Figure 1 The technical terms and technical features mentioned in the embodiments shown and related are the same or similar. Figure 2 The explanation and description of the technical terms and technical features involved in the embodiments shown and related can refer to the above Figure 1 The explanations of the illustrated and related embodiments will not be repeated here.
[0164] Figure 3 The overall flow chart of the data consumption method according to one embodiment of the present disclosure is shown. Figure 3 As shown in the figure, the data source system sends the generated data to the Kafka system, and the Kafka system stores the data in different partitions. The Flume system requests the Kafka system to consume the data generated by the data source system. The Kafka system allocates the corresponding Kafka partition to the Flume system, and the Flume system reads the corresponding data from the allocated Kafka partition. The read data is stored in the cache of the Flume system. Then, the offset of the consumed data is updated based on the amount of the read data, and the offset of the consumed data is fed back to the Kafka system.
[0165] During the data consumption period, the Flume system will monitor the partition reallocation events generated by the Kafka system in real time. Once the event is detected, the Flume system will clear the target data stored in the cache and restore the offset of the consumed data that has been updated based on the data volume of the target data.
[0166] The Flume system also clears the cached data when the offset of the consumed data fails to be successfully submitted to the Kafka system, and restores the offset of the consumed data that has been updated by the amount of cleared data.
[0167] The following are embodiments of the apparatus of the present disclosure, which can be used to execute embodiments of the method of the present disclosure.
[0168] Figure 4 FIG. 1 is a block diagram showing a data consumption device according to an embodiment of the present disclosure. The device can be implemented as part or all of an electronic device through software, hardware, or a combination of both. Figure 4 As shown, the data consumption device includes:
[0169] The first request module 401 is configured to request the Kafka server to allocate a Kafka partition;
[0170] The first receiving module 402 is configured to consume data from the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server, and monitor the reallocation event of the allocated Kafka partition;
[0171] The first acquisition module 403 is configured to acquire target data consumed from the Kafka partition but not submitted after monitoring the reallocation event of the Kafka partition;
[0172] The first clearing module 404 is configured to clear the target data and consume the target data from the Kafka partition again after the Kafka partition is reallocated.
[0173] Figure 5 FIG. 2 shows a structural block diagram of a data consumption device according to another embodiment of the present disclosure. The device can be implemented as part or all of an electronic device through software, hardware, or a combination of both. Figure 5 As shown, the data consumption device includes:
[0174] The second request module 501 is configured to request the Kafka server to allocate a Kafka partition;
[0175] The second receiving module 502 is configured to read a predetermined amount of consumption data from the message queue of the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server;
[0176] A cache module 503 is configured to cache a predetermined amount of the consumption data and update an offset of the consumed data based on the predetermined amount;
[0177] A third submission module 504 is configured to submit the updated offset of the consumed data to the coordinator of the Kafka partition;
[0178] The second writing module 505 is configured to write the cached consumption data into a target storage device after the updated offset of the consumed data is successfully submitted;
[0179] The second deletion module 506 is configured to restore the offset of the consumed data to the value before the update and delete the cached consumed data after the updated offset of the consumed data fails to be submitted.
[0180] The technical features involved in the above-mentioned device embodiment and their corresponding explanations and descriptions are the same, corresponding or similar to the technical features involved in the method embodiment described above and their corresponding explanations and descriptions. For the technical features involved in the above-mentioned device embodiment and their corresponding explanations and descriptions, reference may be made to the technical features involved in the above-mentioned method embodiment and their corresponding explanations and descriptions, and the present disclosure will not repeat them here.
[0181] The present disclosure also discloses an electronic device, which includes a memory and a processor; wherein:
[0182] The memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement any of the above method steps.
[0183] Figure 6 It is a structural diagram of a computer system suitable for implementing a data consumption method according to an embodiment of the present disclosure.
[0184] like Figure 6 As shown, the computer system 600 includes a processing unit 601, which can perform various processes in the above-mentioned embodiments according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage part 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the computer system 600 are also stored. The processing unit 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0185] The following components are connected to the I / O interface 605: an input part 606 including a keyboard, a mouse, etc.; an output part 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker; a storage part 608 including a hard disk, etc.; and a communication part 609 including a network interface card such as a LAN card, a modem, etc. The communication part 609 performs communication processing via a network such as the Internet. The drive 610 is also connected to the I / O interface 605 as needed. Removable media 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., are installed on the drive 610 as needed, so that the computer program read therefrom is installed into the storage part 608 as needed. Among them, the processing unit 601 can be implemented as a processing unit such as a CPU, a GPU, a TPU, an FPGA, an NPU, etc.
[0186] In particular, according to an embodiment of the present disclosure, the method described above can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program tangibly contained on a readable medium thereof, and the computer program includes a program code for executing the data transmission method. In such an embodiment, the computer program can be downloaded and installed from a network through the communication part 609, and / or installed from a removable medium 611.
[0187] The embodiment of the present disclosure also discloses a computer program product, which includes a computer program / instruction, and when the computer program / instruction is executed by a processor, any of the above method steps is implemented.
[0188] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the road map or block diagram can represent a part of a module, program segment or code, and the part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a different order from the order marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0189] The units or modules involved in the embodiments described in the present disclosure may be implemented by software or hardware. The units or modules described may also be set in a processor, and the names of these units or modules do not constitute limitations on the units or modules themselves in some cases.
[0190] As another aspect, the embodiments of the present disclosure further provide a computer-readable storage medium, which may be a computer-readable storage medium included in the device described in the above-mentioned embodiment; or a computer-readable storage medium that exists independently and is not assembled into the device. The computer-readable storage medium stores one or more programs, and the programs are used by one or more processors to execute the methods described in the embodiments of the present disclosure.
[0191] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the embodiments of the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the inventive concept. For example, the above features are replaced with (but not limited to) technical features with similar functions disclosed in the embodiments of the present disclosure.
Claims
1. A data consumption method, comprising: Request the Kafka server to allocate a Kafka partition; wherein, the Flume system requests the Kafka server to allocate a Kafka partition. After receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition for consumption data. The Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data submitted by the Flume system from the message queue. After reading the consumption data, the Flume system temporarily caches it, updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition. After receiving the information of the allocated Kafka partition returned by the Kafka server, consume data from the allocated Kafka partition and monitor the reallocation event of the allocated Kafka partition; After monitoring the reallocation event of the Kafka partition, the target data consumed from the Kafka partition but not submitted is obtained; the target data is the data stored in the Flume system cache, which is the data read by the Flume system from the message queue of the Kafka partition, and the target data has not been fed back to the Kafka partition to indicate that the target data has been read successfully, and the target data has not been submitted to the target storage device; The target data is cleared, and after the Kafka partition is reallocated, the target data is acquired from the Kafka partition again.
2. The method according to claim 1, wherein: After the Flume system reads the consumption data, it temporarily caches it, and updates the offset of the consumed data based on the read data, the method further includes: Submit the updated offset of consumed data to the Kafka partition; After the updated offset of the consumed data is successfully submitted, the cached consumed data is written to the target storage device.
3. The method according to claim 1, wherein: After the Flume system reads the consumption data, it temporarily caches it, and updates the offset of the consumed data based on the read data, the method further includes: Submit the updated offset of consumed data to the Kafka partition; After the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted.
4. The method according to any one of claims 1 to 3, wherein: The method further comprises: After clearing the target data, determining whether the offset of the consumed data has been updated based on the data volume of the target data; When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
5. A data consumption method, comprising: Request the Kafka server to allocate a Kafka partition; wherein, the Flume system requests the Kafka server to allocate a Kafka partition. After receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition for consumption data. The Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data submitted by the Flume system from the message queue. After reading the consumption data, the Flume system temporarily caches it, updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition. After receiving the information of the allocated Kafka partition returned by the Kafka server, a predetermined amount of consumption data is read from the message queue of the allocated Kafka partition; caching a predetermined amount of the consumption data, and updating an offset of the consumed data based on the predetermined amount; Submit the updated offset of consumed data to the coordinator of the Kafka partition; After the updated offset of the consumed data is successfully submitted, writing the cached consumed data into a target storage device; After the updated offset of the consumed data fails to be submitted, the offset of the consumed data is restored to the value before the update, and the cached consumed data is deleted; The method further comprises: Listen for reallocation events of allocated Kafka partitions; After monitoring the reallocation event of the Kafka partition, the target data consumed from the Kafka partition but not submitted is obtained; the target data is the data stored in the Flume system cache, which is the data read by the Flume system from the message queue of the Kafka partition, and the target data has not been fed back to the Kafka partition to indicate that the target data has been read successfully, and the target data has not been submitted to the target storage device; The target data is cleared, and after the Kafka partition is reallocated, the target data is acquired from the Kafka partition again.
6. The method according to claim 5, wherein: The method further comprises: After clearing the target data, determining whether the offset of the consumed data has been updated based on the data volume of the target data; When the offset of the consumed data is updated based on the data amount of the target data, the offset of the consumed data is restored to a value between updates.
7. A data consumption device, comprising: The first request module is configured to request the Kafka server to allocate a Kafka partition; wherein the Flume system requests the Kafka server to allocate a Kafka partition, and after receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition for consumption data, and the Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data from the message queue based on the offset of the consumed data submitted by the Flume system; after the Flume system reads the consumption data, it temporarily caches it, and updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition; A first receiving module is configured to consume data from the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server, and monitor the reallocation event of the allocated Kafka partition; A first acquisition module is configured to acquire target data consumed from the Kafka partition but not submitted after monitoring the reallocation event of the Kafka partition; the target data is data stored in the Flume system cache, which is data read by the Flume system from the message queue of the Kafka partition, and has not yet fed back to the Kafka partition that the target data has been read successfully, and the target data has not yet been submitted to the target storage device; The first clearing module is configured to clear the target data and reacquire the target data from the Kafka partition after the Kafka partition is reallocated.
8. The device according to claim 7, wherein: After the Flume system reads the consumption data, it temporarily caches it and updates the offset of the consumed data based on the read data. The device also includes: A first submission module is configured to submit the updated offset of the consumed data to the Kafka partition; The first writing module is configured to write the cached consumption data into a target storage device after the updated offset of the consumed data is successfully submitted.
9. The device according to claim 8, wherein: After the Flume system reads the consumption data, it temporarily caches it, and updates the offset of the consumed data based on the read data, the device also includes: A second submission module is configured to submit the updated offset of the consumed data to the Kafka partition; The first deletion module is configured to restore the offset of the consumed data to a value before the update and delete the cached consumed data after the updated offset of the consumed data fails to be submitted.
10. The device according to any one of claims 7 to 9, wherein: The device also includes: A first determination module is configured to determine whether the offset of the consumed data has been updated based on the data volume of the target data after the target data is cleared; The first recovery module is configured to restore the offset of the consumed data to a value between updates if the offset of the consumed data has been updated based on the data volume of the target data.
11. A data consumption device, comprising: The second request module is configured to request the Kafka server to allocate a Kafka partition; wherein the Flume system requests the Kafka server to allocate a Kafka partition, and after receiving the Kafka partition allocated by the Kafka server, the Flume system requests the Kafka partition for consumption data, and the Kafka partition allows the Flume system to read the unconsumed data after the offset of the consumed data submitted by the Flume system from the message queue; after the Flume system reads the consumption data, it temporarily caches it, and updates the offset of the consumed data based on the read data, and submits the offset of the consumed data to the Kafka partition; A second receiving module is configured to read a predetermined amount of consumption data from the message queue of the allocated Kafka partition after receiving the information of the allocated Kafka partition returned by the Kafka server; a cache module configured to cache a predetermined amount of the consumption data and update an offset of the consumed data based on the predetermined amount; A third submission module is configured to submit the updated offset of the consumed data to the coordinator of the Kafka partition; A second writing module is configured to write the cached consumption data into a target storage device after the updated offset of the consumed data is successfully submitted; A second deletion module is configured to restore the offset of the consumed data to a value before the update and delete the cached consumed data after the updated offset of the consumed data fails to be submitted; The device also includes: A listening module, configured to listen to reallocation events of allocated Kafka partitions; The second acquisition module is configured to acquire target data consumed from the Kafka partition but not submitted after monitoring the reallocation event of the Kafka partition; the target data is data stored in the Flume system cache, which is data read by the Flume system from the message queue of the Kafka partition, and has not yet fed back to the Kafka partition that the target data has been read successfully, and the target data has not yet been submitted to the target storage device; The second clearing module is configured to clear the target data and reacquire the target data from the Kafka partition after the Kafka partition is reallocated.
12. The device according to claim 11, wherein The device also includes: A second determination module is configured to determine whether the offset of the consumed data has been updated based on the data volume of the target data after the target data is cleared; The second recovery module is configured to restore the offset of the consumed data to a value between updates if the offset of the consumed data has been updated based on the data volume of the target data.
13. An electronic device comprising a memory and a processor; wherein: The memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement the steps of the method according to any one of claims 1 to 6.
14. A computer-readable storage medium having computer instructions stored thereon, wherein: When the computer instructions are executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
15. A computer program product, comprising a computer program / instruction, which implements the steps of the method according to any one of claims 1 to 6 when executed by a processor.
