1 | import * as kafka from '..';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const basicKafkaClient = new kafka.KafkaClient();
|
8 |
|
9 | const optionsKafkaClient = new kafka.KafkaClient({
|
10 | kafkaHost: 'localhost:9092',
|
11 | connectTimeout: 1000,
|
12 | requestTimeout: 1000,
|
13 | autoConnect: true,
|
14 | sslOptions: {},
|
15 | clientId: 'client id',
|
16 | connectRetryOptions: {
|
17 | retries: 5, factor: 0, minTimeout: 1000, maxTimeout: 1000, randomize: true
|
18 | }
|
19 | });
|
20 |
|
21 | optionsKafkaClient.connect();
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const optionsProducer = new kafka.Producer(basicKafkaClient, { requireAcks: 0, ackTimeoutMs: 0, partitionerType: 0 });
|
27 |
|
28 | const producer = new kafka.Producer(basicKafkaClient);
|
29 | producer.on('error', (error: Error) => { });
|
30 | producer.on('ready', () => {
|
31 | const messages = [
|
32 | { topic: 'topicName', messages: ['message body'], partition: 0, attributes: 2 },
|
33 | { topic: 'topicName', messages: ['message body'], partition: 0 },
|
34 | { topic: 'topicName', messages: ['message body'], attributes: 0 },
|
35 | { topic: 'topicName', messages: ['message body'] },
|
36 | { topic: 'topicName', messages: [new kafka.KeyedMessage('key', 'message')] }
|
37 | ];
|
38 |
|
39 | producer.send(messages, (err: Error) => { });
|
40 | producer.send(messages, (err: Error, data: any) => { });
|
41 |
|
42 | producer.createTopics(['t'], true, (err: Error, data: any) => { });
|
43 | producer.createTopics(['t'], (err: Error, data: any) => { });
|
44 | producer.createTopics(['t'], false, () => { });
|
45 | producer.close();
|
46 | });
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | const highLevelProducer = new kafka.HighLevelProducer(basicKafkaClient);
|
52 |
|
53 | highLevelProducer.on('error', (error: Error) => { });
|
54 | highLevelProducer.on('ready', () => {
|
55 | const messages = [
|
56 | { topic: 'topicName', messages: ['message body'], attributes: 2 },
|
57 | { topic: 'topicName', messages: ['message body'], partition: 0 },
|
58 | { topic: 'topicName', messages: ['message body'], attributes: 0 },
|
59 | { topic: 'topicName', messages: ['message body'] },
|
60 | { topic: 'topicName', messages: [new kafka.KeyedMessage('key', 'message')] }
|
61 | ];
|
62 |
|
63 | highLevelProducer.send(messages, (err: Error) => { });
|
64 | highLevelProducer.send(messages, (err: Error, data: any) => { });
|
65 |
|
66 | producer.createTopics(['t'], true, (err: Error, data: any) => { });
|
67 | producer.createTopics(['t'], (err: Error, data: any) => { });
|
68 | producer.createTopics(['t'], false, () => { });
|
69 | producer.close();
|
70 | });
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | const fetchRequests = [{ topic: 'awesome' }];
|
76 | const consumer = new kafka.Consumer(basicKafkaClient, fetchRequests, { groupId: 'abcde', autoCommit: true });
|
77 |
|
78 | consumer.on('error', (error: Error) => { });
|
79 | consumer.on('offsetOutOfRange', (error: Error) => { });
|
80 | consumer.on('message', (message: kafka.Message) => {
|
81 | const topic = message.topic;
|
82 | const value = message.value;
|
83 | const offset = message.offset;
|
84 | const partition = message.partition;
|
85 | const highWaterOffset = message.highWaterOffset;
|
86 | const key = message.key;
|
87 | });
|
88 |
|
89 | consumer.addTopics(['t1', 't2'], (err: any, added: any) => { });
|
90 | consumer.addTopics([{ topic: 't1', offset: 10 }], (err: any, added: any) => { }, true);
|
91 |
|
92 | consumer.removeTopics(['t1', 't2'], (err: any, removed: number) => { });
|
93 | consumer.removeTopics('t2', (err: any, removed: number) => { });
|
94 |
|
95 | consumer.commit((err: any, data: any) => { });
|
96 | consumer.commit(true, (err: any, data: any) => { });
|
97 |
|
98 | consumer.setOffset('topic', 0, 0);
|
99 |
|
100 | consumer.pause();
|
101 | consumer.resume();
|
102 | consumer.pauseTopics(['topic1', { topic: 'topic2', partition: 0 }]);
|
103 | consumer.resumeTopics(['topic1', { topic: 'topic2', partition: 0 }]);
|
104 |
|
105 | consumer.close(true, () => { });
|
106 | consumer.close((err: any) => { });
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | const ackBatchOptions = { noAckBatchSize: 1024, noAckBatchAge: 10 };
|
112 | const cgOptions: kafka.ConsumerGroupOptions = {
|
113 | kafkaHost: 'localhost:9092',
|
114 | batch: ackBatchOptions,
|
115 | groupId: 'groupID',
|
116 | id: 'consumerID',
|
117 | encoding: 'buffer',
|
118 | keyEncoding: 'buffer',
|
119 | sessionTimeout: 15000,
|
120 | protocol: ['roundrobin'],
|
121 | fromOffset: 'latest',
|
122 | migrateHLC: false,
|
123 | migrateRolling: true
|
124 | };
|
125 |
|
126 | const consumerGroup = new kafka.ConsumerGroup(cgOptions, ['topic1']);
|
127 | consumerGroup.on('error', (err) => { });
|
128 | consumerGroup.on('message', (msg) => { });
|
129 | consumerGroup.close(true, (err: Error) => { });
|
130 |
|
131 | const offset = new kafka.Offset(basicKafkaClient);
|
132 |
|
133 | offset.on('ready', () => { });
|
134 |
|
135 | offset.fetch([{ topic: 't', partition: 0, time: Date.now(), maxNum: 1 }, { topic: 't' }], (err: any, data: any) => { });
|
136 |
|
137 | offset.commit('groupId', [{ topic: 't', partition: 0, offset: 10 }], (err, data) => { });
|
138 |
|
139 | offset.fetchCommits('groupId', [{ topic: 't', partition: 0 }], (err, data) => { });
|
140 |
|
141 | offset.fetchLatestOffsets(['t'], (err, offsets) => { });
|
142 | offset.fetchEarliestOffsets(['t'], (err, offsets) => { });
|