9
9
import org .slf4j .LoggerFactory ;
10
10
11
11
import java .nio .charset .StandardCharsets ;
12
- import java .util .Properties ;
12
+ import java .util .* ;
13
13
14
- /**
15
- * Example application that uses the Kafka Clients API to produce messages.
16
- * Run with:
17
- * java -javaagent:path/to/superstream-clients-1.0.0.jar
18
- * -Dlogback.configurationFile=logback.xml -jar
19
- * kafka-clients-example-1.0.0-jar-with-dependencies.jar
20
- *
21
- * Prerequisites:
22
- * 1. A Kafka server with the following topics:
23
- * - superstream.metadata_v1 - with a configuration message
24
- * - superstream.clients - for client reports
25
- * - example-topic - for test messages
26
- *
27
- * Environment variables:
28
- * - KAFKA_BOOTSTRAP_SERVERS: The Kafka bootstrap servers (default:
29
- * localhost:9092)
30
- * - SUPERSTREAM_TOPICS_LIST: Comma-separated list of topics to optimize for
31
- * (default: example-topic)
32
- */
33
14
public class KafkaProducerExample {
34
15
private static final Logger logger = LoggerFactory .getLogger (KafkaProducerExample .class );
35
16
36
17
// === Configuration Constants ===
37
18
private static final String DEFAULT_BOOTSTRAP_SERVERS = "localhost:9092" ;
38
19
39
20
private static final String CLIENT_ID = "superstream-example-producer" ;
40
- private static final String COMPRESSION_TYPE = "zstd" ; // Changed from gzip to snappy for better visibility
41
- private static final Integer BATCH_SIZE = 1048576 ; // 1MB batch size
21
+ private static final String COMPRESSION_TYPE = "none" ; // Changed from gzip to snappy for better visibility
22
+ private static final Integer BATCH_SIZE = 10 ; // 1MB batch size
42
23
43
24
private static final String TOPIC_NAME = "example-topic" ;
44
25
private static final String MESSAGE_KEY = "test-key" ;
45
26
// Create a larger message that will compress well
46
27
private static final String MESSAGE_VALUE = generateLargeCompressibleMessage ();
47
28
48
29
public static void main (String [] args ) {
49
- Properties props = new Properties ();
30
+ // Create a Map with the configuration
31
+ Map <String , Object > props = new HashMap <>();
50
32
props .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , DEFAULT_BOOTSTRAP_SERVERS );
51
- props .put ("client.id" , CLIENT_ID );
33
+ props .put (ProducerConfig . CLIENT_ID_CONFIG , CLIENT_ID );
52
34
props .put (ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG , StringSerializer .class .getName ());
53
35
props .put (ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG , StringSerializer .class .getName ());
54
36
props .put (ProducerConfig .COMPRESSION_TYPE_CONFIG , COMPRESSION_TYPE );
55
37
props .put (ProducerConfig .BATCH_SIZE_CONFIG , BATCH_SIZE );
56
- props .put (ProducerConfig .LINGER_MS_CONFIG , 500 ); // Force batching by waiting 500ms
38
+ props .put (ProducerConfig .LINGER_MS_CONFIG , 500 );
39
+
57
40
58
41
long recordCount = 50 ; // Number of messages to send
59
42
try (Producer <String , String > producer = new KafkaProducer <>(props )) {
60
43
while (true ) {
61
44
// Send 50 large messages to see compression benefits
62
- logger .info ("Starting to send {} large messages..." , recordCount );
63
45
for (int i = 1 ; i <= recordCount ; i ++) {
64
46
String messageKey = MESSAGE_KEY + "-" + i ;
65
47
String messageValue = MESSAGE_VALUE + "-" + i + "-" + System .currentTimeMillis ();
66
48
producer .send (new ProducerRecord <>(TOPIC_NAME , messageKey , messageValue ));
67
49
}
68
50
69
- logger .info ("All 50 large messages queued successfully! Adding a producer.flush() to send them all at once..." );
70
51
producer .flush ();
71
- Thread .sleep (7000000 );
72
- logger .info ("Waking up and preparing to send the next batch of messages" );
73
- // return;
52
+ Thread .sleep (100000 );
74
53
}
75
54
} catch (Exception e ) {
76
55
logger .error ("Error sending message" , e );
@@ -88,10 +67,11 @@ private static String generateLargeCompressibleMessage() {
88
67
json .append (" },\n " );
89
68
json .append (" \" data\" : {\n " );
90
69
json .append (" \" metrics\" : [\n " );
91
-
70
+
92
71
// Add repeating metrics data to reach ~1KB
93
72
for (int i = 0 ; i < 15 ; i ++) {
94
- if (i > 0 ) json .append (",\n " );
73
+ if (i > 0 )
74
+ json .append (",\n " );
95
75
json .append (" {\n " );
96
76
json .append (" \" name\" : \" metric" ).append (i ).append ("\" ,\n " );
97
77
json .append (" \" value\" : " ).append (i * 10 ).append (",\n " );
@@ -102,7 +82,7 @@ private static String generateLargeCompressibleMessage() {
102
82
json .append (" }\n " );
103
83
json .append (" }" );
104
84
}
105
-
85
+
106
86
json .append ("\n ]\n " );
107
87
json .append (" },\n " );
108
88
json .append (" \" config\" : {\n " );
@@ -112,10 +92,10 @@ private static String generateLargeCompressibleMessage() {
112
92
json .append (" \" encryption\" : false\n " );
113
93
json .append (" }\n " );
114
94
json .append ("}" );
115
-
95
+
116
96
String result = json .toString ();
117
97
logger .debug ("Generated compressible message of {} bytes" , result .getBytes (StandardCharsets .UTF_8 ).length );
118
-
98
+
119
99
return result ;
120
100
}
121
101
}
0 commit comments