13
13
// limitations under the License.
14
14
15
15
use std:: collections:: HashMap ;
16
+ use std:: time:: SystemTime ;
16
17
17
18
use rdkafka:: admin:: { AdminClient , AdminOptions , NewTopic , TopicReplication } ;
18
- use rdkafka:: consumer:: StreamConsumer ;
19
19
use rdkafka:: error:: { KafkaError , RDKafkaErrorCode } ;
20
20
use rdkafka:: producer:: { BaseProducer , BaseRecord } ;
21
21
use rdkafka:: ClientConfig ;
@@ -78,10 +78,19 @@ pub async fn producer(broker_addr: &str, datadir: String) {
78
78
. expect ( "failed to create topic" ) ;
79
79
80
80
let content = std:: fs:: read ( file. path ( ) ) . unwrap ( ) ;
81
- // binary message data, a file is a message
82
- if topic. ends_with ( "bin" ) {
81
+ let msgs: Box < dyn Iterator < Item = & [ u8 ] > + Send > = if topic. ends_with ( "bin" ) {
82
+ // binary message data, a file is a message
83
+ Box :: new ( std:: iter:: once ( content. as_slice ( ) ) )
84
+ } else {
85
+ Box :: new ( content. split ( |& b| b == b'\n' ) )
86
+ } ;
87
+ for msg in msgs {
83
88
loop {
84
- let record = BaseRecord :: < ( ) , _ > :: to ( topic) . payload ( & content) ;
89
+ let ts = SystemTime :: now ( )
90
+ . duration_since ( SystemTime :: UNIX_EPOCH )
91
+ . unwrap ( )
92
+ . as_millis ( ) as i64 ;
93
+ let record = BaseRecord :: < ( ) , _ > :: to ( topic) . payload ( msg) . timestamp ( ts) ;
85
94
match producer. send ( record) {
86
95
Ok ( _) => break ,
87
96
Err ( ( KafkaError :: MessageProduction ( RDKafkaErrorCode :: QueueFull ) , _) ) => {
@@ -90,19 +99,6 @@ pub async fn producer(broker_addr: &str, datadir: String) {
90
99
Err ( ( e, _) ) => panic ! ( "failed to send message: {}" , e) ,
91
100
}
92
101
}
93
- } else {
94
- for line in content. split ( |& b| b == b'\n' ) {
95
- loop {
96
- let record = BaseRecord :: < ( ) , _ > :: to ( topic) . payload ( line) ;
97
- match producer. send ( record) {
98
- Ok ( _) => break ,
99
- Err ( ( KafkaError :: MessageProduction ( RDKafkaErrorCode :: QueueFull ) , _) ) => {
100
- producer. flush ( None ) . await . expect ( "failed to flush" ) ;
101
- }
102
- Err ( ( e, _) ) => panic ! ( "failed to send message: {}" , e) ,
103
- }
104
- }
105
- }
106
102
}
107
103
producer. flush ( None ) . await . expect ( "failed to flush" ) ;
108
104
}
0 commit comments